# [C++] importing map files

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

## Recommended Posts

I am working on a level design tool for a 3d game,and so far I have been able to create shapes and compile them to a new file of my choice. That all works well so now all I need is a function for importing the functions from the file and using them in my game. I made a bunch of custom build functions in my game and thats all thats on the map files:
BuildFlat(0,0,0,150,0,150,1,3,15,15);
BuildWall(150,0,0,150,30,150,1,20,8);
BuildWall(150,0,150,0,30,150,1,20,8);
...
All I need is a simple way of opening this file and reading the functions within them. I was able to get this far, but I don't know what other things I need to get this up and running:
bool ImportMap(char *filename)
{
FILE cur_file;
cur_file = fopen(filename,"rt");

fclose(cur_file);
return true;
}


##### Share on other sites
Well, my first thought is that you could write a little parser for that.
But that's probably because in school they made us write about a billion parsers, so I've got parsers on the brain.

But essentially what you would do is check go through the text file line by line.
For each line you pull in text up to the '(', compare that string with the names of your functions, switch to the code which handles that function, pull out a number, store it, toss the comma, continue until you reach the ')'.
Then call the function in your program with all the stored numbers as your parameters.
Then you move on to the next line, repeat until eof.

I could go into more detail if you like.
But I'm guessing someone else might have an easier solution.

##### Share on other sites
What's the file format, i.e., how does it store the data?

BTW cur_file should be declared as FILE*, not FILE. And if you're using C++, you should probably use std::ifstream instead.

##### Share on other sites
Quote:
 Original post by Gage64What's the file format, i.e., how does it store the data?

I got the impression he was reading a list of these:
Quote:
 BuildFlat(0,0,0,150,0,150,1,3,15,15);BuildWall(150,0,0,150,30,150,1,20,8);BuildWall(150,0,150,0,30,150,1,20,8);...
from a generic/text file.

##### Share on other sites
Yes its now *FILE, forgot to make that change before I posted.

And yes its a generic/text file.

I know I could go through number per number but I designed the file to use these functions for a reason. I mean if I have to and their is no other way I guess thats what I will have to do but I am pretty sure this is a much simpler way (just don't remember the function...)

##### Share on other sites
Quote:
 Original post by snowfellI designed the file to use these functions for a reason.

And what is that reason? (it may sound stupid but it's a leading question)

##### Share on other sites
snowfell,

If you insist on using "build" functions, then there really is no easier way than that proposed by brent_w. However, if you store your "Flat" and "Wall" data as objects, then you can simply serialize and de-serialize them to and from the file. Basically, if you represent a Flat as an object with some state, and a Wall as a different object with some state, then you can serialize each flat and wall to a file. Then reading them back is just a process of de-serializing them. To do this you could either write your own serialization/de-serialization functions for each object you wish to store persistently, or you could use a library such as Boost.Serialize to help you out.

##### Share on other sites
Quote:
 And what is that reason? (it may sound stupid but it's a leading question)

Well my the main reason has to be that I am newer to professional programming. So I decided I would start simple and move from their. For my own use I designed bunch of drawing functions that allow me to easily draw 3d objects (the functions are designed similar to those of Game Maker). But all I have is that my level designing tool does the math and already puts the shapes into the necessary code. All I need is for my program to treat the codes in the file the same as if they were just copied and pasted right where the function is. Is it possible?

##### Share on other sites
So you wish to treat the text you read in as code? Again, the best solution, if you really wish to do things this way ( and not through object persistence/serialization as I mentioned before) is brent_w's suggestion. Actually, languages such as C++ aren't really well suited to that manner of doing things (treating input text as code). Languages such as Python and Ruby are actually great for that type of thing. You can read in text from a file and treat it as a new function definition or a function call, and have it interact with your existing code. I strongly suggest you avoid this function call style way of doing things, as in the future you'll likely want to move away from it anyway.

##### Share on other sites
The lines you have in the text file is source code, and as you can imagine, by the time your program reads these lines from the file the program itself is no longer source code, so there is no way they are gonna "fit right in"

##### Share on other sites
You could embed a Lua interpreter into your application and just "execute" the file, it could come in handy afterward for AI and UI scripting as well, its likely to be less work than say write a parser in lex and yacc.

##### Share on other sites
Quote:
 Original post by KwizatzYou could embed a Lua interpreter into your application and just "execute" the file, it could come in handy afterward for AI and UI scripting as well, its likely to be less work than say write a parser in lex and yacc.
I thought about suggesting that, but it's even more complicated than my original suggestion, heh.

Of course, it could be worth it if he ended up using the scripting functionality for other parts of his game.