Suggestion - gml_paragma to specify global execution order

Comments

11 comments

  • Julian Adams

    Could potentially have an execution order manager similar to the new room order manager.

    1
    Comment actions Permalink
  • Core Tech

    you really should not be relying on any order of execution, can you give me an example why you would really need this.

     

    Russell

    0
    Comment actions Permalink
  • Zach Reedy

    I'm currently working on a port of the JavaScript library, lodash for GML.

    In this library I have one script, 'GMLodash', which is the lodash declaration and has a line to declare lodash globally.

    Then I have a separate script 'UnitTests', which are intended to not be included when imported into projects.

     

    I doesn't make sense for me to merge these two script files, but the UnitTests scripts make sense to run in the global scope.

    Sure, I could declare the unit tests as a function and call that on the Room Creation code for the first room, but to be honest, this just feels like a ton of missed opportunity.

     

    Realistically, what we're talking about here is ensuring that "static classes" are available. Given some of the idiosyncrasies of GML, it does make sense to have some sort of execution order. It would just reduce friction where no friction is needed. I can imagine there being other Utility level things that I would want to initialize before anything else.

     

    Example: Maybe I want a debug tracer, and I want to ensure that a log file gets created before I do anything else, so that a global function 'trace' can log to a file. Yes, I could just lazy initialize the file in the trace function, but it would still reduce friction (especially if the issue might occur before the first trace call).

    2
    Comment actions Permalink
  • Gabe Weiner

    As another usage example: I am creating a component system similar to Unity's. Let's say I want my components to be stored in a struct, `Components`,  inside of my controller object, `System`.

     

    /// Script: Collider

    function __Collider() constructor {
      // blah
    }

    System.Components.Collider = __Collider;

    Rather than my components be global (which I very much would want to avoid), I can namespace my components properly this way (while still getting to have one file per component for scalable organization). However, without being able to ensure that a script that initializes my instance `System` runs first, I have no way to do this.

    0
    Comment actions Permalink
  • Dickson Law

    I think the compiler should pull up all functions declared at the global scope (i.e. NOT inside a struct, argument or variable assignment) and initialize all of them first, the same way enums and macros are done. That way order-of-execution will no longer be an issue for other global setup code depending on these declarations.

    I am also making libraries containing utility classes that the end user may potentially use at the global setup level. These declarations have to run first, or the user will get a "trying to initialize something that's not an object" error that is not really their fault. Forcing us to use Room Creation code or one-step alarms creates a syntax rift between code that can run at global setup and code that can't, and synthetically limits the utility of structs and lightweight objects for experienced developers.

    0
    Comment actions Permalink
  • Russell Kay

    All Scripts are executed first (before any rooms are run) so all the functions are declared and any code between them is executed.

    If you need execution order then use a singleton type structure when initialising (in the code between scripts i.e.)

     

    function System() {

       static sSystem = { };

       return sSystem;

    }

     

    System().Components.Collider = _Collider;

     

    NOTE: you would only need to do this in the bits that setup the scripts as when the game starts you could have setup an actual global which could be aliased.

    and you could use a mixture of macros as well to make the syntax for users nicer.

     

     

    Russell

     

    0
    Comment actions Permalink
  • Dickson Law

    The functions are not all declared first. See: https://help.yoyogames.com/hc/en-us/community/posts/360009036238-Can-t-use-constructors-defined-in-other-scripts

    Please consider pulling up global-level scripts to all be defined first, the same way enums and macros are.

    0
    Comment actions Permalink
  • Russell Kay

    This is just a bug and you should be able to call the functions intra script resources

    Russell

    0
    Comment actions Permalink
  • Dickson Law

    Can you please clarify whether it is a bug in my example code or in the compiler?

    0
    Comment actions Permalink
  • Russell Kay

    I mean it is a compiler bug and it should work properly

    Russell

    0
    Comment actions Permalink
  • Cesar Ottani

    I got a same-ish issue. And I totally used the manual examples.

    https://help.yoyogames.com/hc/en-us/community/posts/360009432957-New-Function-Error

     

    It would be nice that GM handles the order of what functions needs to be created like all compilers do. If left to the user it would be... strange(?).

    0
    Comment actions Permalink

Please sign in to leave a comment.