Hello there,
I'm trying to set a global variable in construct 3 using Javascript (in a script.js file)
The code looks like this:
async function get() { let url = "https://domain.com/myjsonfile" let obj = await (await fetch(url)).json(); console.log(obj); runtime.globalVars.Variable1= obj; } var tags = get();
This code log the json in the console, however, when I try to set the Variable1 it gives me the following error in the console:
Uncaught (in promise) ReferenceError: runtime is not defined
Note that the function is launched as soon someone click a button in Construct3. I tried several ways but I'm not able to retrieve it.
The idea is to retrieve the json file using the javascript, then set a global variable and push it to the json plugin.
Thank you
The error is telling you runtime is not defined. It's not a global variable, you have to pass it where it's needed. So get() should take runtime as a parameter, and whatever calls get() needs to pass runtime along too.
runtime
get()
Ashley Thank you for the reply.
I added the following script which works if I run it on the Construct 3 event sheet adding the script as an action: https://www.construct.net/en/forum/construct-3/scripting-51/set-global-variable-using-149803
I guess I'm missing something here. If I add the same code on a script.js I get the undefined variable. How I can access or set the global variables in Construct3 directly using a js file? Shouldn't the runtime already be defined?
See Scripts in event sheets and Script files in the manual. Scripts in event sheets provide runtime and localVars automatically. Script files do not.
localVars
Develop games in your browser. Powerful, performant & highly capable.
Hi Ashley, Thank you for the reply. I was able to set the global variable in the event sheet from script! Thanks again