• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
nesseggman

Game save system?

16 posts in this topic

I am working on a project in C++ with Allegro. It is a pet game. It has a "time" system (it is menu-based/turn-based game... menu options cost "time" and after elapsing so much time you go to the next "day"/turn and after so many "days" your pet grows up, gets old, and dies... so you have to use your actions wisely to raise the pet the best you can before it dies). Your pet can enter different kinds of contests like racing, sparring, pageant... Which means the pet has lots of parameters that show what it's good at, as well as lots of other parameters like stress and affection and states like what species it is and if it is injured, sick, etc.

Anyway, I am wanting to create a game save system so that at the end of each "day" turn the game will automatically save your progress and you can quit the game and come back and resume play later. I figure I would have to do this with file i/o or something, but I'm not really sure how to implement it.

First, there are a TON of variables to keep track of. There's the player's entire inventory, all the parameters of the pet, what day it is, money, the player's skills, etc.

Second, I feel like if you create a file and write to it, the player could just open the file and edit it and give themselves lots of money or a very powerful pet.

I tried searching on this forum but "game save" and similar returns pretty much half the topics on the board in the results. Same for search engines just get a bunch of unrelated stuff.

I've never had to create a game save system before so I am kinda lost, especially since it seems like this one will need to keep track of a lot of information.
0

Share this post


Link to post
Share on other sites
With out seeing all of your code ... I would suggest writing all of the data to a class or struct, and than serializing the class or struct to file [url="http://www.functionx.com/cpp/articles/serialization.htm"]http://www.functionx...rialization.htm[/url]

The file output would be very difficult for a normal computer user to "hack" . Edited by Shippou
1

Share this post


Link to post
Share on other sites
I would say this is beyond serialization. Personally I would use a SQLite database. On game load you can do one big data read and on exit one big data write. This would also give you the advantage of organizing the data rather than serializing multiple object.
0

Share this post


Link to post
Share on other sites
I would suggest looking at Protocol Buffers or similar libraries to serialize the data.
1

Share this post


Link to post
Share on other sites
Thanks. I am indeed much of a beginner, though I think I am good at learning new programming concepts (even if I'm not good at figuring them out on my own). I will look into both solutions... Serialization will probably be easier but if I can figure out SQL maybe I will try it.
0

Share this post


Link to post
Share on other sites
I would say this is beyond serialization. Personally I would use a SQLite database. On game load you can do one big data read and on exit one big data write. This would also give you the advantage of organizing the data rather than serializing multiple object.

 
 
But does that really make sense? What do you get from a SQLite database if all you want is to do a big one load and save? You get the atomic write operation, sure, but still, it seems overkill and over-engineering to me. The only place I would use SQLite is in a scenario where I intend to query for the data when I need it (as in with real databases). Probably a good example is a Football Manager type of game.

0

Share this post


Link to post
Share on other sites
I simply gave my perspective on solving the problem. I find that when dealing with multiple data structures it is easier to put that data in a database than it is to serialize and deserialize multiple sets of data. So no I don't think it is overkill.
0

Share this post


Link to post
Share on other sites
I suggest to serialize a class that has all the data, or you can even "serialize" it your self. Just make a write function write in a specific way to a file and read it in the same way, you are overkilling with some of your suggestions. The way you save /load is only important to you, since its your own file format, and will be used only by you. A user can Hack your game in a lot of other way than changing a save file in my opinion. Also, if the the game is single player (Thats what I got from our post), if the user cheats there wouldnt be any bad consequences, he is in a way cheating himself.
0

Share this post


Link to post
Share on other sites

Let me give you an example Save load for a simple Pet Game.

 

Let's assume you have a Pet class.  In this class it has the pet's age (based on days), it's health, and it's happiness.  let's also assume it has an inventory of items which are unique based on some unique int ID.  Let's also add a Save() and Load() function. At it's definition, it may look like this:

 

[code]  #include "TItem.h"   enum THappiness {   SAD_PET,   CONTENT_PET,   HAPPY_PET };   class TPet { private:   int Age;   int Health;   std::string Name;     THappiness Happiness;   std::vector<TItems> Inventory;   public:   TPet(std::string petName);   ~TPet();     void AddItem(TItem item);   std::vector<TItems> GetInventory();   std::string GetName();     int GetHealth();   void Sethealth(int Health);     int GetAge();     void IncreamentAge();     THappiness GetHappiness();   void SetHappiness(THappiness happiness);      void Load(std::string saveFileName);   void Save(std::string saveFileName);   };[/code]

 

I'm not going to worry about the other files, and no go into detail about reading and writing to files (nor loops), but your save and load would do something like this (in Pseudo-code):

[code]void TPet::Save(std::string saveFileName) {   FileType saveFile = FileOpen(saveFileName);     // We know Age Health and happiness will always be the same length, so we can write them in without   // having to write in the length of the data   WriteFile(saveFile, &Age, sizeof(Age));   WriteFile(saveFile, &Health, sizeof(Health));   WriteFile(saveFile, &Happiness, sizeof(Happiness));     // Store the Name, but give it the name length 1st   int length = Name.size();   WriteFile(saveFile, &length, sizeof(length));   // loop through and write each character     // Now write the number of inventory items   length = Inventory.size();   WriteFile(saveFile, &length, sizeof(length));   // now loop through and store each item id, assuming you can access TItem::GetId()     CloseFile(saveFile); }   // In load you do the same thing in the same order[/code]

0

Share this post


Link to post
Share on other sites

Let me give you an example Save load for a simple Pet Game.

 

Let's assume you have a Pet class.  In this class it has the pet's age (based on days), it's health, and it's happiness.  let's also assume it has an inventory of items which are unique based on some unique int ID.  Let's also add a Save() and Load() function. At it's definition, it may look like this:

 

 

 
#include "TItem.h"
 
enum THappiness {
  SAD_PET,
  CONTENT_PET,
  HAPPY_PET
};
 
class TPet {
private:
  int Age;
  int Health;
  std::string Name;
 
  THappiness Happiness;
  std::vector<TItems> Inventory;
 
public:
  TPet(std::string petName);
  ~TPet();
 
  void AddItem(TItem item);
  std::vector<TItems> GetInventory();
  std::string GetName();
 
  int GetHealth();
  void Sethealth(int Health);
 
  int GetAge();  
  void IncreamentAge();
 
  THappiness GetHappiness();
  void SetHappiness(THappiness happiness);
  
  void Load(std::string saveFileName);
  void Save(std::string saveFileName);  
};

 

 

I'm not going to worry about the other files, and no go into detail about reading and writing to files (nor loops), but your save and load would do something like this (in Pseudo-code):

 

void TPet::Save(std::string saveFileName)
{
  FileType saveFile = FileOpen(saveFileName);
 
  // We know Age Health and happiness will always be the same length, so we can write them in without
  // having to write in the length of the data
  WriteFile(saveFile, &Age, sizeof(Age));
  WriteFile(saveFile, &Health, sizeof(Health));
  WriteFile(saveFile, &Happiness, sizeof(Happiness));
 
  // Store the Name, but give it the name length 1st
  int length = Name.size();
  WriteFile(saveFile, &length, sizeof(length));
  // loop through and write each character
 
  // Now write the number of inventory items
  length = Inventory.size();
  WriteFile(saveFile, &length, sizeof(length));
  // now loop through and store each item id, assuming you can access TItem::GetId()
 
  CloseFile(saveFile);
}
 
// In load you do the same thing in the same order

 

I got a related question to this. Can't you just write a whole instance of a object to the binary file, and then load a full object with a load function? Or am I missing something?

0

Share this post


Link to post
Share on other sites

Yes, that you have absolutely no idea how the data is stored internally (there could be padding or not) and even changing compiler settings could override that (which is why some compilers have #pragmas to override this).

 

That example still makes a mistake though as it completely ignores the endianess of the system. Save a file in a little endian system then try to load it in a big endian system... you won't like it (this only matters for portability reasons, but there's not much of an excuse to avoid it). Also you the size of the variable types may change (e.g. a common recent case would be 32-bit vs 64-bit). You'll need to write individual bytes to work around that (then at least you can be guaranteed in which order they'll be stored in the file and with what size).

 

EDIT: oh, also even if none of the above was an issue, what if you change the object for whatever reason, e.g. to add a new variable or something? If you save the object as-is, suddenly old saves won't be usable anymore.

Edited by Sik_the_hedgehog
0

Share this post


Link to post
Share on other sites


Let me give you an example Save load for a simple Pet Game.

 

Let's assume you have a Pet class.  In this class it has the pet's age (based on days), it's health, and it's happiness.  let's also assume it has an inventory of items which are unique based on some unique int ID.  Let's also add a Save() and Load() function. At it's definition, it may look like this:

 

 

 
#include "TItem.h"
 
enum THappiness {
  SAD_PET,
  CONTENT_PET,
  HAPPY_PET
};
 
class TPet {
private:
  int Age;
  int Health;
  std::string Name;
 
  THappiness Happiness;
  std::vector<TItems> Inventory;
 
public:
  TPet(std::string petName);
  ~TPet();
 
  void AddItem(TItem item);
  std::vector<TItems> GetInventory();
  std::string GetName();
 
  int GetHealth();
  void Sethealth(int Health);
 
  int GetAge();  
  void IncreamentAge();
 
  THappiness GetHappiness();
  void SetHappiness(THappiness happiness);
  
  void Load(std::string saveFileName);
  void Save(std::string saveFileName);  
};

 

 

I'm not going to worry about the other files, and no go into detail about reading and writing to files (nor loops), but your save and load would do something like this (in Pseudo-code):

 

void TPet::Save(std::string saveFileName)
{
  FileType saveFile = FileOpen(saveFileName);
 
  // We know Age Health and happiness will always be the same length, so we can write them in without
  // having to write in the length of the data
  WriteFile(saveFile, &Age, sizeof(Age));
  WriteFile(saveFile, &Health, sizeof(Health));
  WriteFile(saveFile, &Happiness, sizeof(Happiness));
 
  // Store the Name, but give it the name length 1st
  int length = Name.size();
  WriteFile(saveFile, &length, sizeof(length));
  // loop through and write each character
 
  // Now write the number of inventory items
  length = Inventory.size();
  WriteFile(saveFile, &length, sizeof(length));
  // now loop through and store each item id, assuming you can access TItem::GetId()
 
  CloseFile(saveFile);
}
 
// In load you do the same thing in the same order

 

I got a related question to this. Can't you just write a whole instance of a object to the binary file, and then load a full object with a load function? Or am I missing something?

 

No, you can't.  In the class, there are instances of std::string, and a Vector Of Items being stored.  What is actually stored for those values in the class instance is only an address. So, you need to explicitly store each of those items (the character's in the string, and the inventory items stores in the vector).

Edited by BeerNutts
0

Share this post


Link to post
Share on other sites
Yes, that you have absolutely no idea how the data is stored internally (there could be padding or not) and even changing compiler settings could override that (which is why some compilers have #pragmas to override this).
 
That example still makes a mistake though as it completely ignores the endianess of the system. Save a file in a little endian system then try to load it in a big endian system... you won't like it (this only matters for portability reasons, but there's not much of an excuse to avoid it). Also you the size of the variable types may change (e.g. a common recent case would be 32-bit vs 64-bit). You'll need to write individual bytes to work around that (then at least you can be guaranteed in which order they'll be stored in the file and with what size).
 
EDIT: oh, also even if none of the above was an issue, what if you change the object for whatever reason, e.g. to add a new variable or something? If you save the object as-is, suddenly old saves won't be usable anymore.
 
This is a ridiculous thing to be concerned about for a beginner's project.
 
This is a "For Beginner's" forum, so explaining endianness to beginner's is like explaining calculus to a Pre-Algebra class; calculus is good to know, but not for Pre-Algebra students.
 
You're assuming #1, the user will make this game for different platforms (and different OS's), and #2, he's planning on supporting saving on one machine and loading on another.  I'll guarantee you he's not.
 
As far as having different version of saved files, that's something, but my example was just that; an example, to give this guy an idea of how to save. Heck, it's obvious not complete, I didn't even fill out the loops.  Leave the advanced details to other forums looking for advanced help.
0

Share this post


Link to post
Share on other sites





Let me give you an example Save load for a simple Pet Game.

Let's assume you have a Pet class. In this class it has the pet's age (based on days), it's health, and it's happiness. let's also assume it has an inventory of items which are unique based on some unique int ID. Let's also add a Save() and Load() function. At it's definition, it may look like this:

#include "TItem.h" enum THappiness {  SAD_PET,  CONTENT_PET,  HAPPY_PET}; class TPet {private:  int Age;  int Health;  std::string Name;   THappiness Happiness;  std::vector<TItems> Inventory; public:  TPet(std::string petName);  ~TPet();   void AddItem(TItem item);  std::vector<TItems> GetInventory();  std::string GetName();   int GetHealth();  void Sethealth(int Health);   int GetAge();    void IncreamentAge();   THappiness GetHappiness();  void SetHappiness(THappiness happiness);    void Load(std::string saveFileName);  void Save(std::string saveFileName);  };

I'm not going to worry about the other files, and no go into detail about reading and writing to files (nor loops), but your save and load would do something like this (in Pseudo-code):
void TPet::Save(std::string saveFileName){  FileType saveFile = FileOpen(saveFileName);   // We know Age Health and happiness will always be the same length, so we can write them in without  // having to write in the length of the data  WriteFile(saveFile, &Age, sizeof(Age));  WriteFile(saveFile, &Health, sizeof(Health));  WriteFile(saveFile, &Happiness, sizeof(Happiness));   // Store the Name, but give it the name length 1st  int length = Name.size();  WriteFile(saveFile, &length, sizeof(length));  // loop through and write each character   // Now write the number of inventory items  length = Inventory.size();  WriteFile(saveFile, &length, sizeof(length));  // now loop through and store each item id, assuming you can access TItem::GetId()   CloseFile(saveFile);} // In load you do the same thing in the same order
I got a related question to this. Can't you just write a whole instance of a object to the binary file, and then load a full object with a load function? Or am I missing something?
No, you can't. In the class, there are instances of std::string, and a Vector Of Items being stored. What is actually stored for those values in the class instance is only an address. So, you need to explicitly store each of those items (the character's in the string, and the inventory items stores in the vector).
Ok, thank you very much for explaining that :)
So I would have to save everything inside a class separate and then load it in the right order later? Edited by Nausea
0

Share this post


Link to post
Share on other sites

[quote name='Nausea' timestamp='1357503230' post='5018290']
Ok, thank you very much for explaining that
So I would have to save everything inside a class separate and then load it in the right order later?
[/quote]

 

If you have items like strings or a list of items, then you will have to manually write and then read that data out.

If you only have native types, like int's, then you can store that inside a struct (or possibly a class), and just write the struct data to a file, and then read it back just as you wrote it, like this:

0

Share this post


Link to post
Share on other sites
Serialization sounds scary but it's actually really easy to do once you give it a try. For every class whose instances will need to be serialized just write a pair of load/dump functions for it that convert to and from 'raw' data. If it has members that aren't raw data then those member object classes also need load/dump functions written, then you just call those from the owner and chain it on to the serialization. By the time you reach the end you can just load/dump the top level object and the whole chain should respond correctly. It's really a lot easier still if you make use of istream and ostream for loading and dumping, since you can just pass the stream down the chain to whatever is getting serialized next. Just remember that for containers you'll want to dump a member count prior to dumping the actual members so that when you go to load the data you know how much data to use for populating the container before moving on to populating the other members.

Seriously, though, it sounds more complex than it is. When you start doing it yourself it should come out sort of naturally. Just start by writing the dump functions and setting everything up for that, then start writing the load functions and adjust the dump functions as needed until everything matches up. If you get stuck then just post showing where you're at and ask for help. smile.png

In terms of security, which was mentioned by OP, if I want to cheat in a game I typically just modify the values in memory while it's running, so I wouldn't worry too much about the savefile format until it becomes an issue in competitive environments or something. Basically the more powerful you make your security the more hackers get off on breaking it. Even giant companies like Sony or Nintendo can't engineer foolproof systems with millions of dollars of research, so....

Serialization is enough to stop people that don't care. Nothing is enough to stop people that do care.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0