Issue with Life Recharge Timer Not Updating When the App is Closed in GDevelop

Hi everyone,

I’m developing a game in GDevelop 5, and I’m trying to implement a life recharge system that continues to update even when the app is closed. The goal is for the player to regain one life every 30 minutes, up to a maximum of 3 lives.

Currently, I have an external event set up that correctly saves the lives and displays the countdown timer while the app is open. However, the issue I’m facing is that the time doesn’t continue counting down when the app is closed, so lives don’t get replenished until the app is reopened.

Here’s how my external event is configured right now: (insert the image of the external event you’ve shared)

What’s Working:

  • The system saves and retrieves the number of lives correctly.
  • The countdown timer works fine while the app is open.

The Problem:

  • The time doesn’t continue counting down when the app is closed, which means lives don’t recharge as they should.

What I need is for the timer to keep running even when the app is closed, so that when the player reopens the app, lives have been restored based on the time that has passed.

Global Variables Used:

  • Vidas: Stores the player’s number of lives.
  • tiempoParaUnaVida: Defines the time (in seconds) needed to recover a life.
  • horaCierre: Stores the time when the application was closed.
  • tiempoPasado: Calculates the time that has passed since the app was closed.
  • tiempoRestante: Stores the remaining time to recover the next life.

Question: How can I make the timer continue counting down and the lives recover properly while the app is closed?

Any help or suggestions would be greatly appreciated. Thanks in advance!

No puedes hacer que un cronómetro siga corriendo mientras la aplicación está cerrada, ya que por el momento y tal vez por siempre, las aplicaciones hechas con GDevelop no pueden correr “en el fondo”, peeeerro, lo que si puedes hacer, es guardar el timestamp (no se cómo se llama esto en español) pero en es la cantidad de segundos transcurridos en el UTC (un reloj universal que todos los dispositivos tienen, que cuenta los segundos transcurridos desde que inició), al guardar el timestamp en una variable antes de cerrar la aplicación (o cada tantos segundos (esto es mejor, pues previende contra “exploits” o errores de cerrado brusco) y guardar esa variable en el almacenamiento, puedes luego leer ese valor al reabrir el juego y compararlo con timestamp actual, la diferencia, serán los segundos transcurridos con el juego cerrado.

Gracias, es justo lo que estoy probando ahora, pero por mas que pruebo no me salem tocará seguir intentandolo, pero por ahi va la cosa tengo que guardar la hora de cierre UTC y luego comparar la hora de inicio, y restarle el tiempo transcurrido luego actualizar el valor de vidas en base a ese resutado, pero me cuesta la matematica no es lo mio jeje sigo probando, las usar tantas variables me hacen un poco de lio

Hello and welcome. I did a tutorial about this so I will try to summarize it here. It’s pretty simple but people overthink it. I do like using “timestamp” like @Digi-Device_File said, it is in milliseconds in GDevelop.

First, it is not always best to set up this type of event in your beginning of scene events. This is because other things besides the game being closed can cause the game to be paused or sleeping in the background so not keeping track of how much time is passing. When the player returns your game to the foreground, it may not run the beginning of scene events again since the process was not killed, just sleeping. If you build these type of events into the main logic of the game, a game that was sleeping in the background quickly catches up with how much time has passed and regenerates the correct amount of lives or whatever it was supposed to have made.

Second, just don’t approach time in a linear manner because that doesn’t work unless the game is open (and not sleeping). Use a While loop. Here is an example from my tutorial:

In this example from my tutorial, the While loop will trigger if the parent condition (player life is less than total life the player can have) is met. This exact condition is used again in “While these are true” because even though the parent condition will trigger the While loop, it will not be checked to stop the While loop so it must be added there too. So While the player life is less than the maximum life they can have, and the Time (that the life should be refilled) is less than timestamp (current time now), then add 1 to life and change Time (that life should be refilled) to the amount of time it takes to refill a life plus Time (that is when the life should be refilled and when it was last refilled.)

So this works like this: Game is open, the first time the player does an action to use a life, we change variable Time to current time now “timestamp” + amount of time it takes to refill - converted to milliseconds. Say 5 minutes + Time now. In five minutes, games sees variable Time is now less than current time “timestamp” and adds a life, it also sees player has done more actions and current life is still less than maximum life, so it resets variable Time to 5 more minutes from variable Time (last 5 minutes that was set) - not from current time now! This way it plays correctly when game stays open, and when game is opened after being closed. Because after being closed, the calculation will keep adding 5 minutes to the Time it should have been refilled, not from the time now that the game was just opened, and will correctly refill lives based on the amount of time that passed.

1 Like

Wow, thank you so much for taking the time to explain everything so clearly and for showing me an example. I really appreciate it. I’ll start by looking for your tutorial—I think it would be really helpful. I suppose it’s here on the forum, so I’ll search for it and give it a try. Thank you again, I really hope this works because I’ve been struggling with this for three days now, haha.

1 Like

Cool I kind of worried it wasn’t very clear. No my tutorial is on Youtube and I don’t endorse it because I talk so much. There is also a browser playable example and apk download at itch. I never put up the project files because I wanted people to build it and understand it. But here is a link to the project files and I think they will help you more because you can convert them to your native language in GDevelop. The project is older but still runs ok. Change the global variable for regen time to make it shorter than 5 minutes so you can test it easier.

https://drive.google.com/drive/folders/1sFLAtNDdPjc16TKWs7GR_pN7WqxUR4GZ?usp=sharing

You only need the assets folder and game.json.

Wow, great, thanks a lot, it’s just what I was looking for, now I see everything clearly having your example project, it’s simpler than I thought, but without your help it would have probably taken a couple more days, hehe I’ve been dedicating a few hours a day to the project for 4 months and soon if you’re interested I’ll show you how it goes, it’s a platform game, adventure style, the player can get various weapons and objects, I made the inventory system by testing and testing and testing and it’s totally functional, I think it’s turning out well hehe

P/S: I’m new to the forum, I was wondering if there was a thread where people present their projects and receive feedback, well I guess I’ll look for it hehe greetings.

1 Like

It is super simple, to me it looked like you were almost there with your logic. It’s just that no one ever thinks of using a While loop when they first start. I will be very interested to see your game. You can also share it to the Community tab in the forum for feedback.

Thanks, it took me a while to adapt your logic, because of my life system, but now I have it 100% functional. Thank you very much again. I will soon finish the logic of the entire game and I will be able to start putting together levels, but I already have the first one. I will take a screenshot of a couple of minutes and upload it here so you can see the game.

Thanks for the tips, when I have it ready and uploaded to the stores I will post it for everyone, greetings. Do you have any games uploaded by you that I would like to try, thanks.

1 Like