cfg files?

Started by
4 comments, last by Mizipzor 18 years, 9 months ago
Up until now I got my maps data stored in txt's. I know in which order the variable comes so I just do: name = file.getline(); movementcost = file.getline(); And the txt file would look like: grassland 5 But I want to remake it so I in the file just wrote: name = grassland movementcost = 5 And the program stores the values itself. Many games Ive played uses this system for configuration stuff, I searched google for cfg tutorials but couldnt find any. Anyone knows of any good? Or is it so short and easy so that you can tell me here? Thanks in advance, and sorry if you can find it on google, I guess I suck at google searches then... :P
Advertisement
It is really simple (especially when compared to other game development techniques). I don't know which language you are using so I'm keeping this abstract. What you do is read one line of the file into a buffer. Then 'tokenize' this buffer, or split it on the equals-sign, anyway, such that you have a left part and a right part. The left part should be checked to see which property is set. The right part should give the value.

Greetz,

Illco
There was a decent SettingsManager class in Enginuity that dealt with cfg files, you should check it out.
cfg files are just small ini files, I'm using always ini files and it gives you a greater control due to ordering them in logic blocks.
This may not apply now, but it's useful for larger projects. If you're going to be embedding a scripting language in your game/application, then cut out the middle man (i.e. Writing and parsing your own configuration format) and make your config file a script which sets the required variables, calls the required functions, etc. That way, all it takes a simple call to your scripting engine's "execute" function, rather than the extra effort of an entire extra configuration reading/writing module.

However, if you aren't planning on using scripting, XML could be a good choice, provided that you're dealing with a large amount of complex data. The overhead isn't worth it for 5 lines of "key = value" pairs, but for larger sets of data, it is worthwhile, since there exist several highly optimized libraries dedicated to reading and writing it. The ability to modify a document in place makes it possible to write a configuration tool, rather than requiring the user to open the file up in Notepad or whatever (although they could if they so desired, since XML is human readable).
That tokenize thing sounds like the thing I need. Ill try that, thanks! :)

This topic is closed to new replies.

Advertisement