Reading in data files and XML
by baylor wetzel · in Torque Game Builder · 05/30/2009 (11:57 pm) · 1 replies
i make data-driven games that allow user generated content. In the past, i used standard key=value INI files, which i can parse in TorqueScript (although i wish there was an easy way to split fields based on something other than a single space). But my current project is more complicated and i'm considering XML
i am assuming TorqueScript does not support XML (or any other config file format other than perhaps data blocks). Is this correct?
Does the TGB engine have any built in support for XML (or, again, other config file formats)?
If not, i'm sure i can find an open source XML parser (which i assume is faster than writing my own). Anyone happen to know of any issues i should watch out for when integrating an XML engine into the TGB 1.7.4 source code?
If it makes a difference, i haven't decided on the final data format but it will be a couple hundred objects (representing clothes), two or three dozen groups (representing social groups such as cowboy, goth and punk), six attributes (respect, fear, etc.) and essentially a sparse matrix between them (there will also be some other info for the objects, like clothing type, image, etc). The data files will be generated by a tool i need to write (probably in C# since the prototype is already in that). This is for a clothing-based reaction system. The data would look something like:
Most data values will be blank (i.e., cowboy hat has no fear value for punks, no values at all for stoners, etc.). It's a sparse set of 3-part items (group, influenceType, value), currently implemented in C# as lists of dictionaries. i haven't figured out how to implement this in TGB (a hashtable in TGB would be really nice but i'm guessing i'll need to move this to the engine, which means applying stl_fix to the TGB memory manager so i can get access to hashtables)
If there were a super-simple way of representing this with t2Ddatablocks (or whatever they're called) that can be auto-serialized and loaded, that would be nice, but i'm assuming i need to write my own content loader and hence my question about XML
i am assuming TorqueScript does not support XML (or any other config file format other than perhaps data blocks). Is this correct?
Does the TGB engine have any built in support for XML (or, again, other config file formats)?
If not, i'm sure i can find an open source XML parser (which i assume is faster than writing my own). Anyone happen to know of any issues i should watch out for when integrating an XML engine into the TGB 1.7.4 source code?
If it makes a difference, i haven't decided on the final data format but it will be a couple hundred objects (representing clothes), two or three dozen groups (representing social groups such as cowboy, goth and punk), six attributes (respect, fear, etc.) and essentially a sparse matrix between them (there will also be some other info for the objects, like clothing type, image, etc). The data files will be generated by a tool i need to write (probably in C# since the prototype is already in that). This is for a clothing-based reaction system. The data would look something like:
Name = CowboyHat Type = Hat Image = cowboyHat.png Group = Punk InfluenceType = Amuse Value = 10 InfluenceType = Respect Value = -20 Group = Sorority InfluenceType = Fear Value = 10
Most data values will be blank (i.e., cowboy hat has no fear value for punks, no values at all for stoners, etc.). It's a sparse set of 3-part items (group, influenceType, value), currently implemented in C# as lists of dictionaries. i haven't figured out how to implement this in TGB (a hashtable in TGB would be really nice but i'm guessing i'll need to move this to the engine, which means applying stl_fix to the TGB memory manager so i can get access to hashtables)
If there were a super-simple way of representing this with t2Ddatablocks (or whatever they're called) that can be auto-serialized and loaded, that would be nice, but i'm assuming i need to write my own content loader and hence my question about XML
About the author
Torque Owner Koen Van Baelen
TGB does support XML, but it's very poorly documented. Luckily, it's very easy to use. There's an XML object you can use. Here's how:
Suppose you have this XML file:
<XML Section> <SomeData>Random value</SomeData> </XML SectionThen you can read it like this:
%Xml = new ScriptObject() { class = "XML"; }; %Xml.beginRead("Filename.xml"); %Xml.readClassBegin("XML Section"); %SomeData = %Xml.readField("SomeData"); %Xml.readClassEnd(); %Xml.delete();That's all there is to it!