The event ‘window focus’ is causing too much delay.
Is there a way to improve this?
The profiler in the debugger records a total of 20%.
The event ‘window focus’ is causing too much delay.
Is there a way to improve this?
The profiler in the debugger records a total of 20%.
Switch the 2 conditions around - “The window is focused
” and “The vriables Pause_AltTab is true
”. GDevelop processes the conditions of an event from top down. A boolean check will be quicker to perform than window focus check. If the boolean check is the first condition is false, then any following conditions are ignored and the next event gets actioned.
Thank you for your reply!
Unfortunately, it did not yield meaningful results.
It still consumes a significant portion of the performance, taking up more than 90% of the event’s performance, totaling 17%.
I plan to solve this issue using trigger once and a timer.
What do you think?
Trigger once was going to be my next suggestion, on both events.
When you check the timer, there’s no need for the trigger once. The condition changes because of the event’s actions.
My thought was to put a trigger once in (what is now) the 4th and 5th events, where you check the window focus, and do away with the timer.
I tried removing the trigger Once on the timer, but it still doesn’t work smoothly.
Although I tried the method you suggested(4th&5th), there was no significant improvement in performance.
Then maybe look into adding a bit of javascript, and add an event listener for when the window loses focus (if that exists).
Set a boolean variable in the javascript and check that boolean in GDevelop events (with a trigger once on it).
If you are going to do MrMen suggestion, there is a Pause When Focus Lost extension that uses javascript to listen for blur and focus window events. Right now the extension only pauses the game and mutes the music on lost focus, resumes game and channel volume on focus event. But I tested it to see if it could be changed to only pause/resume music on a channel, and it was simple to change it, even for me and I don’t know about any of that stuff.
I just went to
and followed the links in the continueMusicOnChannel and pauseMusicOnChannel, copied the const music = etc and pasted that in the variables at top of the extension and changed channel to 0, copied the 2 functions and pasted them in the focus and blur events, commented the stuff I didn’t want out and it seemed to work fine. It ran in the debugger at .24 ms so it seems performant, probably will be more so if you know what you’re doing.
Edit: Oh and in events I called the extension by an unconditioned “Pause when game lost focus” that came with the extension.
Thank you all! With the help of both of you(@MrMen @Lucky-j ),
I was able to resolve this performance issue!
This is my first time using JavaScript, and honestly, I have no idea how it works. Haha.
I used an extension and modified its contents.
The code modifications were made using ChatGPT.
Here are the screenshots and the modified code.
After a few tests, if there are no issues, I will add [Solved] to the title.
const continueSoundOnChannel = function (runtimeScene, channel) {
if (!runtimeScene || !runtimeScene.getScene) {
console.error('runtimeScene is not defined or invalid.');
return;
}
const sound = runtimeScene
.getScene()
.getSoundManager()
.getSoundOnChannel(channel);
if (sound) {
if (!sound.playing()) sound.play();
} else {
console.error(`Cannot continue playing non-existing sound on channel ${channel}.`);
}
};
const pauseSoundOnChannel = function (runtimeScene, channel) {
if (!runtimeScene || !runtimeScene.getScene) {
console.error('runtimeScene is not defined or invalid.');
return;
}
const sound = runtimeScene
.getScene()
.getSoundManager()
.getSoundOnChannel(channel);
if (sound) {
if (sound.playing()) sound.pause();
} else {
console.error(`Cannot pause non-existing sound on channel ${channel}.`);
}
};
if (runtimeScene && runtimeScene.getGame) {
var sound_manager = runtimeScene.getGame().getSoundManager();
var volumen = sound_manager.getGlobalVolume();
var channel = 0;
window.addEventListener('focus', function (event) {
if (sound_manager && volumen !== undefined) {
sound_manager.setGlobalVolume(volumen);
runtimeScene.getGame().pause(false);
continueSoundOnChannel(runtimeScene, channel);
} else {
console.error('Sound manager or volume is not defined.');
}
});
window.addEventListener('blur', function (event) {
if (sound_manager) {
sound_manager.setGlobalVolume(0);
runtimeScene.getGame().pause(true);
pauseSoundOnChannel(runtimeScene, channel);
} else {
console.error('Sound manager is not defined.');
}
});
} else {
console.error('runtimeScene or runtimeScene.getGame is not defined.');
}
That is amazing! It looks way more advanced than my attempt:
//var sound_manager = runtimeScene.getGame().getSoundManager();
//var volumen = sound_manager.getGlobalVolume();
const music = runtimeScene.getScene().getSoundManager().getMusicOnChannel(0);
window.addEventListener('focus', function (event) {
//sound_manager.setGlobalVolume(volumen);
//runtimeScene.getGame().pause(false);
if (music) {
if (!music.playing()) music.play();
} else {
logger.error(
`Cannot stop a non-existing music on channel ${channel}.`
);
}
});
window.addEventListener('blur', function (event) {
//sound_manager.setGlobalVolume(0);
//runtimeScene.getGame().pause(true);
if (music) music.pause();
else {
logger.error(
`Cannot pause a non-existing music on channel ${channel}.`
);
}
});
Also I forgot you were using sounds so I tested it with music…
I always forget about ChatGPT, your example is really inspiring to see what else it can come up with!
Thank you! Lucky J !
I’ve tested a few things, but occasionally the sound volume becomes very loud.
Although I’m not sure under what conditions this happens, the issue primarily occurs after leaving the window unfocused for a long time and then refocusing on it.
I see something about volume in the chatGPT one, probably because the original extension messes with the volume. I can’t tell what it’s doing but it’s possible it’s turning it up somehow. I took it out and made this:
var channel = 0;
const sound = runtimeScene.getScene().getSoundManager().getSoundOnChannel(channel);
window.addEventListener('focus', function (event) {
if (sound) {
if (!sound.playing()) sound.play();
} else {
logger.error(
`Cannot continue playing non-existing sound on channel ${channel}.`
);
}
});
window.addEventListener('blur', function (event) {
if (sound) sound.pause();
else {
logger.error(
`There is no sound to pause on channel ${channel}.`
);
}
});
I am testing it now. I just left it in the background like 5 mins and no sign of volume increase. I’m leaving it for longer now.
I took out the pause game though, it would have to be added again if you want your game to pause. I find mine usually does pause when it’s unfocused but that’s on Android and the preview, I’m not sure if you built it for desktop.
It looks like the issue has been resolved!
I also added the part you provided and tested it, but unfortunately, the same issue occurred. I still don’t know the exact cause. It might be an issue with the event I created.
So, I went ahead and solved it this way.
It has been really helpful.
Yours is my favorite, it’s so compact! I’m going to steal it, tyvm…
Exactly as I suggested earlier . Thank you for sharing your solution - it’ll help others if they come across the same problem.