GameMaker Passing code onto a changed or created instance.

S

snowy1996

Guest
Hey guys,
I picked up Game Maker Studio 2 about a week ago and I've been enjoying it so far. I'm very very new to making games and I've just been using the drag and drop tools since I'm not entirely up to the more complex side of coding, and so far they've been serving my fine.

I've been slowly building an average RPG battle system, but recently I've come into a problem. Long story short, I have an object built to randomly choose one of several other objects (enemies) so as to decide what appears in the battle room. I'm hoping to possibly pass code from my enemy choosing object onto the chosen enemy object so that I can create a unique variant that will respond to variables set by the object that created it.

Looking at the manual, both the instance_change and instance_create tools both seem capable of modifying their affected instances. However, in my practice I've received various Variable_not_set bugs and the like.

Sorry for the long post, and help is appreciated. I've been trying to work around this by myself for a while now.
I'm still very new to GMS 2, and these forums, so please try to guide me in giving answers to any questions need be asked.
 

samspade

Member
I'm blanking on the exact instance create function for GMS 2, but essentially you can do this:

Code:
var inst = instance_create_layer(x, y, layer, object);
with (inst) {
    /* change some variables */
}
Note that this will run the code after the instance has been created so you can add variables to that object but you need to be careful. For example. Let's say you code this:

Code:
var inst = instance_create_layer(x, y, layer, obj_enemy);
with (inst) {
    on_fire = true;
}
This is fine. The obj_enemy now has the variable on_fire set to true. This will happen before the first step event of this obj_enemy so you can use on_fire in the step event without getting an error. However, if you do not put the variable on_fire in the normal obj_enemy object, you only add it to objects that have been created with this instance create function but you reference the on_fire variable you're going to get a variable not set error. So as a good rule of thumb, you shouldn't use the above method to add variables to an object out of thin air. The object should already have those variables in the create event, probably set to 0 or false if you don't want to use them, and then the code above can change those variables.

Really, you should read this: https://docs.yoyogames.com/source/d..._gml language overview/401_05_addressing.html
 

TheouAegis

Member
I hate instance_change().

To pass variables and such to another instance AFTER IT HAS BEEN CREATED, you can use the with() command or dot-operators.

with instance_create(enemy_x, enemy_y, obj_generic_enemy) {type = get_enemy_by_terrain(); }

To pass variables and such to another instance BEFORE IT HAS BEEN CREATED, you need to store everything (or at least some core variables) in global variables which the instance can then access in its create event.

Nothing can be passed directly to an instance before it has been created.


Edit: Oh yeah this is GMS2. In which case, it'd be instance_create_layer() or whatever, but that's not my concern. The fundamentals are the same.
 
S

snowy1996

Guest
Thank you for the replies. It seems easily possible to set changes in variables, etc, but what I'm wondering now is if it's possible to give the chosen object an if // else check so it will gain the ability to execute additional code based on a global variable. Maybe I've written it out incorrectly, but the check I'm currently trying to give to my chosen object seems to be being ignored.
 

TheouAegis

Member
Suppose you want the global variable to be temporary just for that one moment for when the instance is created. For example, suppose instead of using a local variable we used a global variable.

Code:
global.counter = 0;
repeat 20 {
instance_create(x,y,obj_whatever);
global.counter++;
}
For the first instance created, its create event could read global.counter as 0. The next instance created will read global.counter++ as 1, and so on.

If you wanted to save the value of the global variable at the moment of creation so it can be used later in a step event, you'd save that in the instance created. So for example inside the create event of obj_whatever:

Code:
myIndex = global.counter;
Then you'd just reference myIndex whenever you needed that value. For example:

Code:
alarm[0] = 30 + 15*(myIndex & 1);
 
Top