Hi all,
my question is related to the HTML5 target. When switching from one tab to another in the same browser, or reducing the browser, timers and processes are stopped. Is there a way to receive a signal when the tab gains the focus ?
Hi all,
my question is related to the HTML5 target. When switching from one tab to another in the same browser, or reducing the browser, timers and processes are stopped. Is there a way to receive a signal when the tab gains the focus ?
Hmm, that's an interesting question.
I haven't looked into this but maybe you might be able to pause the game via the engine class in your games HTML shell page: https://docs.godotengine.org/en/stable/tutorials/platform/customizing_html5_shell.html#doc-customizing-html5-shell
https://www.codingwithjesse.com/blog/detect-browser-window-focus/
Perhaps @Calinou might be able to offer a more clear answer, or perhaps this is something being considered/worked on for a future version if the above suggestion doesn't pan out. I don't do anything with godot targeting HTML5 myself so I'm not really sure.
I'm not sure if there is such a signal available. The only reference to NOTIFICATION_WM_FOCUS_IN
in the HTML5 platform code is here, and it's unrelated: https://github.com/godotengine/godot/blob/c42e9bd5890e90f0d0ae1690cb445dc2597d9470/platform/javascript/os_javascript.cpp#L779
Hello, thanks a lot for the help. Regarding that, I tried things in the version of Godot 3.4 that allows to access to Javascript objects directly. But nothing works. By analogy to the JS code:
<script>
document.addEventListener("visibilitychange", myFunction);
function myFunction() {
console.log(document.hidden);
}
</script>
I tried in Godot, in my main script:
extends Node
var callback = JavaScript.create_callback(self, "_on_focus_in")
func _on_focus_in():
print("FOCUSSSSSS IN")
func _ready():
JavaScript.get_interface("document").addEventListener("visibilitychange", callback)
Do you have any clue if we can use addEventListener in Godot thru a JavascriptObject ?
Many thanks.
For the record, this discussion was cross-posted on GitHub.
In Javascript, all event listener callbacks have an event parameter, as seen here:
If you add a parameter to your callback function, it should work.
OMG ! I couldn't figure out this possibility. Thanks you so much for the help, you saved me !!! So, the solution is:
func _on_focus_in(event):
print("FOCUSSSSSS IN")
And how to put this topic has solved ?
I changed it to a Q&A and marked it as solved for you. Also, this is very good to know. Thanks.