• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

HTML5 Game stops when switching tabs

T

ThePandaSenpai

Guest
I've made myself an idle game to be played on a browser (HTML5) but the game stops when I switch tabs and no progress is made (it's an idle game).

I've played several games like that in the past and as a Player I would just leave the game open in it's own window without any other tab. But now as a developer, I've been requested several times to fix it and I've been incapable of doing so.

Does anybody happen to know how to keep the game running even with switching tabs?

Thanks in advance!
 

MarceloP

Member
This is how it has always been. I don't like this feature, but I've never actually needed for my game to be running in background or something like this. I've also searched a bit trying to find a config or solution for this some years ago. I couldn't find much and I gave up fast, since it was not required by any of my games.

The closest I've come to a solution for this was to use "delta_time" and actually calculate advancements in time based on the delta_time. This means that you'll actually increment X times the normal increment of yours, based on real time, not alarm timers and GMS2 steps. This can be a REAL structural remake in an already complete game, and I don't know if you'd like to go for this path. In any case, if you wish, you can check this post, where I've posted a simplified version of what I use when I utilize delta_time to mark timers.

I really don't know if there's a straight solution for your problem, as would be with a option in preferences "Pause game when not on focus" (although I really wish there was).
 
T

ThePandaSenpai

Guest
I wish I could just go with delta_time but since it's a rpg + idle game there would be a lot of info to be processed when the game regained focus, not just a couple of variables. It would be even worse if somebody left their computer on overnight while not focused. That would mean 8+ hours of catching up for the game to do. So I guess if there's no option or any means of tweaking the .js output file for html5 I will be stuck with this "feature". :/

I even tried debugging the game in chrome and checking the js file to see if I could find what's causing the pause event, but despite the fact I tried it in html5 debug mode, some functions were still obsfuscated. All I could find was YoYo_OSPauseEvent function which triggered some random variable in the js. I will keep digging into this matter perhaps from the javascript approach since it's not doable from within GMS2.

I will let you know if I do find anything!
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
File a bug report with a feature request for the HTML5 target. Much like @MarceloP has suggested...

I really don't know if there's a straight solution for your problem, as would be with a option in preferences "Pause game when not on focus" (although I really wish there was).
 

zbox

Member
GMC Elder
If you override this part of the runner:
1584161666635.png

To always return the "setTimeout" option, your game will run successfully in the background (I have tested it). However you should be aware that the setTimeout method has some drawbacks. I was thinking potentially of writing a switcher to make the game use requestAnimationFrame when the tab was in focus, and then do a less frequent version using setTimeout to keep some collision calcs going when it is blurred. If you use delta time in your game that would 100% work. Unfortunately the runner is closed source and obfuscated though so I'd have to figure out a fairly hack-ey way of reliably implementing this!


EDIT: Put this in your Options > HTML5 > General > Prepend output.js with and don't ask any questions:
Code:
const rAF =  window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame;
const sTO = function(_k61) {
        window.setTimeout(_k61, 1000 / 60)
    };
window.requestAnimationFrame =(a1) => {
return document.visibilityState === 'visible' ? rAF(a1) : sTO(a1);
}
 
Last edited:

keyleart

Member
If you override this part of the runner:
View attachment 29387

To always return the "setTimeout" option, your game will run successfully in the background (I have tested it). However you should be aware that the setTimeout method has some drawbacks. I was thinking potentially of writing a switcher to make the game use requestAnimationFrame when the tab was in focus, and then do a less frequent version using setTimeout to keep some collision calcs going when it is blurred. If you use delta time in your game that would 100% work. Unfortunately the runner is closed source and obfuscated though so I'd have to figure out a fairly hack-ey way of reliably implementing this!


EDIT: Put this in your Options > HTML5 > General > Prepend output.js with and don't ask any questions:
Code:
const rAF =  window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame;
const sTO = function(_k61) {
        window.setTimeout(_k61, 1000 / 60)
    };
window.requestAnimationFrame =(a1) => {
return document.visibilityState === 'visible' ? rAF(a1) : sTO(a1);
}
Only reason i logged in to tell you that you are a god!!! i was looking everywhere for a solution for so much time myself,cause i've beein working on an idle game on HTML5 too.
 

zbox

Member
GMC Elder
Only reason i logged in to tell you that you are a god!!! i was looking everywhere for a solution for so much time myself,cause i've beein working on an idle game on HTML5 too.
Much appreciated - I have actually used it in a few places as well myself since this post lol.
 

RLP

Member
Does this still work in 2.2.5?

I've put it in "Options > HTML5 > General > Prepend output.js" but no change.
I'm trying to let the game not freeze/quit when using "url_open_ext" in a new browser tab ...
Any suggestions?
 

zbox

Member
GMC Elder
They may have changed the runner, I'll have a look into it. You can always just revert to using an older version of the runner for now.
 

RLP

Member
Hmm i guess it's a different issue altogether actually, because apparently url_open_ext doesn't just pause the game for the time being.
When I inspect the code in Chrome it tells me ###game_end###-2 .
Same with clickable events ("clickable_add" and so on) ...
So I probably just wasted your time. I might try an older version of the runner though, as you suggested. Thanks!
 
Top