Save & Load
Moderator: Coders of Rage
- PixelP
- Chaos Rift Regular
- Posts: 153
- Joined: Tue Oct 07, 2008 12:23 pm
- Programming Language of Choice: c/c++
- Location: sweden
- Contact:
Save & Load
I need help with making a save & load system for my game... I've tried my own way but it's not that clever:
I use variables for checking if events has happend or not. Then, when you save your game lua prints your characters status and the event values to a text file and then loads them in when the game starts again, like a map file. I know there is a better way to do it but I don't know how.
Thanks.
I use variables for checking if events has happend or not. Then, when you save your game lua prints your characters status and the event values to a text file and then loads them in when the game starts again, like a map file. I know there is a better way to do it but I don't know how.
Thanks.
Re: Save & Load
i have mentioned this before, but i'll try and explain again.PixelP wrote:I need help with making a save & load system for my game... I've tried my own way but it's not that clever:
I use variables for checking if events has happend or not. Then, when you save your game lua prints your characters status and the event values to a text file and then loads them in when the game starts again, like a map file. I know there is a better way to do it but I don't know how.
Thanks.
loading and saving highly depend on how you have your game structured. i always have a singleton object that controls the entire gameloop. ie. what level you are on, whats going on in the world. everything. then when i save, i just write the entire object into a binary file. save01.save or something to that tune.
then when i load, i just load that entire object from the file, and alakazam, there you have your game exactly how it was. bullets fling around and everything.
if there is enough people that would like to see an example i will make a generic save function that you can see how it is done.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- trufun202
- Game Developer
- Posts: 1105
- Joined: Sun Sep 21, 2008 12:27 am
- Location: Dallas, TX
- Contact:
Re: Save & Load
Yeah, this is how I plan on handling saving and loading in my game. This is known as Serializing an object. Basically taking something that's in memory and dropping it to disk, then pulling from disk and reallocating it into memory.avansc wrote:i have mentioned this before, but i'll try and explain again.
loading and saving highly depend on how you have your game structured. i always have a singleton object that controls the entire gameloop. ie. what level you are on, whats going on in the world. everything. then when i save, i just write the entire object into a binary file. save01.save or something to that tune.
then when i load, i just load that entire object from the file, and alakazam, there you have your game exactly how it was. bullets fling around and everything.
if there is enough people that would like to see an example i will make a generic save function that you can see how it is done.
I've never done it in C/C++, but I'll tackle it eventually. I use XML serialization in C# all the time. Works well.
Re: Save & Load
here you go. this will solve all your loading nightmares.
ps: i take paypal or flattering comments.
ps: i take paypal or flattering comments.
Code: Select all
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
class level
{
public:
level();
~level();
void showData();
private:
int data[10];
};
level::level()
{
for(int a = 0;a < 10;a++)
{
this->data[a] = a+1;
}
}
void level::showData()
{
for(int a = 0;a < 10;a++)
{
printf("%d\n", this->data[a]);
}
printf("\n");
}
void saveLevel(level *lvl, char *fileName);
level* loadLevel(char *fileName);
void saveLevel(level *lvl, char *fileName)
{
fstream binFile(fileName, ios::out|ios::binary|ios::app);
binFile.write((char*)lvl, sizeof(level));
binFile.close();
}
level* loadLevel(char *fileName)
{
level *loadTest = (level*)malloc(sizeof(level));
fstream binGet(fileName, ios::binary|ios::in);
binGet.read((char*)loadTest,sizeof(level));
binGet.close();
return loadTest;
}
int main(void)
{
level *temp = new level();
temp->showData();
saveLevel(temp, "level_01.lvl");
level *loadedLevel = loadLevel("level_01.lvl");
loadedLevel->showData();
getchar();
return 0;
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Save & Load
i thought id just add this.
this wont work if you have lets say a linked list in the level. you will have to make a smarter level saver and loader for that.
you will have to have a index int the class that know how much data is in the level, then save each individual linked list item., but its not that hard.
EDIT: actually this might work. i'll have to check.
i know it works for a char pointer.
which is comparable to a linked list.
EDIT2: scrub this post, this method of file loading will work fine no matter what you have as member variables. even if they are classes that have classes, that have structures, that are linkedlists. IT WILL WORK.
EDIT3: okay, so this might not work. i havent done this for a while, so im a bit rusty. this will work find as long as you dont have pointers in ANY of the member variables.
im about to try and see what i can do about that.
this wont work if you have lets say a linked list in the level. you will have to make a smarter level saver and loader for that.
you will have to have a index int the class that know how much data is in the level, then save each individual linked list item., but its not that hard.
EDIT: actually this might work. i'll have to check.
i know it works for a char pointer.
which is comparable to a linked list.
EDIT2: scrub this post, this method of file loading will work fine no matter what you have as member variables. even if they are classes that have classes, that have structures, that are linkedlists. IT WILL WORK.
EDIT3: okay, so this might not work. i havent done this for a while, so im a bit rusty. this will work find as long as you dont have pointers in ANY of the member variables.
im about to try and see what i can do about that.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Re: Save & Load
the problem with trying to write pointers to a binary file is that you just cant. the nature of memory is so volatile that its not possible.
if you have node *head, (a pointed pointing to the head node of youe linked list) and you write that to a bin file, you are just writing the address of the head at that time. which obviosly is not gonna work when you load it up. the only time something like that would work is if you saved the pointed to a bin file, then reload the file. the reason why this works is that the memory has not changed. (because your program still owns it). but for loading and saving this does not help us much.
what you would have to do is loop thought each list and save each individual node to the bin, then load it up independantly again.
i would say that if you knew what the max of a list would be.
like lets say you know you will never have more than 1000 objects in a level, instead of making a linked list i would just have object objs[1000].
not that this has some overhead penalties.
lets say your object has:
ints X, Y = 2*4 = 8 bytes;
bitmap = 8 bytes;
usually there would be more
but lets just keep it like this
for simplicity.
thats 16bytes for a object. then
lets say you only have 50 objects in a particular level
thats 50*16 = 800 bytes. so your level would only bee 800 bytes.
WRONG
if you had a linked list this would be true. but
if you did it with a array[1000] you would atuamatically have
1000*16 no matter if you had 1 object or 1000. this thats 16000 bytes.
so be wary of this. its easiers to save it to file this way, but once you start having big levels.
your sizes + load times are going to suffer.
i'll try and make an example of how to save a linked list into a binary file. i think that would help alot of poeple, including me.
if you have node *head, (a pointed pointing to the head node of youe linked list) and you write that to a bin file, you are just writing the address of the head at that time. which obviosly is not gonna work when you load it up. the only time something like that would work is if you saved the pointed to a bin file, then reload the file. the reason why this works is that the memory has not changed. (because your program still owns it). but for loading and saving this does not help us much.
what you would have to do is loop thought each list and save each individual node to the bin, then load it up independantly again.
i would say that if you knew what the max of a list would be.
like lets say you know you will never have more than 1000 objects in a level, instead of making a linked list i would just have object objs[1000].
not that this has some overhead penalties.
lets say your object has:
ints X, Y = 2*4 = 8 bytes;
bitmap = 8 bytes;
usually there would be more
but lets just keep it like this
for simplicity.
thats 16bytes for a object. then
lets say you only have 50 objects in a particular level
thats 50*16 = 800 bytes. so your level would only bee 800 bytes.
WRONG
if you had a linked list this would be true. but
if you did it with a array[1000] you would atuamatically have
1000*16 no matter if you had 1 object or 1000. this thats 16000 bytes.
so be wary of this. its easiers to save it to file this way, but once you start having big levels.
your sizes + load times are going to suffer.
i'll try and make an example of how to save a linked list into a binary file. i think that would help alot of poeple, including me.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
Save and Load Complete
Code: Select all
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
struct node;
class level;
char *newStr(char *str);
void saveLevel(level *lvl, char *fileName);
level *loadLevel(char *fileName);
node *newNode(int data);
struct node
{
int data;
node *next;
};
node *newNode(int data)
{
node *temp = (node*)malloc(sizeof(node));
temp->data = data;
temp->next = NULL;
return temp;
}
char *newStr(char *str)
{
char *temp = (char*)malloc(strlen(str)*sizeof(char));
temp = str;
return temp;
}
class level
{
public:
level();
~level();
void addNode(int data);
void showData();
node *head;
int nodeCount;
};
level::level()
{
this->head = NULL;
this->nodeCount = 0;
}
void level::addNode(int data)
{
node *temp = (node*)malloc(sizeof(node));
temp = this->head;
if(this->head == NULL)
{
this->head = newNode(data);
this->nodeCount++;
}else{
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode(data);
temp = temp->next;
temp->next = NULL;
this->nodeCount++;
}
}
void level::showData()
{
node *temp = (node*)malloc(sizeof(node));
temp = this->head;
printf("Number of nodes = %d\n",this->nodeCount);
while(temp != NULL)
{
printf("node data : %d\n", temp->data);
temp = temp->next;
}
}
void saveLevel(level *lvl, char *fileName)
{
fstream binFile(fileName, ios::out|ios::binary|ios::app);
binFile.write((char*)lvl, sizeof(level));
node *temp = (node*)malloc(sizeof(node));
temp = lvl->head;
for(int a = 0;a < lvl->nodeCount;a++)
{
binFile.write((char*)temp, sizeof(node));
temp = temp->next;
}
binFile.close();
}
level *loadLevel(char *fileName)
{
level *loadTest = (level*)malloc(sizeof(level));
fstream binGet(fileName, ios::binary|ios::in);
binGet.read((char*)loadTest,sizeof(level));
node *temp = (node*)malloc(sizeof(node));
level *returnLevel = new level();
for(int a = 0;a < loadTest->nodeCount;a++)
{
binGet.read((char*)temp, sizeof(node));
returnLevel->addNode(temp->data);
}
binGet.close();
return returnLevel;
}
int main(void)
{
level *temp = new level();
for(int a = 0;a < 5;a++)
{
temp->addNode(a+1);
}
printf("Data of level saved.\n");
temp->showData();
saveLevel(temp, "level_01.lvl");
free(temp);
level *loadedLevel = loadLevel("level_01.lvl");
printf("\nData of level after load.\n");
loadedLevel->showData();
free(loadedLevel);
getchar();
return 0;
}
here is the level file, the first 4 byte is just the level class. the second 4 bytes is the variable nodeCount, note that its 05 (which is correct.) there affter there are 5 units of 8 bytes each, the first 4 bytes is the value of the nodes, as you can see they go from 1 to 5. the second 4 bytes is the address where the next node was. we dont need it. i didnt know how to write just ints to bin file so this solution can be made more efficient, but hey. at least now you can see how linked lists would be written to and loaded from a bin file.
hope this helped. let me know if there are any questions i can answer for you guys.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- PixelP
- Chaos Rift Regular
- Posts: 153
- Joined: Tue Oct 07, 2008 12:23 pm
- Programming Language of Choice: c/c++
- Location: sweden
- Contact:
Re: Save & Load
Man, it looks like you've spent a lot of time doing this. I'm sorry to say but this is to advance for me... I think I'll need to learn more advance c/++ to get a grab of this, I'll save this post and read it through when I'm a more advance programmer. But thanks a lot anyway!
Re: Save & Load
well dont worry about the binary file. just worry about the source and make sure you can adapt it to what you need.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"