How to save/load simple game data

i m making a 2d platformer and after the player finishes one level, one bool for each level should be saved and then loaded next session. like if the player completed level1, a bool called level1_done = true should be saved and then loaded next session. the same thing for level2, level3 etc. i already read a bit but i didnt really understood the whole serialization thing. the data should be saved in a XML file

Can the user play levels out of order? If not, I’d suggest that you can save the “highest level” as an int and that will cover everything you need.

The easiest solution, in my opinion, for small data to be saved is PlayerPrefs (API provided in/with Unity). Check that out.

Not sure if you’re saying you read it should be saved in XML or that’s what you want…

If you want some other options/ideas for saving data, please say so. If you’re only interested in XML, I don’t know that off the top of my head, sorry :slight_smile:

1 Like

it s like a simple platformer game with a level menu where you can only select the levels you already finished. if you finished a level, the level gets unlocked in the level menu. (so yes, the player can basically play levels out of order)

most people said that playerpref is not a good way to store data and that XML is the easiest one? but yeah, i ll look into playerprefs. thanks!

wow, it s really easy. it works like a charme. thanks a lot, here s my code if anyone else needs it:

i used this simple code in my exit script(if the Player completes the Level):

PlayerPrefs.SetInt("level01_finished", 1);

and in the game menu i check if the player completed the Level. I also give the button a new unlocked sprite:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class levelMenu : MonoBehaviour {

    public Sprite unlockedSprite;

    public Button level01Btn;

    void Start() {
        UnlockLevels ();
    }

    void UnlockLevels() {
        int finished01 = PlayerPrefs.GetInt("level01_finished");

        if (finished01 == 1) {
            level01Btn.image.overrideSprite = unlockedSprite;

            // more Code...
        }
    }
}

Cool :slight_smile:

As I was saying, for very simple stuff PlayerPrefs is wonderful.
If you need JSON at some point, that is good. BinaryFormatter is great, too, imo.

It depends on the situation. I’m glad you got it working :slight_smile:

note: if you create a variable like “levelInt” on the button, you could fill that in per instance, and use a string or int.ToString() on the GetInt (thereby not needing to hard code levels).* If you hadn’t already thought of that, of course. :slight_smile:

1 Like

If you want to learn in detail about saving and loading check out this thread that I made where people posted up a bunch of code and explanations on the subject. It really helped me out and all the code is up to date so you won’t have to screw around too much trying to get everything working.

I know you’re only doing something very simple right now, but if you ever want to do something like save your player position and so on in the future this is what you’ll need to learn.

1 Like