Constructor showdown

Discussion in 'Advanced Programming Discussion' started by GMWolf, Feb 7, 2018.

  1. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,164
    Following this topic and this status update thread, I hereby open this Constructor Showdown!

    Really simple really, Just show your way of dealing with constructors, and we can compare and discuss them!


    So here goes!

    For mine, I had a few requirements:
    • Compatible with normal instance creation, object variables, and room creation.
    • No bolier plate, no complicated functions to call.
    • Creation code friendly.
    I settled on having an initializer rather than full blown constructor. The idea being that they would server more as a means to initialize the new "object variables".
    The reason being defaults can be set in IDE, and they can be changed in the room editor. However they cannot be initialized at run time before the creation code runs.
    So, because I like my creation code to operate on those values, I wrote the following.
    (note, I did pretty much adapt @FrostyCat 's code)

    scripts go as follows:
    Code:
    ///@func global_construct()
    gml_pragma("global", "global_construct();");
    
    #macro parameters global._parameters
    
    #macro initializer if (constructing_instance_toggle())
    
    global._construct_param_stack = ds_stack_create();
    global._constructing_instance = false;
    
    show_message(ds_stack_pop(global._construct_param_stack));
    
    parameters = undefined;
    
    Code:
    ///@func constructing_instance_toggle()
    var t = global._constructing_instance;
    global._constructing_instance = false;
    
    return t;
    
    Code:
    ///@func instance_construct(obj, layer, ...)
    ///@desc constructs an instance with parameters
    ///@param {real} object the object to instanciate
    ///@param {real} layer
    ///@param {array} parameters parameters to construct instance
    
    var _object = argument0;
    var _layer = argument1;
    var _params = argument2;
    
    ds_stack_push(global._construct_param_stack, parameters);
    parameters = _params;
    
    var _inst = instance_create_layer(0, 0, _layer, _object);
    
    parameters = ds_stack_pop(global._construct_param_stack);
    
    return _inst;
    
    Example usage in create event:
    Code:
    initializer {
        x = parameters[0];
        y = parameters[1];
        foo = instance_create_layer(x, y, layer, parameters[2]);
        width = parameters[3];
        height = parameters[4];
    }
    
    //back to normal creation code
    grid = ds_grid_create(width, height);
    


    But this wouldn't be a showdown if there was only one solution. Post your own scripts and discuss!
     
    Ampersand and Tthecreator like this.
  2. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,066
    I wrote a blog post about this this time last year and that was a little less complex than this (no nesting support).
     
    Ampersand, Tthecreator and GMWolf like this.
  3. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,164
    Yes i remember that post! Great stuff! The instance change hack is super cool
     
    Ampersand likes this.
  4. Ampersand

    Ampersand Member

    Joined:
    Jan 30, 2017
    Posts:
    216
    I had actually been doing something similar to @YellowAfterlife's method without having seen that post! I appreciate seeing both of your methods though, especially yours @GMWolf. Really proper
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice