-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a Script -> JavaScript interface to improve calling JavaScript code from Godot #1852
Comments
Just throwing an idea out there: Could promises be represented in Godot-land as resumable function state? If so, users could use The returned value could be some special JavascriptPromiseResult object that contains a |
To specifically address the issue of the promise potentially rejecting before |
@MickeMakaron I had originally thought about having a specialized Instead of var req = axios.call("get", "/user?ID=12345")
var result = await req.as_promise().completed
if result.error:
print("Error: ", result.as_variant())
else:
print(result.as_variant()) What do you think? |
@Faless Sounds good! Would it be possible to allow the user to await the return value without var result = await axios.call("get", "/user?ID=12345").completed Or maybe that's still risky, as you said? I guess that would require something along these lines?
If doing the above on every call comes with issues, maybe it could be done sneakily only when the |
I don't think this is possible.
I fear it might still be risky, but it could actually work, since we'll have to try/catch anyway. |
Will it be possible to call GdScript functions from javascript by registering methods via e.g. |
You don't really need that method, because you can call
JavaScript.get_interface("my handler").call("register_callback", cb) to
register callback that can be called from JS
…On Fri, Nov 20, 2020, 07:26 Mikael Hernvall ***@***.***> wrote:
Will it be possible to call GdScript functions from javascript by
registering methods via e.g. JavaScript.add_interface? I noticed you
mention godotengine/godot-proposals#286 <#286>
but you don't mention gd->js interop.
Sonething I've noticed being asked about numerous times in various Godot
forums is how to call a GDScript function from JavaScript.
Since you're already in the process of making engine.addInterface, would
it be possible to also add a JavaScript.add_interface,
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1852 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAM4C3RTYYM5OAF5OEE7TATSQYDYNANCNFSM4TYX32TQ>
.
|
In C# we have And for callback you should adopt |
There are https://github.com/WebAssembly/reference-types support for WASM to allow get and pass reference from JS around in WASM system. It would make interop with js more natural if we have support for it in C# |
@Thaina: It's the proposal, I don't think it's gone anywhere yet. |
@Zireael07 It was already supported in some browser. At least firefox since 79. I am quite sure it supported in chrome too but not sure since when. And don't know about other. But investigating for such time I think it was solidated to be supported in all browser eventually |
@Thaina reference types are still being worked on, but implementations are slowly catching up. |
Closing, implemented in |
@Faless Are there any sample for C# |
Describe the problem or limitation you are having in your project:
Currently the only way to communicate with JavaScript code from Godot in HTML5 exports is by using
eval
, which, beside performing poorly, is also very limited.Describe the feature / enhancement and how it helps to overcome the problem or limitation:
The idea is to expose an interface in the
JavaScript
singleton that allows to call JS functions and get JS properties in a Godot-y way.Ideally, the API should support almost all possible interactions with JavaScript, this include, getting/setting properties, calling functions, and handling callbacks and promises.
Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
The idea is to expose a new
get_interface
method in theJavaScript
singleton, which will return aJavaScriptObject
.The "interface" will be registered via JavaScript (e.g. the custom HTML include):
You can create your own interface, or expose an external library (e.g. d3):
The interface will then be available to scripting via:
This is the proposed interface for
JavaScriptObject
:Conversion Table
This is the minimal types conversion table between Godot and JavaScript:
Some more specialized types (always copy for safety):
Promises & Callbacks
Passing callbacks to a function, or chaining asynchronous code, is a very common pattern in JavaScript, this is sadly not trivial to integrate with Godot due to scripts and application lifecycle.
Following, is a proposed addition to the aforementioned API that would enable taking advantages of callbacks/promises but requiring a bit more consciousness during its usage.
The idea is to add a method to the
JavaScript
singleton to bind a function reference:And 2 helper methods to the
JavaScriptObject
class:So you can interact with functions that requires callbacks this way:
And potentially, with promises this way:
This approach at promises could prove risky if we end up running the engine outside of the main thread in the future, due to the asynchronous nature of the calls, where a promise may throw an error or be rejected before a
catch
could be called.Due to this scenario, and the possibility that, in any case, a called function might throw an error and break the script, my suggestion is to always wrap the code in try/catch blocks and adding an
error
property toJavaScriptObject
that is!= OK
when the catch has been evaluated. This is still suboptimal, but the best I came up with.(Formalizes) Fixes: #286
(Supersedes) Closes: #1723
The text was updated successfully, but these errors were encountered: