(Solved)Save boolean values of instances in storage

Hi everyone.

I have an object named “Star”. I manually place 3 of the Stars around the level.
Once the player is in collision with the star, the boolean value of variable “PickedUp” turns to true.

My question is, how do I save the boolean value of each instance of the Star.
The reason why I need this is, if the player manages to get only 2 out of 3 stars, and decides to try the level sometimes later again, I would like for the Stars that had their boolean “PickedUp” changed to true, not to be shown upon repeating the level.

I apologize, but saving is somewhat difficult for me to understand.

Hi @Emis, and welcome to the GDevelop forums.

A solution is to add a unique identifier on each star for each level. Easiest is to add a number object variable to the star with a value of 1, 2 or 3.

Then use a global structure to store the state of each star on each level upon level completion. Something like

(although it’s using values, the boolean is still intact - true is the same as 1, and false the same as 0)


When you start a level, read the states and assign it to each star. If the star’s state isn’t in the structure, GDevelop will add it automatically and default it to false (or 0, it’s the same thing).

Use events along the lines of:

3 Likes

Hey, thank you very much for the reply.

I understand the solution your provided but I am confused on how to make it work.

I gave the stars their unique ID and gave each of them “PickedUp” variable.

save1

I also made a global structure, but somehow I think I overcomplicated it.

I am also not quite sure how to combine all of this with actual reading and writing from storage itself.

Don’t populate the structure. leave it to get filled in when the game runs. You should only need one global variable:

I have currentLevel as a scene variable in this case. However, you could make it a global one if you like.

1 Like

Check this link to how it’s done.

1 Like

I think it is very close to working. Right now what is happening is that once the star has been picked up, it doesn’t show up if I come back to that level. However if I close the preview and start it, they appear again.

save5

How can I fix this?

From the red error, it looks like you are using write a value to storage. You need to use write a text:

image

1 Like

Thank you very much for answering my problems. I’ve tried many things but I just can’t seem to get to save the actual values if the star has been picked up or not.

This is how I’m trying to save.

This is how I’m trying to read.

Do the write to storage after the repeat event, not during the repeat event. The repeat events populates the structure, and you save it once in a separate event afterwards


Don’t parse jsonPickedUp into the object variable PickedUp. jsonPickeUp is a string representation of the structure that was saved.

Parse jsonPickedUp into the global variable StarState
.

1 Like

Managed to get it working for the first level. I suppose I need to do exactly the same for every other level?

EDIT. Got everything working. Thank you so much MrMen.

You could put the load and save StarState events into an external event called StarStateEvents, as:


And then just link it in each scene you want to save the star states. This saves you having to remember to copy and paste into every game scene. In each game scene, right click on "Add a new event" to link it:

image

and

image