Page 1 of 1

How to copy CfgElemRef into struct?

Posted: Mon Aug 06, 2007 2:02 am
by Miral
I have a config file which contains object (spell) properties:

Code: Select all

Spell 1
{
	Name			Ultimate Spell Of Mass Destruction
	PowerWords		Kal Vas Mon Ty Pyt Hon
	ManaCost		50
	Difficulty		50
	CastType		Normal
	TargetType		Mobile
	Notoriety		Harmful
	ResistType		Damage
	Animation		Target
	Sound		0x209
	CastCycles		4
	CycleDuration	800
}
Some properties are not mandatory, they are present only if I want to override default setting. I read the entry using

Code: Select all

var spellcfg := ReadConfigFile(":NpcSpells:NpcSpells");
var cspell := spellcfg[id];
cspell now contains something named ConfigElemRef. It's the config element. I can access cspell's properties like I'd do with struct: cspell.Animation for example. It's not a struct however. Since I want to change some values from symbolic values (like Animation - "Target") into numeric (0x10), I need to be able to modify values. "Ok", I thought, "let's copy it into real struct". Sounds easy, but as hard as I tried, I found it impossible to do.

There is a function ListConfigElemProps which returns names of all element's properties. Good. But How do I read specific property in a generic way when I don't know its type and there are only type-specific functions like GetConfigString or GetConfigInt? I'd do something like

Code: Select all

var spell := struct;
foreach prop in (ListConfigElemProps(cspell))
	spell.Insert(prop, GetConfigXXX(cspell, prop));
endforeach
... but it can't be done in a generic way unless I'm missing something obvious. Any help would be appreciated.

Posted: Mon Aug 06, 2007 4:27 am
by Austin
If you want to know why this wont work, you can check the error value returned by the core...

Code: Select all

var spell := struct;
foreach prop in (ListConfigElemProps(cspell))
   var result := spell.Insert(prop, GetConfigXXX(cspell, prop));
   if ( result == error )
        print(error.errortext);
   endif
endforeach

There are two options:
1 - Use a dictionary data-type (associative array)
2 - Use the [] operator on the struct because insert isnt a method for structs.

Posted: Mon Aug 06, 2007 4:43 am
by Miral
Austin wrote:There are two options:
1 - Use a dictionary data-type (associative array)
2 - Use the [] operator on the struct because insert isnt a method for structs.
According to Racalac's Escript guide, struct is internally a dictionary and dictionary methods work for structs.

Code: Select all

var spell := struct;
spell.Insert("a", 1);
spell.Insert("b", "string1");
print(spell);
Output:

Code: Select all

struct{ a = 1, b = "string1" }
My problem is not in creating the struct/dictionary, but in getting the value of ConfigElem property, where name of this property is in a variable.

Posted: Mon Aug 06, 2007 3:51 pm
by Austin

Code: Select all

var elem_props = dictionary;
foreach property in ( ListConfigElemProps(elem_ref) )
      var values := GetConfigStringArray(elem_ref, property);
      if ( values.Size() > 1 )
           elem_props[property] := values; // Array
      else
           elem_props[property] := values[1];
      endif
endforeach