![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Cone |
I am trying to call a function in GDscript from HTML 5 javascript or emit a signal is it possible? and how can i do it
![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Cone |
I am trying to call a function in GDscript from HTML 5 javascript or emit a signal is it possible? and how can i do it
![]() |
Reply From: | jrkb |
This is for Godot 4, but it should work similary for Godot 3.5.
The syntax is just slightly different, check this: JavaScriptObject — Godot Engine (3.5) documentation in English
GDscript (watch out, Godot 4):
var _my_js_callback = JavaScriptBridge.create_callback(myCallback) # This reference must be kept
var externalator = JavaScriptBridge.get_interface("externalator")
func myCallback(args):
print(args)
func _ready():
externalator.addGodotFunction('print',_my_js_callback)
GDscript (Godot 3.5 - untested as I don’t have it on my machine):
var _my_js_callback = JavaScriptBridge.create_callback(self, "myCallback") # This reference must be kept
var externalator = JavaScriptBridge.get_interface("externalator")
func myCallback(args):
print(args)
func _ready():
externalator.addGodotFunction('print',_my_js_callback)
template.html somewhere in js before the engine starts
window.godotFunctions = {};
window.externalator = {
addGodotFunction: (n,f) => {
window.godotFunctions[n] = f;
}
}
and then in javascript (you can test in browser console):
godotFunctions.print('say something');