Describe the project you are working on:
Any project that is built for when and requires interaction between JavaScript and GDScript.
Describe the problem or limitation you are having in your project:
When exporting the game for web the interaction between the game and the website is limited to calls from GDScript to JavaScript. The only way to get information back into the game is via the return value of the JavaScript function.
Describe the feature / enhancement and how it helps to overcome the problem or limitation:
The current workaround is to store the information on the JavaScript side and poll every frame in GDScript if new information is available.
There should be a singleton in JavaScript for calling GDScript functions.
Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
I imagine something like this in JavaScript
let my_args = JSON.stringify({x: 0, y: 0});
GodotEngine.call("my_function", my_args);
If this enhancement will not be used often, can it be worked around with a few lines of script?:
This can not be worked around.
Is there a reason why this should be core and not an add-on in the asset library?:
I am not sure if this is even possible as an asset as it is part of the build process.
I'm a little confused by your proposal's description.
Are you suggesting that calls from Godot code should be allowed in JavaScripts outside the project? Do you want to write a JavaScript, using the exported game as a module for JavaScript functions?
Something like this?
const Godot = require('mygame')
Godot.call("my_gdscript_func", ...)
You can export your Godot project for different platforms. One of those platforms is for the web which will generate a small website which embeds the Godot game. You can modify the generated website to add additional functionality, i.e. you could display a highscore as an HTML element next to your Godot game. To interact with the surrounding website from within the Godot game you can use the JavaScript singleton ( https://docs.godotengine.org/en/stable/classes/class_javascript.html ) in GDScript. One simple example would be to open an alert() in the browser when certain things happen in the game.
But to go the other direction, to feed information from JavaScript back into the embedded Godot game, there is no clean solution. Example use cases would be a HTML button to switch scenes, spawn enemies or trigger a signal in the Godot game.
As stated above, you have store some kind of data which buttons have been pressed on the JavaScript side and then to poll for the available information on the Godot side.
I hope this example makes the usecase clearer?
Another use case: browser Javascript virtual keyboard interacting with a Godot game to work around the fact that the Android keyboard doesn't pop up for Line/TextEdit controls...
Duplicate of #286.
Most helpful comment
You can export your Godot project for different platforms. One of those platforms is for the web which will generate a small website which embeds the Godot game. You can modify the generated website to add additional functionality, i.e. you could display a highscore as an HTML element next to your Godot game. To interact with the surrounding website from within the Godot game you can use the JavaScript singleton ( https://docs.godotengine.org/en/stable/classes/class_javascript.html ) in GDScript. One simple example would be to open an
alert()in the browser when certain things happen in the game.But to go the other direction, to feed information from JavaScript back into the embedded Godot game, there is no clean solution. Example use cases would be a HTML button to switch scenes, spawn enemies or trigger a signal in the Godot game.
As stated above, you have store some kind of data which buttons have been pressed on the JavaScript side and then to poll for the available information on the Godot side.
I hope this example makes the usecase clearer?