+3 votes

Hi guys! im trying to call a funcion that rotates my model every time thats the user clicks on an html button on the page where my godot game canvas is loaded ... Is it possible to access the functions stored in the wasm via javascript ? Somebody can give some examples ? Thanks !

in Engine by (25 points)

I have tried to do something similar but have had no luck. The only form of external communication I could get with an exported Godot game was sending it files via the user:// directory (which in the web export is located in the IndexedDB).

If you were super dedicated to getting something like this working, then you might be able to do some kind of network communication with the Godot game acting as a server to trigger a function call, but that's a pretty wild hack that probably isn't even feasible.

I found two ways of communicating any data between web components and the godot engine:

1. URL parameter polling
You can use the following line of js code in both Godot (as JavScript.eval(...)) and in the actual web environment you are hosting it in to append url parameters:


While this is a quick implementation it is also resource heavy since it is based on polling the url parameters every frame.

2. postMessage with callbacks
You use Godots


to register the callback, and JavaScripts


to send data on both ends.

This is a way better solution because it only requires resources when data is sent / received.

It also works its way around CORS Errors insanely well, so you can inject godot into existing websites as sub components (e.g. present your project live on you portfolio homepage).

Since the original question only has 2 upvotes and is from early 2018 I am not gonna bother gathering it all together right now.

Let me know if you need details / code on how to do it.

1 Answer

+3 votes
Best answer

I've tried to do the same. This might not be quite what you asked for and it's a bit hacky but it might work for you. I am also interested in other solutions btw. Basically Javascript.eval() can return certain datatypes(check docs for full list). Here's how I use this. I set a certain js variable for example when a html button is pressed and then in godot poll that variable during the process step. I then convert what was stored in that variable to a godot InputAction.

This is the part in my gdscripts that sets up the js environment with the necessary things:

    function Input(action){

    function Input(action, processed, data){

    //starting input, doesn't need to be processed, so processed=true
    var input=new Input("default")

    function setInput(newInput){
    var godotInputActions=new Object()

# creates a js object containing strings for all godot actions
# dont try to create inputs with other actions in js
for action in InputMap.get_actions():
    """ % [action,action],true)

Here's the js code for what my html button does(had to add that to godot.html from the webassembly export templates):

document.getElementById('output-button').addEventListener('click', () => {
                setInput(new Input(godotInputActions.ui_button))

This is the function that grabs new input(called during each process step):

func _fetch_js_input():
    var input_action=JavaScript.eval("""input.action""")
    var input_processed=JavaScript.eval("""input.processed""")
    var input_data=JavaScript.eval("""input.data""")

    return {"action": input_action, "processed": input_processed, "data": input_data}

This marks the js variable as processed(called e.g. after fetchjs_input):

func _set_js_input_as_processed():

Then I can do whatever is needed where these inputActions are processed:

func _input(event):
    if (event is InputEventAction and event.action=="ui_button"):
        print("Button pressed");
by (69 points)
selected by

Thanks ! Works like a charm in my project !

Hello! I attempted to repeat the above process you created, but I'm having a small issue. Every time I use the triple quotation marks in my JavaScript.eval() function it's commenting out that content. My JS is multi-line strings, so single quotations won't work (I get an unexpected EOL error). Is there some way around this or am I missing something? Any help would be greatly appreciated!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.