Game save system?

Started by
15 comments, last by Khatharr 11 years, 3 months ago
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.
Advertisement
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 http://www.functionx...rialization.htm

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

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

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.
From the sound of it, I think having a beginner go the SQL route is overkill and serialization is probably the best method. I would think it would be pretty easy to have a format to save off the state of the game in binary.

In simple terms, there are the typical stats that you always must save: day, and pet's Attributes (health, niceness, whatever else the pet can have). Then, for the dynamic items (inventory), you would simply write how many items the pet has, then write the items to disk. When reading back, you first read the amount of items, then read back each item and save in inventory.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

I would suggest looking at Protocol Buffers or similar libraries to serialize the data.
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.
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.

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.
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.

Check out my new blog: Morphexe

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

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement