sheefo
August 23rd, 2006, 03:42 PM
I can't really explain my problem (and it's not really 'for' loops, I just said that so people wouldn't be scared).
I am making an XML generator for with wxRuby to generate XML code for units in my 3D RTS strategy game. My game reads the unit's attributes from XML code.
The loop runs through the 'ATTRIBUTES_LIST' array and puts the name in a certain XML header. Now, the class 'Unit' has 'attr_accessor' attributes with the same name as each object in the 'ATTRIBUTES_LIST' array. I want to access the same attribute in the array as in the 'Unit' class. This means I don't want to have to delete the loop and add each one seperatly.
ATTRIBUTES_LIST = ["max_health", "attack", "defence", "firing_rate", "sight_range",
"speed", "terrain_type", "construction_time", "construction_cost"]
def onGenerate(event)
@CODE = String.new
@CODE << "/*\n\tCritical Mass - Units.xml\n\tGenerated on: #{Time.new}\n*/\n"
@CODE << HEADER
# Sort in alphabetical order
UNITS_ARRAY.sort!{|x, y|x.unit_name <=> y.unit_name}
for unit in UNITS_ARRAY
@entry = String.new
@entry << ENTRY
@entry << NAME + (unit.unit_name).downcase + NAME_
for att in ATTRIBUTES_LIST
@entry << ATTRIBUTE
@entry << ATTNAME + att + ATTNAME_
### NEED TO EDIT THE FOLLOWING!!!
@entry << ATTVALUE + (unit.max_health).to_s + ATTVALUE_
@entry << ATTRIBUTE_
end
@entry << ENTRY_
@CODE << @entry
end
@CODE << FOOTER
@xml_file = File.open("units.xml", "wb")
@xml_file.write(@CODE)
@xml_file.close
puts @CODE
end
It generates this:
* Notice how all the 'AttValue' are the same. That the problem, I need to loop the classes 'attr_accessors'.
/*
Critical Mass - Units.xml
Generated on: Wed Aug 23 20:28:58 GMT Standard Time 2006
*/
<VOBS>
<VOB>
<Name>abra</Name>
<Attribute>
<AttName>max_health</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>attack</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>defence</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>firing_rate</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>sight_range</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>speed</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>terrain_type</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>construction_time</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>construction_cost</AttName>
<AttValue>0</AttValue>
</Attribute>
</VOB>
<VOB>
<Name>kadabra</Name>
<Attribute>
<AttName>max_health</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>attack</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>defence</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>firing_rate</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>sight_range</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>speed</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>terrain_type</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>construction_time</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>construction_cost</AttName>
<AttValue>0</AttValue>
</Attribute>
</VOB>
</VOBS>
I am making an XML generator for with wxRuby to generate XML code for units in my 3D RTS strategy game. My game reads the unit's attributes from XML code.
The loop runs through the 'ATTRIBUTES_LIST' array and puts the name in a certain XML header. Now, the class 'Unit' has 'attr_accessor' attributes with the same name as each object in the 'ATTRIBUTES_LIST' array. I want to access the same attribute in the array as in the 'Unit' class. This means I don't want to have to delete the loop and add each one seperatly.
ATTRIBUTES_LIST = ["max_health", "attack", "defence", "firing_rate", "sight_range",
"speed", "terrain_type", "construction_time", "construction_cost"]
def onGenerate(event)
@CODE = String.new
@CODE << "/*\n\tCritical Mass - Units.xml\n\tGenerated on: #{Time.new}\n*/\n"
@CODE << HEADER
# Sort in alphabetical order
UNITS_ARRAY.sort!{|x, y|x.unit_name <=> y.unit_name}
for unit in UNITS_ARRAY
@entry = String.new
@entry << ENTRY
@entry << NAME + (unit.unit_name).downcase + NAME_
for att in ATTRIBUTES_LIST
@entry << ATTRIBUTE
@entry << ATTNAME + att + ATTNAME_
### NEED TO EDIT THE FOLLOWING!!!
@entry << ATTVALUE + (unit.max_health).to_s + ATTVALUE_
@entry << ATTRIBUTE_
end
@entry << ENTRY_
@CODE << @entry
end
@CODE << FOOTER
@xml_file = File.open("units.xml", "wb")
@xml_file.write(@CODE)
@xml_file.close
puts @CODE
end
It generates this:
* Notice how all the 'AttValue' are the same. That the problem, I need to loop the classes 'attr_accessors'.
/*
Critical Mass - Units.xml
Generated on: Wed Aug 23 20:28:58 GMT Standard Time 2006
*/
<VOBS>
<VOB>
<Name>abra</Name>
<Attribute>
<AttName>max_health</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>attack</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>defence</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>firing_rate</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>sight_range</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>speed</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>terrain_type</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>construction_time</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>construction_cost</AttName>
<AttValue>0</AttValue>
</Attribute>
</VOB>
<VOB>
<Name>kadabra</Name>
<Attribute>
<AttName>max_health</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>attack</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>defence</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>firing_rate</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>sight_range</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>speed</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>terrain_type</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>construction_time</AttName>
<AttValue>0</AttValue>
</Attribute>
<Attribute>
<AttName>construction_cost</AttName>
<AttValue>0</AttValue>
</Attribute>
</VOB>
</VOBS>