Pre-Create-Variables (just a thing i wish implemented)

WilloX

Member
Hi,

so one thing that bothers me a bit with GML is that by creating an instance there is no (easy / reliable / known to me) way to set variables before the create event is executed. I would have no problem with the work around
GML:
var inst instance_create_depth(x, y, 0, obj_test);
inst.spd = 2
inst.type = 1
//...
for example.

But i run into trouble time and time again once the create event relies on some variables (i "fix" it with alarms after 1 step but it feels clunky).

But now we have structs and i feel like it is the perfect time to get some new functionality with the instance creation:
GML:
instance_create_depth_ext(x, y, depth, object, {spd : 2, type : 1})
where you give a struct as an argument to have the variables in it set immediately.
I sure don't know how GML works behind the curtains but this shouldn't be to difficult (i hope - could be wrong though).

Thanks for reading^^
 
Last edited:

chamaeleon

Member
Seems to me that, if such an ext function was implemented, with a parameter holding a struct with data, this could take on the form of a Create event equivalent of network events async_load (not the same name, just some variable whose purpose in life is to be valid to reference within a Create event).
 

TsukaYuriko

☄️
Forum Staff
Moderator
With methods, this is as simple as this:

Create event of the object:
GML:
// code that doesn't rely on variables being set from the outside

function post_create()
{
    // code that relies on variables being set from the outside
}
Creating instances of said object:
GML:
with (instance_create...(...))
{
    // set variables...
    post_create();
}
 

WilloX

Member
With methods, this is as simple as this:

Create event of the object:
GML:
// code that doesn't rely on variables being set from the outside

function post_create()
{
    // code that relies on variables being set from the outside
}
Creating instances of said object:
GML:
with (instance_create...(...))
{
    // set variables...
    post_create();
}
That is a way better workaround than the one i was using. But i still wish there was a supported function that saves you the extra steps
 

kburkhart84

Firehammer Games
If you use the object dialog to define variables instead of the create event(at least for the ones you want easily changeable), you can change them easily for each instance you put down. This only applies to instances placed in the room editor though. For instances I create in code, I just change whatever I need to in the with() statement afterwards. If I was creating these instances in multiple places, I would likely make the repeating code as a method like suggested above.

For the most flexibility, they could do it by adding a pre-create event, which gets called just before the regular create event. That event would receive arguments based on what you send to the instance create function besides the main ones. I don't know the easiest way to handle it, if they could do it like function used to be with argument0, etc... since you can't define it like the new 2.3 functions, or if they would just limit it to a single struct that contains everything, I don't know. But something along these lines seems like the easiest way to do it to me. And the pre-create event simply wouldn't happen if no arguments were added.

Code:
instance_create_depth(x, y, 0, obj, someVal, someStruct, etc...);
 

FrostyCat

Redemption Seeker
Yal has published a book line for this use case 4 years ago, and here is my interpretation of it in post-2.3 GML. With the exception of nullified Variable Definitions (AKA Object Variables), most of it seems to be working. Until YoYo makes it built-in or when someone else has a better idea, this is the best way I know of.
 
Top