How do I make a saving system (with global variables and local storage)?

  • Hi, I’m making a prototype base for my future game project, which currently has 457 events. It’s been going decent, until I tried to program a save system with Local storage.

    In it’s testing state the game has four levels, which are accessed through level buttons. Each one has two instance variables, ”Unlocked” (a boolean which defines is the level unlocked) and ”Level”(a number of the level layout the button leads into). In the beginning only one level button is accessible, but every time you beat a level by touching the ExitPortal, depending on the portal’s ”UnlockedLevel” variable (of the next level to be opened), a new value is set on the global variable”LevelUnlockCountGeneral”, which (depending on it’s count) opens the next level, turning it’s ”Unlocked” to positive, while the prievously unlocked levels stay open too of course. Pressing M saves by setting the key ”UnlockedLevels” to ”LevelUnlockCountGeneral”.

    My objective is to be able to save and load the ”LevelUnlockCountGeneral” as well as the instance variables under it’s control, through the main menu with the self explaining ”New”, ”Load” and ”Exit” buttons. Here’s what I did.

    I tested the game and proceeded normally, unlocking the levels. The problem came in, when I saved and returned to main menu to try loading the last state. The level buttons for the unlocked stages weren’t working except for the first, which is open by default. When I played it and touched the ExitPortal, nothing happened as if the global variable wasn’t reacting. I checked debug and reproduced the problem. It turned out that after pressing ”Load”, the box of ”LevelUnlockCountGeneral” wasn’t showing any value, but instead a word ”NaN”

    Why it’s unable to load global variables? How are they usually handled in these situations? Despite the title I'm not restricted to use just Gvariables and Lstorage, any help to achieve the right outcome is welcome. These tools are just what I used first, because I prefer to use methods which I can understand myself.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Looks like you are setting the local storage item to the string/literal text "LevelUnlockCountGeneral". Should be ""&LevelUnlockCountGeneral to use the variable.

  • yeah, I saw that too in event 88.

    NaN stands for Not a Number, and means you are trying to put a text value into a number variable...

    You might want to use an array if you are going to have more levels. Then you can save and load the entire array with one action.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)