How do I save instance variables?

0 favourites
  • 4 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hi, I’m in a need of help. After a year long break I’ve continued my project’s development. The progress has been good, until I tried to save some instance variables for the state of collectables.

    I first tried it the same way as I previously had done with global variables (successfully), as shown in the pic below. The instance variable I’m trying to save is BmaskCollectedNum, the variable for the collectable mask’s state, which turns from zero to one when it’s collected. This solution did not work, the masks changed state would not remain when closing and reopening the game.

    Next I tried using dictionary, because it seemed a common variable saving method, based on what I read from other threads about variable saving. I’m completely new to dictionaries, so I tried to understand them by studying the code of a metroidvania game template I bought from the Scirra asset store years ago, which heavily relies on dictionaries. The result is shown below and it still didn’t work. I guess I don’t understand dictionaries enough to know how to properly use them with instance variables.

    Any help is appreciated.

    And by the way, thank you very much for helping me know how to save global variables to local storage years ago. Sorry I forgot to reply to the last post. Saving global variables is no longer a problem, now I’d like to know how to do the same with instance variables. I actually tried to start this as a reply to my previous thread from two years ago, but the site didn't let me, so I made a new thread instead.

  • If you collect the item then that one instance of the object is set to 1 and you store it as 1, but the other instances presumably remain as 0. Then when you load the game and get that item from the local storage you now set all instances to 1. Not understanding that last event. Even if this did work are you really going to repeat those events for each collectable in the game? Also I think you should not be saving instance variables to local storage, probably why you found no information on this.

    If you're doing collectables I would advise using an array where you can list all collectables in Y=0 and then have yes/no or 0/1 in Y=1. Then you can save the full array as JSON format into local storage then you only have to save/load one item. On starting the game you load the local storage item into the array and it's filled with what you have collected, 1s and 0s.

    Or if arrays sound like it's not your thing then please revert back to global variables, that's all you need to store to say that you collected an item. When you overlap the object, you set a GV to 1, not on the object itself.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thank you for the reply!

    I know my code might not have made sense, but I wasn't really sure of any of my solutions. It was experimenting based on what I knew and understood and in the first attempt's case, I just kind of thought "For each" would individually treat each mask instance, to set each ones correct instance variable, but good to know that's not the right way.

    I can't rely in global variables alone in my case. Collecting masks does add up to a certain global variable, but I also need to have some variables that affect the masks themselves, to be able to change their states, because these are special collectables. Unlike more generic things like coins, with the masks, if the player has already collected one instance, they should not be able to collect it again (until they start a new game of course). I want each to disappear or change frame on collecting and stay that way on a loaded game.

    I will try the array next, despite being new to that feature too. I'll do research for it and combine my new knowledge with your advices.

  • Ok, I made an array system where the changing array values co operate with instance UIDs to determine each masks instance variable.

    However I'm doing something wrong when I'm saving the array or loading it, maybe both, I'm not sure, but either way the array values don't load. I was prepared for that arrays have to be dealt with differently than global variables when it comes to saving or loading. Here's how I tried to save and load the array. Can you help with this?

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