+2 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 (24 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.

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:

JavaScript.eval("""
    function Input(action){
        this.action=action
        this.processed=false
        this.data=""
    }

    function Input(action, processed, data){
        this.action=action
        this.processed=processed
        this.data=data
    }

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

    function setInput(newInput){
        input=newInput
    }
    var godotInputActions=new Object()
""",true)

# 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():
    JavaScript.eval("""
        godotInputActions.%s="%s"
    """ % [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():
    JavaScript.eval("""input.processed=true""",true)

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 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 webmaster@godotengine.org with your username.