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

Storing the user's level.

Discussion in 'Scripting' started by UmutSAKICI, Jan 12, 2021.

  1. UmutSAKICI

    UmutSAKICI

    Joined:
    Apr 1, 2020
    Posts:
    16
    Hello guys, first thank you for browsing my thread.
    I have a question. This question may sound ridiculous.
    Because some complex. (for me)

    Think of a game and there are levels in the game. There are also questions at these levels.
    For example, at level 1 there are 3 questions.
    At level 1, we answered our second question incorrectly. The player then quits the game, but I want the player to save the question he was in and answer that question when he comes back.
    I would like to record which level and which question lasted.
    It's like recording in registration.
    This is a question and answer game.

    What I want from you is to explain the logic of this. Because I did not understand anything. At least it could be a video.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Huh? You expect someone to make you a video?

    All you need to do is save what level and question they were on and then reload the data. Depending on your needs, it could be as simple as a player pref, or an actual save and load system, or saving online. But for simple, just make two playerprefs and have one be a level and one be the question. Then update the level playerpref when they go to a level and as they answer questions, record the last question they were on.
     
  3. UmutSAKICI

    UmutSAKICI

    Joined:
    Apr 1, 2020
    Posts:
    16
    Shall I check which level falls on which question? So it will be intertwined? I got it, the variables will trigger each other if I guess. :)
     
  4. UmutSAKICI

    UmutSAKICI

    Joined:
    Apr 1, 2020
    Posts:
    16
    In addition: I do not want a video to be prepared, if there is a video on this subject, I just wanted it.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    If ... if only!

    Screen Shot 2021-01-12 at 2.41.05 PM.png

    Also, some random thoughts about Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    Don't use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide
     
    UmutSAKICI and NOT_A_ROBOT1101 like this.
  6. NOT_A_ROBOT1101

    NOT_A_ROBOT1101

    Joined:
    Jun 18, 2020
    Posts:
    39
    PlayerPrefs should be enough, though if you don't want casual users going around and editing their save files, BinaryFormatter should be good enough, though slightly more experienced users can break its security.

    Brackeys' Save and Load video teaches you how to use the BinaryFormatter, but the PlayerPrefs method is simpler.

    Saving just the question number:

    Code (CSharp):
    1. PlayerPrefs.SetInt("QuestionNumber", /* Question number */);
    2.  
    3. // Optional because it is automatically called when the application closes
    4. PlayerPrefs.Save();
    Loading the question number:
    Code (CSharp):
    1. int questionNumber = PlayerPrefs.GetInt("QuestionNumber", /* Value if nothing has been saved, recommended: 0 */);
     
    Last edited: Jan 14, 2021
  7. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    One point about BinaryFormatter that is not often mentioned is how fragile it is. If you so much as change which interfaces your serialized class implements any serialized data will become incompatible. Not robust whatsoever.
     
    UmutSAKICI likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Adding to that, here's an example of simple persistent loading/saving values using PlayerPrefs:

    https://pastebin.com/icJSq5zC

    Useful for a relatively small number of simple values.
     
    NOT_A_ROBOT1101 and UmutSAKICI like this.
  9. UmutSAKICI

    UmutSAKICI

    Joined:
    Apr 1, 2020
    Posts:
    16
    Thanks for everything.

    I'll test it soon and get back to you.