Detect focus lost on mobile

How do I…

Hi there, i’m making a puzzle game where time is crucial. I would like to auto pause the game (showing a proper pause layer that covers the game) when the game window loses focus. Unfortunately “Window focused” condition works fine only on desktop, i’m testing on iOS and when playing i can switch to another app, which gets the game frozen, and then leverage the task switcher preview to study the next move with the timer frozen as well. Is there any way to overcome this?
I found a similar topic in the forum but unfortunately still not answered.

What is the expected result

I would like to detect if a user on mobile switched to another app, another good solution would be to keep the app running in background but i know there are big implications that i doubt could be overcome by gdevelop.

Thanks a bunch for such a great engine :slight_smile:

2 Likes

I hesitated answering because I really don’t know the solution. But since it’s been awhile with no answer I thought I’d check if you still needed help with this.

There is an extension that uses blur events to detect when the game goes into the background so that it can pause music but I don’t know if it works on iOS or Android. If it does, it’s possible you could study that and use it to trigger a pause screen.

That won’t protect against a player that is so determined to solve the puzzle that they take a screenshot, pause the game and study the next move. Maybe a better solution is to not allow the level timer to be paused, so that if they close the game or send it to the background, when they come back the time ran out so they gained no real advantage to studying the next move. Of course they can cheat their system time to get around that too if they wanted., unless maybe you are getting your timestamps from a server.

Thanks a lot for your reply :slight_smile:
I indeed ended up not allowing any kind of pause at all otherwise, as you said, a player could take a screenshot before each move and cheat.
Since it’s impossible to keep an app running in the background i put together some sort of anticheat routine that constantly checks and store the timestamp of the device, so you were on the right track.
Tested on iOS (both web and compiled) and it works fine :slight_smile:
I’m attaching a screenshot of the snippet in case anyone is interested.

1 Like

Thanks for letting me know you found a solution, now I can sleep at night again.

Just a suggestion. If you don’t already have a difficulty setting, it would be nice to have one. Maybe one that increases the allotted time or no timer at all for casual gameplay. Some people don’t like the pressure of a time limit and others have difficulty making quick decisions. IDK if you have a score board but you could only post the default level scores or list them separately or only save them locally. The same for any rewards, if your game has any. You could decide whether certain modes get any or their value changes based on difficulty.

Thanks for your concern :joy:
Keith_1357, there is a “tutorial” mode with no time limit where a user is taught the game mechanics, of course the score isn’t saved in this case. Time is crucial in my game and pressure plays an important role, after all each game session lasts 2 minutes only :slight_smile:

1 Like