[Solved] Help with saving the timer value

I am having a strange issue while saving the timer value to storage. I want to save the best time to the storage. i.e. when the time taken to complete a level is lesser than the previously stored value, it saves the best score to the storage.

This is the event I am using:

I tried this also:

None of them stores any value to the storage.

When I switched the logic (which is opposite of what I want), it works and saves the score if the LevelTimer > Stored value.


How can I fix this?

Is it saving the best time as a number or as formatted text? You need to compare a number to a number.

My global variable PlayerData.BestScore.Level1 is a string. If that’s what you mean to ask.

I’m asking is the variable that you’re comparing the timer elapsed to a number of seconds like 120 or formatted text like “02:00”

edit: here’s an example. IDK if the timer is the most accurate way to measure time.
I used 2 buttons to test it. One to start and one to stop. The initial value needs to be ridiculously high or you need to add a condition to check if the value is zero since the timer will never be less than zero.


I believe I am using the events in the same manner:


However, I am not getting the best score recorded. Also, I don’t think that setting the global variable “Best Score” to extremely high score would make any difference. It can be set to any values that can be used to compare the player score and save it if it is lower than the default values.

Am I missing anything in my events/actions that needs correction?

As long as the variables default value is higher than the expected shortest time then it’s fine. Just as long as it’s not zero because nothing is quicker than 0. So, it would never record another score.

The time needs to be saved in the same format as the timer. The timer is in seconds so the best score needs to be in seconds for comparison.

While we know 60 seconds is shorter than 02:00, the condition doesn’t do the conversion. It’s trying to come compare a number to a text. When it can’t it will either do nothing or try to compare it to zero. IDK how it would handle it. Normally, it uses 0 as a default value to prevent crashes.

The time needs to be saved in seconds. If you want to can it into 2 variables or a structure. On e in seconds and once formated. Or you could save it in seconds and then format it when it gets displayed. Either or.

Edit: the 2nd example won’t work bc it’s using the elapsed time as the name of the timer. So, it would be looking for timer 132, 132.1313

If timer timer name is less than the variable that is in seconds then set the variable to the current timer in seconds.

Yes, I am saving the data in the same format (seconds).

This is my global variable for saving the data.

And this is the corresponding event to save the best score:

The timer is showing the correct time in the debugger:


But this value is not getting saved in the global variable. Is there anything wrong with my value conversion/storage?

This sort of works like a stop watch. I needed something to test it. Each “level” of the structure needs to be either set to a large number ahead of time or through actions or it needs to check if it exists first. In Gdevelop even just trying to get a variables value creates the variables if they don’t exist. Which means the best time would be 0.

This would make sure the child exists first. This is more complicated because in Gdevelop just trying to read a variable will create the variable with a default value. For numbers it’s 0.

It would be simpler to set the variable to some huge number either in the setup screen or through an action. Another way would be.

The difference is the child would exist but would be incorrect. It wouldn’t be the true best time. I prefer the 2nd version that checks if the child exists first. If it doesn’t exist then the first time would be the best.

I hope this helps.

Thanks! Now it makes sense to me why having an high initial value is important for the best score.

I changed the global variables:

As well as the events:

However, when the value of “Level Timer” is lesser than the default high value, as confirmed by debugger:

It changes the default value 88888888, but saves only 0 in the global variable.


Another new interesting issue is if I use "Delete timer “LevelTimer” from memory, the timer does not start at all. To prevent this, I am using “Pause timer” rather than “Delete Timer”. But there’s no difference in the outcome.

Are those last 3 events sub events of something? I’m assuming that would only be needed at the end of the level. Otherwise, it would immediately set the score to zero on the 1st frame and save zero.

You don’t need to pause or delete the timer until after the whatever is being timed is over. Otherwise, there is no timer.

Ok… So adding this condition fixed the issue at the beginning of the scene fixed the issue.

Thank you so much @Keith_1357

1 Like