How to copy CfgElemRef into struct?

Get Help on scripting in POL with configurations, config setups, script trouble shooting, etc.
Post Reply
Miral
New User
Posts: 5
Joined: Mon Aug 06, 2007 1:39 am

How to copy CfgElemRef into struct?

Post 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.
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Post 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.
Miral
New User
Posts: 5
Joined: Mon Aug 06, 2007 1:39 am

Post 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.
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Post 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
Post Reply