design choice: space or comma ?

Started by
15 comments, last by Washu 14 years, 10 months ago
I have not used xml before, but wouldn't that mean I'd have to wait til the point where my entity knows about the value type, so that it can then iterate through the xml tags and load their values ?
I can't say what kind of type it is at load time, and I don't wanna put too much parsing code into the entity classes.
so the only way to avoid actual xml parsing in the entity class (and every other class that uses xml data) is to create a generic type, that can store every kind of data.
Advertisement
Quote:Original post by TTK-Bandit
I have not used xml before, but wouldn't that mean I'd have to wait til the point where my entity knows about the value type, so that it can then iterate through the xml tags and load their values ?
I can't say what kind of type it is at load time, and I don't wanna put too much parsing code into the entity classes.
so the only way to avoid actual xml parsing in the entity class (and every other class that uses xml data) is to create a generic type, that can store every kind of data.
But then how do you know what is actually stored in that generic type? Maybe you should just try working with XML. It sounds like you don't understand it or how it's parsed. Even if you don't use it in this case, it's something that every programmer really should know.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
"But then how do you know what is actually stored in that generic type?"
I do the conversion of the string to the actual type when the entity spawns.
If I'm not totally off here, I'd guess with xml I'd have to do something like this:
node = xmlData->FindFirstOff("entity");while( node != NULL ) {    if ( node->GetParam("classname") == "item" && node->GetParam("name") == "armor" ) {        subNode = node->FindFirstOff("bounds");        if ( subNode ) {            subNode = node->FindFirstOff("mins");            if ( subNode ) {                mins[0] = subNode->GetParam("x");                mins[1] = subNode->GetParam("y");                mins[2] = subNode->GetParam("z");            }            subNode = node->FindFirstOff("maxs");            if ( subNode ) {                maxs[0] = subNode->GetParam("x");                maxs[1] = subNode->GetParam("y");                maxs[2] = subNode->GetParam("z");            }        }    }}

correct me if i'm wrong.
I see the value of xml for some stuff, just not for the stuff i'm currently on.
I will try definitely try out xml when I get some time.. will tinyXML be ok as library?
Putting aside the whole xml debate, I would use comma and ignore spaces that aren't enclosed in quotes or some other escape mechanism. The reason is that this is the natural way of writing out such lists, at least in English. Since they're plain text files eventually somebody is going to edit them by hand and if you have to rigid of a format then they will be broken.
-Mike
on the other hand, I don't know how much items you're going to use in your game, but XML files use a lot more disk space than the files you wanted to use. If every byte is one, I would go for your first idea!
Quote:Original post by Washu
space certainly tends to be a lot more locale friendly than "," or ".".


It also tends to make parsing easier, for those who have to do it manually in low-level languages (poor you).

Quote:You could also use xml attributes and be even cooler! <rect x='50' y='50' width='200' height='100'/> ftw (and now i know what those 4 random numbers mean too! readability ftw!)!


Or you could use a format that preserves type information, allows attributes to be extensible and doesn't require closing tags to have a 'name' matching the opening tag. :) I use, for lack of a better term, PON, which is like JSON but with Python syntax rather than &#106avascript: <tt>Object(dimensions = Rect(x = 50, y = 50, width = 200, height = 100), color = Color(r = 0.5, g = 0.2, b = 0.9, a = 1.0))</tt>. Of course, if you're not writing Python code (or want to do serious validation), you might not find that terribly useful.
So, if I'm interpreting what you've said so far, your entities take a dictionary of strings and then basically pull out and typeconvert the data, filling in their own fields. I hardly see how that's any different than having an entity parse an XML node representing it. But there's nothing actually stopping you from turning an XML document into a dictionary of things either...
std::vector<Node*> nodes = doc->SelectNodes("/entities/entity");for(std::vector<Node*> ni = nodes.begin(); ni != nodes.end(); ++ni) {    std::vector<Node*> entityNodes = ni->SelectNodes("./*");    for(std::vector<Node*> ni = entityNodes .begin(); ni != entityNodes .end(); ++ni) {        entityData[ni->Name] = ni->Value;    }}

of course, this won't be nearly as clean as ensuring each entity knows how to parse its own node, but it does exactly the same thing as having a dictionary of random strings containing misc. datatypes.

Quote:
Or you could use a format that preserves type information, allows attributes to be extensible and doesn't require closing tags to have a 'name' matching the opening tag. :) I use, for lack of a better term, PON, which is like JSON but with Python syntax rather than &#106avascript: Object(dimensions = Rect(x = 50, y = 50, width = 200, height = 100), color = Color(r = 0.5, g = 0.2, b = 0.9, a = 1.0)). Of course, if you're not writing Python code (or want to do serious validation), you might not find that terribly useful.

You can extend your XML parser to support </> if you want to. It's not hard. Plus if you're going to go that route, why not use a dictionary format [grin] {'Object' : {'dimensions':{'x':50,'y':50,'width':200,'height':200},'Color':{'r':0.5,'g':0.2,'b':0.9,'a':1.0}}}.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement