Saving and Loading

Hey folks!

In the last couple of weeks I was busy rewriting the whole Inventory/Quest/Diary-System of the game. We will also redesign some parts of the UI, but as for now only the logic-part was remade. Before the change everything worked fine till I started to think about saving and loading content. A colleague of mine gave me some tips and I started to dig in PlayerPrefs. At the beginning the Inventory-System worked like this:

– when the Character walks over an Item the OnTriggerEnter-Method is called
– in this Method I destroy/despawn the Item on the scene and load the prefab into the Inventory-GUI

Well you might think: “Soo~~…where’s the problem?” I’m just picky and want more flexibility. I guess I could manage saving/loading without rewriting the whole thing, but I wanted a cleaner solution.

I started to separate the actual Data from the GameObjects and used the GameObjects in the scene only as pure graphical representations of the Data itself. What I did:

– Each item used in the game IS a little Script which does not derive from MonoBehaviour and is used only as Data
– The Inventory has a MonoBehaviour-Script attached which has an Array where the mentioned items (Data) are added to when
the Character walks over them
– When the Inventory-GUI is open it’s checking which items are currently in the Array and starts to instantiate and display the respective item.

With separating Data from actual Object I have less problems instantiating the Prefabs and running in danger displaying wrong items. The Quest/Diary-System works similar.

Here is a little code-snippet:

public static void Save()
	{
		for(int i = 0; i < ITEM_COUNT; i++)
		{
			Type itemType = m_Items[i].GetType();
			PlayerPrefs.SetString("SLOT_" + i, itemType.FullName);
		}
	}

As for future updates: we have a big surprise, so stay tuned!