Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to save/load simple game data

Discussion in 'Scripting' started by pehlivan2000, Jan 1, 2018.

  1. pehlivan2000

    pehlivan2000

    Joined:
    May 23, 2015
    Posts:
    20
    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
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 :)
     
    pehlivan2000 likes this.
  3. pehlivan2000

    pehlivan2000

    Joined:
    May 23, 2015
    Posts:
    20
    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!
     
  4. pehlivan2000

    pehlivan2000

    Joined:
    May 23, 2015
    Posts:
    20
    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):
    Code (CSharp):
    1. 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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class levelMenu : MonoBehaviour {
    7.  
    8.     public Sprite unlockedSprite;
    9.  
    10.     public Button level01Btn;
    11.  
    12.     void Start() {
    13.         UnlockLevels ();
    14.     }
    15.  
    16.     void UnlockLevels() {
    17.         int finished01 = PlayerPrefs.GetInt("level01_finished");
    18.  
    19.         if (finished01 == 1) {
    20.             level01Btn.image.overrideSprite = unlockedSprite;
    21.  
    22.             // more Code...
    23.         }
    24.     }
    25. }
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool :)

    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 :)

    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. :)
     
    pehlivan2000 likes this.
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    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.

    https://forum.unity.com/threads/please-help-me-serialize-gameobjects-dont-advertise-assets.409098/
     
    pehlivan2000 likes this.