• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Variable access on Creation (?)

MIchael PS

Member
Hello,


I have a small question here.
I have an obj_A and an obj_B. Through obj_ A, I create an obj_B, “instance_create(x,y,obj_B)”, and I want to access a variable in obj_B through obj_A, to change it. I don’t want to do it with “obj_B.variable” because I have a lot of obj_Bs and I want to change only the one I am creating now.
I have seen someone doing something like this:

“ var B = instance_create(x,y,obj_B);
B.variable = whatever; ”

But that doesn’t seem to work.
Any ideas?


Thank you a lot for your time,
Michael
 

TheSnidr

Heavy metal viking dentist
GMC Elder
That should work though! What goes wrong when doing that?
Another way to do it is as follows:
Code:
var B = instance_create(x, y, obj_B);
with B
{
    variable = whatever;
}
 

MIchael PS

Member
That should work though! What goes wrong when doing that?
Another way to do it is as follows:
Code:
var B = instance_create(x, y, obj_B);
with B
{
    variable = whatever;
}
Instance create returns the id of created instance, so you dot notation B.variable will change the value.
the example you give is exactly what you need :)
why isent it working?
Thanks for answering.
Well... I use this way and there is a message appearing that says:

" local variable inverted(100001, -2147483648) not set before reading it.
at ��ЩGct_obj_shotgun_ObjAlarm0_1 (line 2) - if inverted = true "

Here is exactly what I am using:

=The event where the obj_B is created:

repeat 10
{
var pellets = instance_create(x+lengthdir_x(10,image_angle),y+lengthdir_y(10,image_angle),obj_enemy_pellets);
pellets.inverted = true;
}


=Obj_enemy_pellets, Create ev. :

var inverted;
if inverted = true
{
direction = point_direction(obj_player.x,obj_player.y,x,y)+random_range(-30,30)
} else
{
direction = point_direction(x,y,obj_player.x,obj_player.y)+random_range(-30,30)
}
 

TheSnidr

Heavy metal viking dentist
GMC Elder
Well, you could do it like this:
Code:
//obj_enemy_pellets's create event
point_direction(x,y,obj_player.x,obj_player.y)+random_range(-30,30)
Code:
var B = instance_create(x, y, obj_enemy_pellets);
with B
{
    direction = point_direction(obj_player.x,obj_player.y,x,y)+random_range(-30,30)
}
The latter will overwrite the former
 
you could put the code from the create event and put in a with statment when you create the instance.

with (instance_create()) {
direction = point_direction(obj_player.x,obj_player.y,x,y)+random_range(-30,30)
}
 

MIchael PS

Member
Well, you could do it like this:
Code:
//obj_enemy_pellets's create event
point_direction(x,y,obj_player.x,obj_player.y)+random_range(-30,30)
Code:
var B = instance_create(x, y, obj_enemy_pellets);
with B
{
    direction = point_direction(obj_player.x,obj_player.y,x,y)+random_range(-30,30)
}
The latter will overwrite the former
Well it worked!
Thank you for your help!!!
 
Top