Save/load

Hi guys! i would like to make a reward system with ads. example: if you touch the blocked item an advertisement appears and it is unlocked.

The problem is: i would like to save the state of the object if it has been unlocked and i close the game and when i get back in the game, it keeps unlocked, but i do not know how to do this…

When i click:

Unlocked item:

if the game was closed and reopened the object keep unlocked:

Any idea?

Did you check the save-load and level-editor example to see how to do it?
Here is also a basic explanation how to use the storage action to store values:
wiki.compilgames.net/doku.php/gd … -explained

It is pretty straight forward really. In case you are using instances of the same object only displaying a different animation for different items, add an object variable to each instance to identify them. The most simple way is to use a For each object event at the beginning to loop through each instance and

Do = Variable(Counter) to variable ID of object
Do + 1 to value of scene variable Counter

After you have an ID value set for each instance or using different objects, the only thing left is to have an object variable for each object again to store if the item is locked or not.

If Variable Locked of object = 1 the item is Locked
If Variable Locked of object = 0 the item is Unlocked

After you can save this locked value for each object as shown in the save-load and level-editor example and as explained on the wiki.

i don’t know how to save and how to load… I can not understand the example of saving and loading ‘-’

I did a test, but is not working haha

id = 0 the icon is visible
id = 1 the icon is invisible

The variable you read the value in to, must be a Scene variable and need to be created in the Scene Variable Editor. It is to make sure the variable is defined by the time the storage event is trying to use it. In this case, simply using the variable in event is not going to work, the variable must be created in the variable editor.

I believe it should work then.

But, I think you misunderstood the purpose of the ID and Locked variables I mentioned.
You should set the ID variable for each instance at the beginning and then use this value to identify the instance.

So for example you want to save the locked state of the first object, assuming it is ID 1 then you can reference the object in the Group name using the ID value.

“GroupLocked” + ToString(object.Variable(ID))

It is going to save GroupLocked1

So when you want to read the value for the first object with ID 1, you can use the ID value of the object to find the group storing it value.

Read “GroupLocked” + ToString(object.Variable(ID)) from storage “save” value in to locked

So if you look at the examples, this is what is happening, looping through all instances, get the ID, use the ID value to find the group, read the value from the group and use it and then move on to the next instance and do the same.

I’m not sure if I can explain this in an easy way. If we could share Functions already, I could and in-fact I have already created some functions to make this process as easy as pie, but it is not possible to share. You can find the example on my GitHub, I did not released on itch.io yet because I am waiting for the feature to be able to share or copy functions, it is just useless without the option to be able to copy or share, but take a look the events might help to get the idea:
github.com/GameTemplates/GDevel … -functions

Good Morning! I saw the example you sent, and the example of animation caught my attention, so i tried this.
I have a single object that takes two animations:
one is the image and the other is a transparent image, when I click on the object, the image becomes transparent and the object is “unlocked”.

the object is in a group called “Animation”, and i started the scenes variables:

I followed your example:

this is for load:

and this is for save:

Good … Now the state is being saved and loaded but it has a problem, if I click on less than three items to unlock, example: if i clicked in two itens, when I load the game, only one item will be unlocked.
also if I click on only or more than 3 items to unlock, Example: if i have 10 itens, and unlock 3, when I load the game all the 10 items will be unlocked… :frowning:

Maybe you know what is my mistake.

Thank you so much for your help and i’m sorry if i’m taking your time. :slight_smile:

It seems to me the saving is fine except what I recommend to do is assign an ID value to each object at the beginning of the scene instead of when you save them.

At the beginning of the scene Do = 0 to Variable Counter Repeat for each Object object Do = Variable(Counter) to variable id of Object Do +1 to Variable Counter

This way you can reference any object at any time unless you create and delete objects during runtime. But in this case I think you don’t.

Then when you save just do:

Repeat for each Object object Write Object.Animation() to "Animation" + ToString(Object.Variable(id)) of storage

Then when you load, just take the id of the object in to account to reference the object like so.

For each Object object Read "Animation" + ToString(Object.Variable(id)) from storage and store value in loadAnimation Do = Variable(loadAnimation) to the number of current animation of Object

I did not test it but I hope it works :laughing:

If you delete and create objects on the fly then you need to create the ID values when you save as you did, but then you also need to save the objects and load the objects too not only their animations.

I did it, but not work hehehe

Now when i click on an item and load the game all others below are been unlocked as well ‘-’

I think this will never work for me heheh

Created a quick example you can find it here at the bottom of the list:
gametemplates.itch.io/gdevelop-examples

Hope it helps :laughing:
Let me know if you want something to be different but you don’t know how to do it.

Thank you for all your help and patience, I’m very grateful!
It’s working thanks to you!
thank you so much! :blush:

Hi @ddabrahim,

i also am making a similar type of thing…i cant find the example from the link…when i click …it opens the example list and like you have mentioned that it is at the bottom of the list…i only see “pathfinder” example at the bottom.

Can you provide me the zip file of the json file you have made for the lady?

Regards.