Accessing global variables

  • Hello, I've written a JavaScript function in a script, and I would like it to update either a global variable string or an array, and I cannot figure it out. I know scripting is a recent addition, but is it possible for JS to update a string created in an event sheet?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Right now the easiest solution would be to use the runtime.callFunction() method to call a function in the event sheet to set the global variable.

    I think it would be useful to come up with a more convenient way to return values from scripts to events. I'm not sure what that feature would look like yet though.

  • Ashley (and anyone else that understand this better than me!), thanks for the idea, but I don't have a runtime object. I've created a function that formats a string, and this function is called from my event sheet. From what I understand, the runtime is a parameter to some of the system functions, such as OnBeforeLayoutStarts(runtime). My function exists independently, so I don't think it has a runtime parameter. Either that or I'm completely wrong... this is all new to me. It's OK to tell me to wait until more samples exist! What I do know is that the runtime.callFunction() method isn't working for me, and I get an error in the development tools console.

  • when you put java scripts in event behind the scenes it creates a async function that has the runtime injected, so it should be there if calling function from events

  • Ohh, I think this may work for me! I don't have my laptop with me but will try some things tomorrow. What's different than this example is that my function is in a script file. But it is being called by a javascript action in an event sheet, so perhaps the runtime is accessible in some manner. If not, I might just put more of my code into the script file, since it looks like for now it might be easier to access Construct object instances than trying to do some processing in a javascript function and some in an event sheet. Thanks for the thoughts, and if you have more, please share!

  • Got it!!! Thank you Ashley & Piranha!

    In my JS function call from within my event sheet, I simply added "runtime" as a parameter. Then I added "runtime" as a new parameter within my large function in the script sheet.

    Now my JS script can call a different function in my event sheet that simply updates a global variable. This was a good puzzle to solve for me, and I got to go into devel tools in Chrome. Learned a lot. Thanks.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)