Morgawr wrote:mm since I'm thinking about making an RPG, I guess it'll be kinda complex...
Yes. Yes it will.
Morgawr wrote:I will have to put a variable for each event in the game and set it to true when it's been triggered already or not... then save all those variables + items + character position + levels + everything else in a file.. right? Hope it works, it looks complex..
Yes. Yes it is.
First off, your serialisation scheme will change a lot during development. I'd strongly recommend that you start your serialisation (of the whole file, and of each significant object in it) with a version number. That will give you the opportunity of loading older save files, if they contain a subset of your current data and if you can fill in the blanks with defaults; at a minimum, you should fail gracefully with a descriptive error. Remember to take into account older software being given a newer save file. It's better to do it and then never need it than to try to kludge it in later once you've released versions of both software and save files into the wild.
I'd recommend making objects responsible for serialising themselves. This breaks the task up into more manageable subtasks, and keeps the responsibility for each part of it better compartmentalised. Note that when you do the load, that each object will be responsible for creating new instances of its children, and then telling them to set themselves up (passing them whatever stream/file you're using).
Notionally,
each object should do this during saving:
- Its own data (e.g. global data for the world, starting with a version number)
The number of children of each type (e.g. 3 dungeons)
Telling each of its children (e.g. each of 3 dungeons) to serialise itself out.
When loading, each object:
- Loads its own data (e.g. global data for the world, starting with a version number and either handing older versions gracefully or raising an error if it can't handle them or if a newer or unknown version number is encountered)
The number of children of each type (e.g. 3 dungeons)
Create that number of instances of the appropriate object type.
Tells each of these new instances (e.g. each of 3 dungeons) to serialise itself in.
Also, I'd recommend put in error checking and descriptive logging right from the start. If you produce a human readable log of everything that's being saved and loaded then it'll save you time in the long run when you introduce a subtle discrepancy between what you're saving and what you're loading.
Morgawr wrote:and regarding the other question, I'm a newbie with the saving/loading files subject, how can I make a binary file with 16x16 ints using IReadFile and IWriteFile?
God knows. I'd urge you not to become reliant on Irrlicht for anything other than rendering. Just use C++ streams or (heresy!) stdio. That way you'll learn skills that are applicable outside of Irrlicht.
Code: Select all
#include <stdio.h>
int matrix[16][16];
FILE * file = fopen("save.bin", "wb"); // "wb" is "write" + "binary"
if(file)
{
// Even for a simple case that you think will never change,
// write out a version in case you do change your mind later
// and need to identify older versions. It's also a good idea to start
// with a magic string to identify the file type. Here, we'll use
// one string for both
const char magicString[] = { 'M', 'A', 'T', '1', '6', 'x', '1', '6' };
if(1 != fwrite(magicString, sizeof(magicString), 1, file) ||
1 != fwrite(matrix, sizeof(matrix), 1, file))
{
// handle error
}
(void)fclose(file);
}
else
{
// handle error
}