Global Scope AND access to runtime?

0 favourites
  • 9 posts
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • Reading through Docs and Forums, and a little testing out, it seems like when using script files:

    • Accessing Construct objects is done via runtime, which is passed into functions such as the runOnStartup
    • To integrate with events, specifically having functions in a script file called, the functions need to be declared in global scope, which means NOT in runOnStartup

    Is there a way to do both?

    I'm thinking declare an object in the global scope and add methods to it from inside the runOnStartup function . . . but wanted to tread carefully until I'm understanding further.

    --A little background:

    I'm actually working in a fairly large project that is ported over from C2 (using C3 runtime). I wanted to create some objects in script files that handle a lot of the logic (such as filtering, sorting, mapping arrays) and then either call functions (Event Sheet functions that were setup prior) or assign variables (Global and/or Instance). So that is why I'm trying to get it declared in the global scope but have access to runtime / fully integrate and interact.

    So I'm new to Scripting in C3, just recently upgraded, but experienced with both Construct and JavaScript.

    Thanks for any help here! I don't want to spend too much time figuring this out right now if this solution isn't possible/practical, or even if it isn't recommended as I don't want to cause myself troubleshooting issues down the line.

    Thanks

  • I guess I can just pass in runtime to each method that needs it. Will give that a try . . .

  • This is a copy from the answer I provided on the Discord server, in case it might help anybody else.

    If I understand your struggle correctly you just want to have access to runtime globally

    Just add it to the global scope

    Something like this in your code will do.

    runOnStartup(async runtime => { globalThis.g_runtime = runtime })
    

    Then from any function, using g_runtime or globalThis.g_runtime will return the runtime object

    If you do not want runtime to be globally accessible however, there are other ways which I explained here:

    construct.net/en/tutorials/js-writing-javascript-modules-2384

    In summary, you can do this:

    runOnStartup(async runtime =>
    {
     let localVar; //Defines a local variable that will still be accessible to your global functions
     runtime.globalVar; //Defines a global variable in the runtime object
     runtime.globalFunction = () => {} //Defines a global function in the runtime object
     globalThis.globalVar; //Defines a global variable in the real global scope
     globalThis.globalFunction = () => {} //Defines a global function in the real global scope
     function localFunction () {} //Defines a local function that will still be accessible by your global functions
    });
    

    I do recommend reading through the tutorial as I go through a few other methods on how to properly design JS modules that are both powerful, easy to write, have access to whatever you might need and are easy to contain to avoid overlap with other modules and prevent external people from accessing unneeded data

  • Update

    I am testing out declaring my main object in the global scope and attaching runtime to it as a parameter inside the boilerplate function runOnStartup.

    I literally just did this seconds ago so will see if I find any issues from this. The main reason I needed this now is to call some functions setup in event sheets.

    I have also attached the Sprite this logic is controlling in a parameter and that seems to be working fine. I pass it to my script from an 'On Layout Start' - on the layout this sprite is on.

    In another case when I call a method from an event sheet I pass the instance 'picked' with the events, using getFirstPickedInstance().

    So I am finding different ways to go about this that all seem to be working out fine right now.

    As far as the tip to put scripts in an anonymous function, I don't believe I need that as I am using a runtime worker. skymen, you can correct me if I'm wrong. Also, it isn't my biggest concern as this logic wouldn't really benefit any cheaters.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Using worker will prevent name overlap with code on the window object, and will prevent people from accessing your code from the console, but you'll still have to be weary of name overlaps between your own functions (which is a lot more manageable indeed)

  • FYI you can still access workers in the browser console/dev tools.

  • I'm a beginner. I don't know if I understand you correctly.

    That's what I do

    	globalthis.a=runtime.objects.a.getFristInstance()
    
  • I'm a beginner. I don't know if I understand you correctly.

    That's what I do

    > 	globalthis.a=runtime.objects.a.getFristInstance()
    

    I'm sure that works but I'd assume it might be bad practice - polluting global namespace if not anything else.

    But not sure what issues or performance you'd actually run into. Not my area of expertise.

    And yes, that is what I was looking to solve.

  • JavaScript is case sensitive. You must use globalThis, not globalthis.

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