Sign in to follow this  
TTK-Bandit

design choice: space or comma ?

Recommended Posts

I'm reading in ascii text files for some definitions, like entity startup values, etc.. it is set up by lots of key/value pairs. like: "name" "armor" now I wonder how I would seperate vector(xyz, color, rect,..) values. would you rather seperate them via spaces: "rect" "50 50 200 100" "color" "0.5 0.2 0.9 1" or via commas: "rect" "50,50,200,100" "color" "0.5,0.2,0.9,1" I tend to use comma, but when its in combination with dots it feels weird( probably because in my language, floating point values are written with comma instead of dots) sometimes I find myself stuck with simple questions as this one :-\

Share this post


Link to post
Share on other sites
space certainly tends to be a lot more locale friendly than "," or ".". 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!)!

Share this post


Link to post
Share on other sites
You could just accept either, so the user can type whatever they feel like.

But wait.. if floating point values are written with commas, then how can you use commas as a separator? How would the parser know whether "1,1" is two ones or 1.1 ?

Share this post


Link to post
Share on other sites
I prefer '|'.

That is just because then I can use the string.Split without really having to worry about hitting a character that might have been used somewhere.

theTroll

Share this post


Link to post
Share on other sites
Quote:
Original post by pinacolada
But wait.. if floating point values are written with commas, then how can you use commas as a separator? How would the parser know whether "1,1" is two ones or 1.1 ?
He's not writing the things in the file in his normal, native notation. He's using the period to denote where the fraction begins, not the comma. With such convention, "1,1" would never be 1.1.

As far as suggesting conventions for your parser... I'm going to second Washu's suggestion on XML. XML is really nice for readability reasons. There are lots of XML parsers you can use in pretty much any language, so writing your parser would be pretty easy too.

Share this post


Link to post
Share on other sites
pinacolada, I was talking about my language, not the parser ;>
the float values are all done using dots.
using both separators is not good, I'd rather have a fixed standard.

Washu, xml is not an option, since I store all key/pair values in strings so they can contain any type.
could probably convert them to a string again while loading, but that's not the coding style I wanna follow.

Share this post


Link to post
Share on other sites
Quote:
Original post by TTK-Bandit

Washu, xml is not an option, since I store all key/pair values in strings so they can contain any type.


Um... XML only uses strings.

Share this post


Link to post
Share on other sites
Quote:
Original post by TTK-Bandit
pinacolada, I was talking about my language, not the parser ;>
the float values are all done using dots.
using both separators is not good, I'd rather have a fixed standard.

Washu, xml is not an option, since I store all key/pair values in strings so they can contain any type.
could probably convert them to a string again while loading, but that's not the coding style I wanna follow.


How does that preclude the use of XML? There's nothing in XML that says that x='10' is x holding an int.

Share this post


Link to post
Share on other sites
imagine a key/value pair set for an entity:
"name" "armor"
"mins" "-15 -15 -15"
"maxs" "15 15 15"
mins would be a key/value pair.
with xml, this would need to be a key/<variable number of values> "pair" for each attribute.
or it would look something like this:
<entity classname="item" name="armor" minsX="-15" minsY="-15" minsZ="-15" maxsX="15" maxsY="15" maxsZ="15"/>
and this is a very small entity definition. as soon as you add more attributes, it'll get more & more messy.

Share this post


Link to post
Share on other sites
Quote:
Original post by TTK-Bandit
imagine a key/value pair set for an entity:
"name" "armor"
"mins" "-15 -15 -15"
"maxs" "15 15 15"
mins would be a key/value pair.
with xml, this would need to be a key/<variable number of values> "pair" for each attribute.
or it would look something like this:
<entity classname="item" name="armor" minsX="-15" minsY="-15" minsZ="-15" maxsX="15" maxsY="15" maxsZ="15"/>
and this is a very small entity definition. as soon as you add more attributes, it'll get more & more messy.


So what you're saying is that I would actually be able to read the notation and understand it without having to guess what "maxs" "15 15 15" meant. Which btw could be redone as something better in XML, such as (with me making a guess as to what "mins" and "maxs" are...):

<entity classname='item' name='Armor'>
<bounds>
<min x='-15' y='-15' z='-15'/>
<max x='15' y='15' z='15'/>
</bounds>
</entity>


Because the only reason to use text at all is for human readability. Computers tend to prefer binary input a lot more than they do textual.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
"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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 javascript: 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.

Share this post


Link to post
Share on other sites
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 javascript: 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}}}.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this