Scripting Languages?

xISOx 101 Oct 20, 2005 at 20:11

I am pretty new to C++, and programming in general.

I have been searching around for a while now, but I have not been able to find anything about how to create save game files for a game.

I am doing a little text based game as a project for one of my classes at school, but I want to be able to save things such as experience, level, name, so on and so forth.

So, I was looking at python, but that seems like it’s a completely new language, I just want to save some variables in an external file(files?).

Anyone have any enlightening information?

6 Replies

Please log in or register to post a reply.

bignobody 101 Oct 20, 2005 at 20:47

Hi. You really don’t need a scripting language to save a file. There are many ways to do this that are native to C/C++ ( fopen, fwrite, fprintf, etc.). If you are working exclusively with Windows, you can also use the win32 file i/o function calls (CreateFile, WriteFile, etc.).

It’s not too difficult. Just read your data back in the same order it was written ;)

Good luck!

NomadRock 101 Oct 20, 2005 at 21:18

If you are using C++ you are probably familiar with iosteam, you know like with cout and cin.

There is a file equivalient in fstream.


std::ofstream out("output.txt");
out << << " " << player.level << std::endl;

std::ifstream in("input.txt");
in >> >> player.level;

Simple no?

SamuraiCrow 101 Oct 20, 2005 at 23:05

If you’re looking to do a lot of string manipulations (and since you said you are doing a text adventure you will be) stay away from arrays of characters. They were the standard in C but are slow and error prone. Use C++ for simple strings or Python for advanced strings. Python, being a scripting language, has superior string handling capabilities.

It is difficult to embed Python in C++ unless you use the Boost library for doing so. Python is an entirely different (and usually much shorter and simpler) language to use for most purposes. C++ generates faster code, however, and is more widely used. For a text adventure I’d recommend Python as a first choice in a language if you haven’t started already but if you’ve already started in C++ then stick to what you know.

To learn how to program in Python (if you’re interested) go to and download the appropriate version of “How to Think Like a Computer Scientist: Programming in Python”. There is a C++ version of “How to Think Like a Computer Scientist” there also. In the long run I’d strongly recommend using both Python as a primary language and C++ for writing extensions to Python if you plan on being a game designer.

eddie 101 Oct 25, 2005 at 23:58

If you’re really ardent about embedding a language in C++, look at Lua.

Python is great and all, but Lua’s lighter, faster, and scales better (Python has a global interpreter lock that means you can’t thread the application that embeds it and expect to scale).

Look at luabind if you want to set things up easily.

That said, if you’re new to C++, I woudln’t recommend that first. :) Try Python on it’s own: it’s very easy to use, very C/C++ like, and they even have a project called PyGame that can help you out. ;)

xISOx 101 Oct 26, 2005 at 02:33

The thing is, I’ve worked on various MUD’s before, and done some other program manipluation, I’ve never just been able to just sit and type out what I want and have it work.

I don’t want to change languages because the whole purpose of this project is to help me learn the C++ language better, not achieve the actual end result.

I also don’t want to have something similar sitting right there, because I would be prone to just steal ideas from that instead of coming up with my own solutions and such.

More on-topic:

With fstream, does it create a new line for each variable you throw in there?
Like the output file would be something neat like this… = John
player.level = 7


Also, do you guys know anywhere where I could find how to split up the program so I have files for player functions (save, attack), and other ones for handling other parts of the game?

Can you use a variable instead of “output.txt” ??

EDIT: After playing around with the fstream for a little bit, I am thinking that the input is just interpreted by the program as in int or char or whatever type it is, then defined, and each sequencial one, seperated by a space, is a different variable. Is this right?

Ahh, how would I input a string, all i’m getting is the first letter!

Just so you know what’s going on…

using namespace std;

int main()
int variable;
char charlie[7];    
ifstream in("output.txt");
in >> variable >> charlie[7];
cout << "If this works, I own.\n" << variable << "\n" << charlie[7] << endl;
return 0;

my output.txt looks like (7 charlie)

What I get is…

If this works, I own.
eddie 101 Oct 28, 2005 at 17:04

If you are looking at doing writing of save files or some such, I would heartily recommend a glance at boost::serialization (

If you’re not familiar with boost, I’ve just given you the C++ equivalent of a silver bullet. You’ll save yourself a tonne of time by re-using their components.

Basically the serializer will let you dump a class and load it up again, without having to worry about crazy parsing routines and the like.

I can’t vouch for it’s speed, and I leave it as an exercise to the reader to implement it in non-speed critical areas (load time, outside of loops, etc).

If you need something “faster”, more under your control, [whatever-your-rationale-is-here], there are various ways of doing something similar …

Now, I’m a staunch advocate of not doing things yourself that others may have done better, so I ardently recommend you try something like boost::serialization first, as it will save you having to build an architecture for this, and you can always fix speed issues later (and easily, if you encapsulate well), when you actually realize them.

But, if you need a faster load trick, you can do direct class writing (take a pointer to your object, and fwrite out sizeof(MyClass) bytes to file in the simplest case), but this will require an architecture to support this (you’ll have to handle member data that is pointers), and if you’re smart, various unit tests to make sure things actually work like you want. :)

Anyways, that’s probably information overload, but I hope it helps! :)