I'm creating a web game using Godot.
For close the game, i tried to use `get_tree().quit()`.
If I use it on the IDE, it works. When i tried it on my server (after exported the project) it doesn't work.
I'm sure that Exporting setting are okay.
How can I close the game?
And, how can I add an hypertext link (similar to html `` tag)?
Thanks for your answer and sorry for my bad English
Exit the game
On the web, using
get_tree().quit()
Should work. That is, it should stop the runtime. The game will not continue running. It does not close the browser tab. In fact, browsers have restriction on scripts closing tabs.
Note: Make sure you are using Godot 3.2.3 or newer (see #39604). I tried it, it works.
Making a link
You can a LinkButton
, which is a button that looks like an hyperlink. And you want to connect its "pressed"
signal to a script where you use OS.shell_open
, for example:
OS.shell_open("https://example.com")
Note: This result in a new tab in web exports. On the desktop it opens the default browser.
Navigating the browser
Since you ask about closing the game, and about making a link, I'll venture to guess that what you actually want is to navigate (leaving the game and going to another page).
In a web export, you can access the JavaScript runtime of the browser to do this.
In Godot 3 use JavaScript.eval
:
JavaScript.eval("window.location.href='https://example.com'")
In Godot 4 use JavaScriptBridge.eval
instead:
JavaScriptBridge.eval("window.location.href='https://example.com'")
Detecting Web Build
You can use OS.get_name
to identify the platform.
For example, you can do this:
Godot 3
if OS.get_name() == "HTML5":
JavaScript.eval("window.location.href='https://example.com'")
else:
OS.shell_open("https://example.com")
Godot 4
if OS.get_name() == "Web":
JavaScriptBridge.eval("window.location.href='https://example.com'")
else:
OS.shell_open("https://example.com")
Which will navigate the browser if this is a web build, but if it isn't, it will try to open the default browser.
© 2022 - 2024 — McMap. All rights reserved.