ID:1654360
 
(See the best response by Pirion.)
Code:
proc
LoadPrototypes()
var/XML/Element/root = xmlRootFromPath()
world.log << "Loading prototypes"
for(var/XML/Element/ParentType in root.ChildElements())

var/parent_id = ParentType.Attribute("parent_id")
var/atomtype = ParentType.Attribute("atomtype")

world.log << "Loading prototypes of parent_id \"[parent_id]\""

for(var/XML/Element/Child in ParentType.ChildElements())

var/proto_id = parent_id + ": " + Child.Attribute("id")
var/prototype/proto = new(proto_id, atomtype)

//if(proto)
// world.log << "Created prototype with id \"[proto.id]\""

for(var/XML/Element/ChildVars in Child.ChildElements())
//proto.properties += ChildVars.Tag()
world.log << "Adding \"[ChildVars.Tag()]\" with a value \"[ChildVars.Text()]\" to properties of \"[proto.id]\""

proto.properties["[ChildVars.Tag()]"] = ChildVars.Text() /*Error on this line*/



prototype
var
id
atomtype
list/properties = list()
//there are more procs underneath but I don't want to overcrowd this post


Problem description:
Inspired by Ter13's tutorial on not using subtypes for everything, I decided that a good idea would be to load in the parameters for objects from an XML with the aid of Deadron's XML library. However I'm getting a "bad index" error at runtime on the line noted above, where I am trying to set the properties.

That description was probably clear as mud, but I am willing to elaborate more if there are specific questions.
Best response
Could you post the XML and the log message right before the error?
Here is what there is in the only xml file right now. In the future they may be more.

<weapons parent_id="weapon" atomtype="/obj/item/equipment/weapon">
<weapon id="machete">
<name>Machete</name>
<icon_state>machete</icon_state>
<damage>5</damage>
</weapon>

<weapon id="longsword">
<name>Longsword</name>
<icon_state>longsword</icon_state>
<damage>8</damage>
</weapon>

<weapon id="demon">
<name>Chaos Blade</name>
<icon_state>demon</icon_state>
<damage>17</damage>
</weapon>

<weapon id="holy">
<name>Holy Guardian</name>
<icon_state>holy</icon_state>
<damage>25</damage>
</weapon>

</weapons>


Last log message is 'Adding "name" with a value "Machete" to properties of "weapon: machete"'
No error when using the mentioned library with the code and config you posted. [Side note, New() code is needed to make this work ;)]


Are you possibly destroying the list() you created for properties in your new?
var/list/prototypes = list()


proc
InheritAtom(id, atom/ref)
var/prototype/proto = prototypes[id]
proto.Inherit(ref)

prototype
var
id
atomtype
list/properties = list()
proc
Instance(loc=null)
new atomtype(loc, id)
Inherit(atom/ref)
for(var/prop in properties)
if(prop in ref.vars)
ref.vars[prop] = properties[prop]
else
world.log << "Variable \"[prop]\" not found in [ref]."

New(id, atom_type, list/properties)
src.id = id
src.atomtype = atomtype
src.properties = properties

atom
var
id = null

New(loc=null, id=null)
..(loc)
if(id)
src.id = id
if(src.id)
InheritAtom(src.id, src)
Read(savefile/F)
..(F)
if(id)
InheritAtom(id, src)


The remainder of my code. Still getting an error.
Are you possibly destroying the list() you created for properties in your new?

This is what you're doing.

    New(id, atom_type, list/properties) //you're only passing two arguments, properties is uninitialized
src.id = id
src.atomtype = atomtype //missed an _ here?
src.properties = properties //you then set src.properties = null on this line.


Here is the fixed code:


    New(id, atom_type, list/properties)
src.id = id
src.atomtype = atom_type //add the _
//check if the properties exists - you could go as far as check if it is a list here
if(properties) src.properties = properties
Thanks a bunch! It's working now. I decided to load the properties into its own list and make the new prototype later so I could pass the list as an argument.