Help with expressions please

It’s gonna be difficult to explain i hope you will understand. The goal is to save the player progress and to be able to load it in his next session…

I would like to change the value of a global variable child.
The structure variable is called “nom”.
But i would like that the child variable name = an object variable. The object is called “door” and the object variable is “name=indian”.
So it would be :
Make the value of global variable
“nom. Indian” =1
I tried :Nom.door.VariableString(name)=1 but it won’t work…

And the next thing is how to save all these child variables and how to load them to the right door. Because i have multiple instances of the door in a group “doors” and they all have a different “name=X” object variable.

I hope you can helo because I’m stuck… :cry:

For the global variable access you’ve the dynamic syntax:

Do = 1 to the global variable Nom[door.VariableString(name)]

Note the [], what is inside can be any dynamic string:

Not sure what do you want to save/load, but:
1 - To save lot of children from a structure variable there’s a string expression: ToJSON, this convert a structure variable into a string, easy to save (there are variants for global and object variables). Then, when you load the saved game, you’ve an action to convert a JSON string into a structure :smiley:

2 - To save multiple instances check out the level editor example, you need a for-each loop and dynamic group string in function of an instance unique identifier (in your case the variable “name”) in the save values action.

Thanks to answer but i think something is weird… Is it possible to change the value of a variable calling the name of an object variable in gdevelop 4? Because i tried your expression and it doesn’t work… So i tried something more simple :
Make the variable:
-door. VariableString(name)=1 - >doesn’t work
-ToString(door. VariableString(name)) =1

  • doesn’t work
    -ToString(door.Variable(name))=1 - >doesn’t work
    Nothing is working and i have a biiiiiiig headache…

What events are you using?, you’ve to use the actions to change a variable value, and pass to it the variable name and the value, in GD it should read:

Do = 25 to the variable MyVar

To change a child from a structure the variable name is the child path, for example you have the structure variable “MyVar” and it has a child “MyChild”, to modify the child value:

Do = 25 to the variable MyVar.MyChild

But the child path is fixed, if you want the child path to be dynamic use [] instead the dot to separate the child name:

Do = 25 to the variable MyVar["MyChild"]

Now, if you have another variable “ChildTextVar” with the text “Child” inside, you can use it too:

Do = 25 to the variable MyVar["My" + VariableString(ChildTextVar)]

Finally, you have an object Door with a variable Name, a structure variable Nom, and in this structure you want to modify the child called the same way than the Name value of Door:

Do = 25 to the variable Nom[Door.VariableString(Name)]

Hope it helps :slight_smile:

1 Like

Finally :exclamation: i succeed with a structure and the last expression you gave me :smiley:o = 25 to the variable Nom[Door.VariableString(Name) many manyThanks!
But i am not able to do change the value of a “simple” scene variable and i think i will need it sooner or later.
I tried with a scene Variable called “indian=0” and an object “door” with the object Variable Name=indian
My expression is :smiley:o=25
to the Variable Door. VariableString(name)
And it won’t work. Is it the right expression?

The problem is that you can’t use an expression for a variable name unless it’s a child and you’re using [].
To use it you’ll need a structure, add a “root” variable to the scene and add your “indian” variable as a child, then you’ll be able to use the dynamic access with [] :


“The problem is that you can’t use an expression for a variable name”
Aaaargggghhhh noise of a heart attack
I spend hours to figure out how to do that and… It’s not possible lol.

Anyway. I managed to finish my save/load system with your help (yay and thanks again) and now… My game preview is a mess because of all the save/load variables i did. So… Where is the save file of the preview? I would like to edit it to start the game from scratch again…
I have a file called “save” in my project directory wich is the name i asked in the write/read events but it’s blank…

Web games use browser storage, so the file is “inside” the browser you used to launch the previews :slight_smile:
Launch a preview and press Ctrl+Shift+i to open the developers console, and search for the local storage, in Firefox you can find it around here after enabling it: [url]], but previews use Chromium and the location is a bit different :confused: