Constructor showdown

GMWolf

aka fel666
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!
 
A

Ampersand

Guest
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
 
Top