GML How to pass arguments when create instance

Zhanghua

Member
As the title said, How to pass arguments when create instance.
I realize that by changing the argument after object-creating, and trigger the intial method in first step.
But I want to realize this much more gracefully.... Is there any way to gotcha?
Thanks a lot!
 

Simon Gust

Member
Either use a dot operator or a with() statement
Code:
var this = instance_create(x, y, object);
this.attack = 15;
this.defense = 6;
this.image_xscale = image_xscale;
or
Code:
var this = instance_create(x, y, object);
with (this)
{
 attack = 15;
 defense = 6;
 image_xscale = other.image_xscale;
}
This will be run after the create event of the just created instance.
 

Zhanghua

Member
Either use a dot operator or a with() statement
Code:
var this = instance_create(x, y, object);
this.attack = 15;
this.defense = 6;
this.image_xscale = image_xscale;
or
Code:
var this = instance_create(x, y, object);
with (this)
{
 attack = 15;
 defense = 6;
 image_xscale = other.image_xscale;
}
This will be run after the create event of the just created instance.
TKS for your reply.

But the argument is self-defined, and I want it be effected immediately in the create event.
Cause I used this argument to do sth. while intializing.
 

Simon Gust

Member
TKS for your reply.

But the argument is self-defined, and I want it be effected immediately in the create event.
Cause I used this argument to do sth. while intializing.
Then you have to do the initializing directly inside the with() statement.

Can you show the create event for refrence?
 

Zhanghua

Member
Then you have to do the initializing directly inside the with() statement.

Can you show the create event for refrence?
So the FlameLv as following is my intial argument for create the sub objects "FlameBody";

Code:
FlameCenter = oFlameCenter;
FlameBody   = oFlameBody;
FlameTail   = oFlameTail;


Fw = sprite_get_width(FlameCenter);
Fh = sprite_get_height(FlameCenter);

MaxIndex = sprite_get_number(FlameCenter);
FlameLv  = 20;
var _bias = 1;


//Center
InstFlameCenter    =    instance_create_depth(x,y,depth,FlameCenter);

//Body  Here is "FlameLv"
InstFlameBodyArr   =    array_create(FlameLv*4,noone);
for( var i = 0; i<FlameLv; i++ ){
    InstFlameBodyArr[0 + i*4] = instance_create_depth(x+(i+1)*Fw -_bias ,y,depth,FlameBody);
    InstFlameBodyArr[1 + i*4] = instance_create_depth(x-(i+1)*Fw +_bias,y,depth,FlameBody);
    InstFlameBodyArr[1 + i*4].image_angle = 180;
   
    InstFlameBodyArr[2 + i*4] = instance_create_depth(x,y+(i+1)*Fh -_bias,depth,FlameBody);
    InstFlameBodyArr[2 + i*4].image_angle = -90;
   
    InstFlameBodyArr[3 + i*4] = instance_create_depth(x,y-(i+1)*Fh +_bias,depth,FlameBody);
    InstFlameBodyArr[3 + i*4].image_angle = 90;
}



InstFlameTailArr    = array_create(4);
InstFlameTailArr[0] = instance_create_depth(x+(FlameLv+1)*Fw -_bias,y,depth,FlameTail);
InstFlameTailArr[1] = instance_create_depth(x-(FlameLv+1)*Fw +_bias,y,depth,FlameTail);
InstFlameTailArr[1].image_angle = 180

InstFlameTailArr[2] = instance_create_depth(x,y+(FlameLv+1)*Fh -_bias,depth,FlameTail);
InstFlameTailArr[2].image_angle = -90;
   
InstFlameTailArr[3] = instance_create_depth(x,y-(FlameLv+1)*Fh +_bias,depth,FlameTail);
InstFlameTailArr[3].image_angle = 90;
 

Rayleed

Member
Well, as I know, it's impossible to pass argument to Create Event. When you try to set the variable of an instance by getting its id from instance_create, the assignment will be executed after Create Event.
One possible solution is to move the code in Create Event to a User Event (such as User Event 0), then you can simply use the code like:
Code:
with (instance_create(x, y, obj)) {
    a_variable = some_value;
    //.....
    event_user(0);
}
This is all what I know.
 

chamaeleon

Member
Create a script that takes the required arguments (including x, y, object type, etc.) and sets global variables to the create arguments you require in addition to the normal instance_create() arguments, and refer to the global variables inside the create event?
Code:
/// @desc instance_create_args
/// @param x
/// @param y
/// @param layer
/// @param object
/// @param ...

var x = argument[0];
var y = argument[1];
var layer = argument[2];
var object = argument[3];

for (var i = 4; i < argument_count; i++)
{
    global.create_param[i-4] = argument[i];
}

var instance = instance_create_layer(x, y, layer, object);

return instance;
 
Last edited:
Top