Is it possible to pass through parameters from javascript into my game?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Smtih
:warning: Old Version Published before Godot 3 was released.

Hi, I’m trying to make a vocab game that I want to reuse for multiple sets of words. As a test I had the engine read the dictionary of words and definitions from a .json file which worked fine.

For everyday use I want to be able to pass a JSON string from my javascript code to the game such that I can use the game with multiple sets of words easily. Is this possible for a HTML5 build of the game?

:bust_in_silhouette: Reply From: willnationsdev

Yes, you should be able to. If you are referring to over a network with Node.js as a server, then you can use the HttpRequest class to send messages in between. As for locally, I’m not sure if there’s a way to set up a pipe or anything, but you can just as easily write directly to a local dummy file using JavaScript and then read from it with the Godot File class. Can probably also subsequently delete the file if you want with File or OS or something.

At the moment I’m just hosting the files without much access to the hosting server. I can’t imagine a way that would work with files with multiple users that wouldn’t run into weird timing issues.

For a windows build I’m able to use the get_cmdline_args() function in OS. I was hoping there would be an implementation for this in html5

Smtih | 2017-06-28 02:48

:bust_in_silhouette: Reply From: bobojo

You should be able to get send your JavaScript JSON as a string to the game using Godot’s Javascript.eval() command. Using it you can access a global variable from like this:

JavaScript:

const obj = {name: "John", age: 30, city: "New York"};
const myJSON = JSON.stringify(obj);

Godot:

var jsonSTR = JavaScript.eval("myJSON";)