How do I save a objects variable and read the value?

Hi… Im kinda new to Gdevelop, but I have watched and read up about how to get started and the basics.

I just want to know how to save and then load the players Score.
This “score” is a object variable.

I have read up about the Storage part of Gdevelop but it just wont work for me.
I know that Write a value only works for values and write a text only works for texts but I cant seem to get Gdevelop to save the player score and then load it. It returns the player name fine, but the score keeps being returned as 0.

This is the idea of how I want things to look.
When the player dies, it goes to a scene that asks for the player to type their name and press a “enter” button which then hides that layer and displays another layer that shows the player name along with their score.
Any help would be appreciated.

Hi, do you want the name and score to stay after the game is closed? That’s when you need to use Storage actions, but otherwise they should just be Global Variables.

You seem to be pretty close already, if your Name returns correctly, and assumedly Score also appears in gameplay. I think in your display event, you need to write something like

"Your name: " + GlobalVariableString(playerName) + " Your score : " GlobalVariableString(playerScore)

There could be other reasons, such as confusion between global and scene/object variables, so if your problem persists I’d appreciate a screenshot of your events of the score variable and how it’s displayed - use print screen or Windows’ Snipping Tool.

(Also, kudos for probably even reading the wiki!)

Hi…
Yes I want the Score and name to stay after the game is closed. The name returns correctly but the score returns “0”.

Here is the screenshot of the event:

I have tried everything from Write and read as value and write and read as text and so on…

Really struggeling. I just want to create a event that asks for the player name after they die and then save the score along with that name and then be able to view it again next time I play the game.

That loading event looks good. Can you check if scene variable Score is updated inside debugger or ingame? (Debugger is the bug icon, click the second option). I assume the problem is because the highscore is not in storage - wrong name while saving/loading?
Can you also show the saving score event?

You can also use:
ToNumber(Variable(Score)) in case Variable score is string.
On display Highscore, try "Highscore: " + GlobalVariableString(Highscore)

Hi…

Thanks. Will try out what you suggested and then report back😉

Hi…
Sorry

I dont really understand what you mean. Im still technically a noob at Gdevelop😂 I did open the debugger and played that scene but from there… Im lost😂

The score is a object variable. The player is a tank and I made the tank a global object and then used the tanks object variable (score) as the players score.

Don’t worry, we all have to start somewhere :grinning::ok_hand:
Firstly we want to check the object variable, so we’ll have to go through the object name first.
I’ll give example from my own project, to find a variable “fileLoaded” for an object called LoadedFileText.
image
Before doing anything, press Refresh. This will scan the current objects in the scene, and variables. LoadedFileText is (0) because I have not made the game load savefile yet.
image
After doing that in the game window, I press Refresh again and select from objects on the left, and check their Instance variable, which is displayed at the right. For object #7 only fileLoaded is 1, that’s the object variable in this example.

Since your score is attached to tank, you can do it in the same way. Check if score variable is available and not 0.

By the way, do you delete your tank object when the player loses? That might mean your score variable is also deleted. I’d recommend scene variable or just global variable instead.

Okay thanks😁 I’ll try the global variable instead of the object variable.

Just quick question. Should a sprite be placed for the object variable to be loaded or not?

Nope - not at all! In fact, global variable will stay even if you change scene, and only dissapears if you close game. But you might know that already