+1 vote

How do I detect when my html5 exported game is not visible?
Like:
- when the user changes to another tab
- when the user has minimized the browser
- when the user alt+tab to another app

I already tried to print all events in _notification (what). however it appears that no notification is sent in these cases *sigh*

thanks in advance

Godot version v3.2.3
in Engine by (45 points)

1 Answer

+2 votes
Best answer

All those cases can be handled with MainLoop's NOTIFICATION_WM_FOCUS_OUT.

func _notification(what):
    match what:
        MainLoop.NOTIFICATION_WM_FOCUS_OUT:
           #Pause however you want here
        MainLoop.NOTIFICATION_WM_FOCUS_IN:
           #Unpause
by (95 points)
selected by

This works great in the editor, but nothing happens on the HTML5 exported version

my code:

func _notification(what):
    match what:
        MainLoop.NOTIFICATION_WM_FOCUS_OUT:
            if $background.playing:
                print('pausing background')
                $background.stream_paused = true
        MainLoop.NOTIFICATION_WM_FOCUS_IN:
            if play_background and Settings.is_music_on():
                if $background.stream_paused:
                    $background.stream_paused = false
                    print('resuming background')

Well... this sucks. I discovered that this notification is only emitted on the Release version and not in the Debug version of the HTML5 exported version for some obscure reason, which is a pain to test

That's not the case either. Something else is interfering with this notification.
Sometimes this works, sometimes don't.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.