# Where to save enemy positions in C++?

I'm wondering how and where to save the positions my enemies should spawn.

My idea was to use a struct such as:

struct enemy{
int x,y,type;
}


And save an array of it in a .dat with fwrite() but I'm not sure if it's a good idea.

I want to know how it's commonly done. Also I recently asked a question about events and was answered something about scripting languages like Lua, is this related to the enemy positions too? Could I create a file in a scripting language with them?

• This question could use a bit of work. Honestly, the mention of C++/SDL isn't very relevant. It seems you are more interested in storing data in files. Therefore, you need to understand the differences between storing data in a binary file, versus parsing a plain text file (or tagged data format). Scripting language stuff isn't relevant for this question either, BUUUUUUUUUUUUUT storing entity information in binary formats as you have indicated as an idea here, is a very common approach. – Evan Oct 7 '13 at 19:57
• my gripe with using a binary format is that it won't be visible in a text editor, so I have to build a little program to edit the enemy positions. I thought about using something like XML or a database (or even a scripting language) but I have never used any so I'm a bit reluctant of adding more complexity – Lisandro Vaccaro Oct 7 '13 at 20:38
• I'm not sure what makes you think a scripting language is what you want here. Scripting languages are usually intended to implement game behavior at a higher level, which drastically reduces iteration time, and decouples behavior from core systems. A tagged markup language like JSON or XML is a suitable choice. I don't see how you expect to generate enemies and positions without some sort of tool, or painful hours of writing them out by hand. – Evan Oct 7 '13 at 20:55
• Also, the fact that you have never used them before doesn't fly :P. This is the reality of game development, you have to solve problems you have never encountered before, using technologies you may have never even heard of before, and in ways that you have never tried before. This is part of what makes game development so hard to get in to, you need to be a problem solver, not a code monkey :) – Evan Oct 7 '13 at 20:57
• yeah, to this point I have used the implementation that I considered the best in every case but I'm on a tight schedule to deliver this as a college project so I'm starting to cut corners =@ – Lisandro Vaccaro Oct 7 '13 at 20:59

I'm not sure if it's a good idea.

This sounds like a perfectly good idea for a start. Apparently you plan on dumping directly your struct in binary data files: as Evan said, this is a commonly used approach. It's compact and efficient, but it has quite a few pitfalls:

• It's hard for a human being to read them.
• It's hard to find differences between versions of the same data file.
• It works for POD-types but will get you into trouble for anything more object-oriented.
• For multi-platform games, due to the variety of computer architectures out there (e.g. endianness), C++ offers you no warranty that data files will be compatible between platforms.
• And probably more.

Advanced game engines usually use binary data files only for the final game for performance reason, but use a more "human-friendly" text format for development. It's perfectly feasible to support both at the same time and switch between the two when required (do some research about serialization if you want to know more, e.g. see this question on GDSE).

Also I recently made a question about events and was answered something about scripting languages like LUA, is this related to the enemy positions too? Could I create a file in a scripting language with them?

Yes. You can use a scripting language to define this type of data. I've seen a few engines doing that and it works quite well. Another approach is to use a static, data-only text format (.ini, XML, JSON, something custom...) and write or reuse a parser for it.

• as I told Evan my gripe with using a binary format is that it won't be visible in a text editor, so I have to build a little program to edit the enemy positions. I'm also very intimidated by the scripting languages, I couldn't even found a good tutorial for implementing LUA – Lisandro Vaccaro Oct 7 '13 at 20:39
• The problem is there's no "standard" way of doing this, especially in C++. You should probably use good old text files and parse them yourself to figure out how this is done, and maybe move on to a more advanced ready-made parser when you need it. Check fscanf and C++ streams for details on how to parse text files "by hand". – Laurent Couvidou Oct 7 '13 at 20:57