Godot Version
4.4.1
Question
I’m trying to figure out how to call an asynchronous method on my html page’s window
object using JavaScriptBridge.
I’ve added this javascript code to the webpage that was generated when I created a new web export target for my project:
<script type="application/javascript">
window.loadOutcome = async function() {
return new Promise(function(myResolve, myReject) {
myResolve({
outcome: 2
winText: "2"
})
});
}
</script>
I’m trying to call this from Godot with this code:
var _load_outcome_callback_js = JavaScriptBridge.create_callback(_load_outcome_callback)
func _state_entering():
var outcome:Dictionary
if OS.has_feature("web"):
print("web")
var window:JavaScriptObject = JavaScriptBridge.get_interface("window")
print("window ", window)
var promise = window.loadOutcome()
print("promise ", promise)
promise.then(_load_outcome_callback_js)
func _load_outcome_callback(args):
print("callback done")
window.loadOutcome()
appears to never complete because ‘promise’ is never printed to console and _load_outcome_callback(args)
isn’t called either. I’m not sure if calling JavaScriptBridge.get_interface("window")
is right because I’m not sure if window
is a javascript object that can be returned. In any case, calling loadOutcome()
on it does not seem to do anything.
Am I doing this the right way?