Archived

This topic is now archived and is closed to further replies.

reading in a level

This topic is 5504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve been working on this all day, and i finally decided to ask for guidance. suppose i have a level file in text format: MY LEVEL Level Numero Uno #level name #begin NPC list NPC 1 Brian NPC 2 Danny END #end player list # begin something else i''m using ifstream. I''ve opened the file and i can use getline and cmpstr (which i dont really understand except for it returns 0 if they are the same), but other than that, i really need a primer on parsing data like this. i''ve scoured the internet for a tutorial or some example code, but im not sure i knew what to look for. BTW, two features i need are ignoring whitespace (except for spaces delimiting between fields) and comments with a certain character that tells parsing to go to the next line....any help would be appreciated. Brian

Share this post


Link to post
Share on other sites
um.., first you should know how your level text file is structured,

then you read them the way you structured them so in your example say,

Level Numero Uno #
// you''d be using some string comparign functions to compare those three strings before you go on and load the # to indicate the level
NPC 1 Brian
NPC 2 Danny
// again, you should compare the NPC string, then read an int or unsigned int or whatever that number signifies, then probably the name you will store in a buffer or something,
END #
// this is probably nothing but just to tell your game that it shuold stop loading the npc lists has ended?

etc.,

if you''re using the ifstream class, everything can be loaded with ifstream::read(...), use ifstream::good(), to check if file is opened successfuly,

as for the things you''d like to be ignored, declare a std::string object with the characters that you''d like to filter, then just compare the current character(on the file) with this and ignore,

hope i gave you some basic ideas,

Share this post


Link to post
Share on other sites
Windows has an interesting set of commands to find the right information in an .ini file: GetProfileString, GetProfileInt, GetProfileSection. The ini file structure is as follow:

  
; this is a comment
[Section 1]
Key_1 = Data
Key_2 = Data

[Section 2]
Key_1 = Data
Key_2 = Data

You can use it as a start to structure your initialization file and to simplify your code. Still, be aware it is windows dependant (it is available on windows 95, 98 and NT).

For my own initialization file parsing, my architecture is as follow:
Token Structure: this token is a format into which all data is translated. This is a common internal format to separate file parsing format from initialization function parameter format.
Loader Object: holds the file open / close method plus the Section research function, the Read Section function and the Set Parsing Strategy function.
Parser Object: this is an abstract object holding all the parsing strategies from the file to the token

When initializating a game from a file:
1- I open the file with the loader object
2- I set the section I wish to parse and I set the parsing method.
3- the read fucntion returns a pointer to a linked list of tokens
(The read function will check each line until it encounters the next Section or the EOF. For each line that is not a comment or not empty, the read function will call the parsing strategy to translate the line into a token or a number of tokens. These tokens are added to the linked list.)
4- the game objects are initialized with the linked list of tokens.
5- the linked list of tokens is deleted.

Hope that helps.
Ghostly yours,
Red.

Share this post


Link to post
Share on other sites