How do I call a function or emit a signal from javascript to GDScript

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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

:bust_in_silhouette: 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');