Search Unity

Question How to reset saved variables to initial

Discussion in 'Visual Scripting' started by maxiska2009, Mar 15, 2023.

  1. maxiska2009

    maxiska2009

    Joined:
    Aug 27, 2021
    Posts:
    7
    How do this in bulid?
    I would like to reset this data at the first launch of the game on the player's phone, so that in case of an update, the game starts from the beginning.
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    I guess that depends on 'how' you saved it.
     
  3. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,068
    Saved variables are serialized to PlayerPrefs: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html There are methods like DeleteAll and HasKey/DeleteKey which can find and delete variables by saved variable name.

    I wouldn't use Saved Variables for anything but maybe options settings. They're hard to debug and generally work with for anything more complex.
     
  4. maxiska2009

    maxiska2009

    Joined:
    Aug 27, 2021
    Posts:
    7
    When I call the deleteAll method for PlayerPrefs in VS at the start of my game, nothing happens, the saved variables are not reset to initial
     
  5. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,068
    I'm not entirely sure about the lifecycle of Saved Variables but it might be that the variables are loaded before you delete them from playerprefs since UVS stuff is set up before anything happens in a graph. So while they're technically deleted from PlayerPrefs, they still exist in UVS Variables window which is what your graphs access. You likely can only do this by using a C# script.

    The Manual is very sparse on how Saved Variables actually work: https://docs.unity3d.com/Packages/com.unity.visualscripting@1.8/manual/vs-variables.html

    Alternatively, just set up versioning. If player's saved game version doesn't match current Application.version
    - reset variables with SetVariable nodes and update saved game version to current version.
     
    Last edited: Mar 17, 2023
  6. maxiska2009

    maxiska2009

    Joined:
    Aug 27, 2021
    Posts:
    7
    In general, why am I asking this.

    My game has more than 1000 players in the store. There are regular active users.

    I have prepared an update that affects a lot of components and cannot work with the previous version of the game in any way.

    I want players who have received an automatic or manual update of the game after the release of a new version in the store to reset all their saved variables, only this will solve my problems. Yes, some users will be offended, but this way I can keep the game active for all those who update it. Otherwise, their game will be broken.

    "Alternatively, just set up versioning. If player's saved game version doesn't match current Application.version
    - reset variables with SetVariable nodes and update saved game version to current version."


    Could you tell me in more detail how best to do in this case, how do I understand the saved version of the game from the player and compare it with updates to reset the variables
     
  7. maxiska2009

    maxiska2009

    Joined:
    Aug 27, 2021
    Posts:
    7
    Maybe there is a way to make sure that their game is not updated, namely, installed again...
     
  8. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,068
    In Project Settings/Player you can enter the game version: https://docs.unity3d.com/Manual/class-PlayerSettings.html

    You can define a new Saved variable GameVersion that tracks the game's version and Set it by using Application.version node.

    Then, when you enter the game, you can check if GameVersion saved variable exists by using the IsSavedVariableDefined node, it it doesn't exist reset the game's variables by simply setting all saved variable values to default values you can define with Literal nodes or however else you want. Forget about resetting to initial, make a graph that resets the variables manually via Set Saved Variable nodes. After variables are reset, set the GameVersion saved variable to current Application.version.

    In case GameVersion saved variable is defined, compare saved version with current Application.version, if they're not equal, reset the variables or have some kind of variable migration logic, etc