Save and load

At the moment i’m writing code for save and load functionality for an RPG game. I have not started yet, any of you wonderful programmers please guide me threw ?

Thank You

If you don’t want to reinvent the wheel, use something like Easy Save 2. Even if you plan to reinvent the wheel, it’s a good idea to study how others have previously built them.

It really depends on what you need to save, when, and where.

When:

  • If you save at checkpoints, create triggers and invoke a save method in OnTriggerEnter().
  • If you save at timed intervals, use a coroutine and invoke the method after yield WaitForSeconds().
  • If you save using a menu, a lot of the work is in the UI.

Where:
You need to decide if you’re saving to the cloud (e.g., a MySQL database on a server), PlayerPrefs (a good choice for mobile), or local files. In any case, XML is a good choice. Generally you can gather your save data into one big object and serialize it to an XML string. You don’t need to save every detail of the game, just the data that need to be updated when you reload.

What:
It’s easiest if the entire game state is in the current scene. I don’t mean that your game only has one level. You can have one or more manager singletons that persist across level changes. These singletons can hold data such as faction/reputation values, quest statuses, etc. One of these singletons should keep track of the player’s current level. When you load a game, this singleton can load the appropriate level.

You can put a special script on every GameObject that needs to save and load its state. It can return an object or string containing the minimum amount of data necessary to restore its state when loading a game. For example, a wandering villager might just return its current position in the level. A monster might also return its current HP and Mana. A faction singleton might return a list of all faction values. You don’t want to serialize entire objects or the entire scene; that’s too much data.

When you save a game, find all GameObjects in the scene that have this script and invoke a method to get the save data. Likewise, when you load a game, invoke a method to restore state from the saved data.

2 Likes

Thank you very much for your reply Tony :slight_smile: . it gives me a idea to start with and will get back if I’m struck with something .

My final implementation would look like this

Anytime save during the presence in the world map
save point save and load during the presence in village and in dungeon
and a checkpoint if the player is dead during the dungeon raid .

my approach would be to enable Save and load method in UI menu on world map.
trigger enabled save and load method on village and dungeon ?

One “mistake” that people like to make when first saving to PlayerPrefs is to make a new key for EVERY SINGLE entry. An example of this:

Say your player is capable of having 5 stats… For the sake of it, let’s say they are Intelligence, Stamina, Defense, Agility, and Spell Power, and they are all assigned a value… Let’s say: 15, 25, 10, 20, and 15 respectively. You could write something like this:

PlayerPrefs.SetKey("Intelligence", PlayerStats.Intelligence);

PlayerPrefs.SetKey("Stamina", PlayerStats.Stamina);

PlayerPrefs.SetKey("Defense", PlayerStats.Defense);

//etc...

The ultimate problem with this, is that if you go into your Registry Editor (where all playerprefs are saved), you will see that the registry key is actually named after the stat and the value of the key is the actual int you assigned it. This is not good because it is VERY simple to modify values this way.

So, you could keep your playerprefs neat and organized by using string formatting. You could literally save EVERYTHING you need to save in one PlayerPrefs key and separate them out nicely, like so:

//Save
PlayerPrefs.SetKey("PlayerStats", String.Format("{0}.{1}.{2}.{3}.{4}", PlayerStats.Intelligence, PlayerStats.Stamina, PlayerStats.Defense, PlayerStats.Agility, PlayerStats.SpellPower);

From there, you can Split(“.”) the PlayerStats string into a char[ ] and then parse the chars back into ints and assign them to your players stats when they load the game back up. This doesn’t make it impossible for the players to hack their characters, but it provides at least an extra layer of protection, because they don’t actually know which stat they are modifying.

Actually, Tony from the above posts has an asset that does all of this neatly and nicely for you in Lua. It has saved me a TON of time and headache trying to save and load data as my game is huge and saves a bunch of data for different NPC’s as well. I HIGHLY recommend getting his system… It just helps a ton!

1 Like

Thanks a lot Epictickle, What is the asset name of Tony’s please ?

Hi codezeero, it’s the Dialogue System for Unity. You can read more about it on the forum thread.

1 Like

Thank you
TonyLi :smile:

I suggest using binary formatter and reading this guide:
http://unitygems.com/saving-data-1-remember-me/

This is what I’m using in my RPG game.

1 Like

The binary formatter in that Unity Gem would work fine, too. There are really three steps to saving a game:

  • Gathering the data that needs to be saved.
  • Putting the data in a saveable format.
  • Writing it to persistent storage (e.g., PlayerPrefs, local file, SQL database).

The Dialogue System handles step #1 by providing components that you add to objects to automatically save position, active/inactive state, etc. It includes general-purpose save components as well as purpose-built components for Dialogue System data. It also includes sub-packages to save UFPS, Realistic FPS, and other frameworks’ data. Step #1 is usually the most complicated step, and the most dependent on your specific game. It’s fairly simple to then put the data in a saveable format and write it to storage. The Dialogue System handles #2 by compiling all of the data into a big string. It does not handle step #3, except for a basic implementation that writes to PlayerPrefs. It’s up to you to write it out to whatever storage is appropriate for your project.

Easy Save 2 and similar products handle all three steps and in fact work well in conjunction with the Dialogue System to save Dialogue System data to any type of persistent storage. It doesn’t have purpose-built save components for any specific frameworks, since it’s a general-purpose tool.

The Unity Gem suggested by raycosantana briefly covers #1 and #3, but its focus is on #2 (putting the data in a binary format). But, on the plus side, it’s free. :slight_smile:

1 Like

Thank you
raycosantana

the link is very much helpful in learning system . :slight_smile:

1 Like

Thank You
TonyLi

for enlightening me :slight_smile: