godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Add the ability to call GDScript code from JavaScript in HTML5 exports #286

Closed davthedev closed 2 years ago

davthedev commented 4 years ago

Describe the project you are working on: Integrating a technical demo (minimal 2D game) with the AirConsole (airconsole.com) service to see how feasible it is to bridge a Godot game with it. The service already supports Unity and Construct as well as a raw Javascript API. AirConsole is an online, local multiplayer gaming platform where the actual game runs in a web browser and players use their smartphones as gamepads to control the game. Gamepad-to-game communication goes over Internet and is totally managed by the service. A simple system of pairing codes allows players to quickly join without the need to install anything on their phones. The transmission of "key presses" from players' smartphones goes through their system and arrives to the game through a JavaScript API.

Describe the problem or limitation you are having in your project:

I cannot directly call functions or events from the JavaScript world to the Godot HTML5 game. The opposite (Godot to JavaScript function call) is possible by calling the appropriate GDScript singleton.

According to some posts, there is a hacky way to overcome this by polling JS variable changes & reacting on it. But this limits the events possibility to the game loop framerate (60fps). In the AirConsole paradigm, player input arrives through the JavaScript API. It would be better to process it in real time rather than waiting for the next game loop iteration.

Describe how this feature / enhancement will help you overcome this problem or limitation:

I can pass input from the controllers to the game in near-realtime, and not use the hacky polling solution that limits the input framerate to 60fps.

Show a mock up screenshots/video or a flow diagram explaining how your proposal will work:

Principle is simple:

Describe implementation detail for your proposal (in code), if possible:

Exposing an API that a JS can function can call, triggering a GDScript function call.

If this enhancement will not be used often, can it be worked around with a few lines of script?:

There is the polling possibility explained above. But requires overhead and limits input framerate.

Is there a reason why this should be core and not an add-on in the asset library?:

Can such integration of API be done using an add-on? It looks like a core communication principle.

DleanJeans commented 4 years ago

Hey, I also made a AirConsole game with Godot like a year ago. My implementation was also polling + saving/converting return values.

I'll open source when I got some free time in case you run into any problems.

https://github.com/DleanJeans/godot-airconsole

Thaina commented 4 years ago

Is this also include C# mono. Or it would need separated issue. Or are there possible workaround in C# side?

bend-n commented 2 years ago

This exists now, can this be closed?

Faless commented 2 years ago

Yes, thanks!