ECMAScript
ECMAScript copied to clipboard
Accessing the global scope
Hello again! I've been trying to figure out a way around this problem I've run into. QuickJS creates its own scope within Godot, but I need to access the javascript, the window variable, or the document variable included with the HTML document from a web build and I don't think that's possible.
Alternatively I could also import the js file into Godot and run it, but this js file attaches its methods to the window, and since window is not defined, there's no way to access them.
Has anyone else run into this problem?
I haven't tried it, but does https://docs.godotengine.org/en/stable/getting_started/workflow/export/exporting_for_web.html#calling-javascript-from-script work for accessing window in an html build? There's a bunch of issues with trying to use that eval function for anything but a toy example though (how to get return values, etc.).
The issue here is that when godot is compiled for the web w/ this ECMAScript module, the browser is effectively running an opaque binary for the godot game. QuickJS is its own interpreter inside of godot and not related to the Javascript VM in the browser so there's no DOM, window, or document available. According to the docs, Godot itself only seems to provide that Javascript.eval function in order to interface with the browser's JS VM.
If you're having an issue with a 3rd party dependency from npm that requires window, document, etc. you'll probably have a bunch of issues running it since QuickJS isn't running in the browser context at all.
I haven't tried it, but does https://docs.godotengine.org/en/stable/getting_started/workflow/export/exporting_for_web.html#calling-javascript-from-script work for accessing window in an html build? There's a bunch of issues with trying to use that eval function for anything but a toy example though (how to get return values, etc.)
It does work yes, I found that shortly after posting this, but unfortunately I do need access between the 2 and that only gives 1 way access unless you do polling which is a pretty awful way of handling things. There's a Godot.call() function exposed to the Javascript console, but there's no documentation on it so I can't tell whether it's what I'm looking for, and searching around has only pointed to the JavaScript.eval().
Unfortunately I don't think either this project or Godot can handle it. Importing the JS file directly causes the editor to crash, so it basically has to be floating around in the global context, and accessing Godot functions from there appears to be impossible.
According to this you might be able to get serialized (string) data back from Javascript.eval, which would at least allow you to do some bidirectional communication, but it's going to be messy for anything that needs event handling.
It might be possible to make a better abstraction by updating godot's C++ code or adding some functionality to this module, but it would require someone more familiar with godot's internals.