SOLVED Help with "with statements" and instances

Hello!
I've been struggling a bit on understanding how to manipulate and instance objects on multiple instances of my enemies.
Basically i'm trying to create a secondary collision mask that follows them locked to their x, y.

if (!instance_exists(obj_M_Collider)) {
with(obj_Monster_Parent) {
instance_create_depth(x, y, 0, obj_M_Collider);
}
}

with (obj_Monster_Parent) {
obj_M_Collider.x = x;
obj_M_Collider.y = y;
}

My if statement works fine, since it creates the "obj_M_Collider" on the x, y of each enemy on the room, however i don't know how to proceed because when i declare that the mask is tied to their x, y, all the created masks gets tied to the last enemy instance in the room.
I'm storing most of the monster basic codes on the obj_Monster_Parent since it's something all the monsters in the game will have and it seems to be the right way to do it instead of putting this same code on every single monster i think. (Except for bosses wich i'll use another Parent).

I tried to do this in many ways and still didn't found an answer, neither on the internet, i found many posts talking about this kind of stuff, that you need to make game maker understand each instance separatedly, but didn't found anyone talking about how i should proceed or what logic i should use.

Any help is welcome! :D
 

TsukaYuriko

☄️
Forum Staff
Moderator
When writing to variables in an object context, you're writing to every instance of that object in the room. Your code has no concept of which collider belongs to which parent, so it's impossible to tell them apart.

When creating colliders, assign the collider's instance ID (the return value of instance_create*) to a variable, then set the coordinates of the instance referenced by said variable rather than every instance of the object.
 
When writing to variables in an object context, you're writing to every instance of that object in the room. Your code has no concept of which collider belongs to which parent, so it's impossible to tell them apart.

When creating colliders, assign the collider's instance ID (the return value of instance_create*) to a variable, then set the coordinates of the instance referenced by said variable rather than every instance of the object.
So, when i use instance_create with the "with statement" it does reach out for every instance of my enemy and create the mask, but if the object already exists, then i need to store their IDs in a variable and then attach the mask to "variable.x , variable.y" ?
 
It worked! This opened a whole new level of ideas 😂
Thx a lot @TsukaYuriko!

And for anyone who comes here the solution was:

if (!instance_exists(obj_M_Collider)) {
with(obj_Monster_Parent) {
mask_ID = (instance_create_depth(x, y, 0, obj_M_Collider))
}
}

with (obj_Monster_Parent) {
mask_ID.x = x;
mask_ID.y = y;
}
 

TsukaYuriko

☄️
Forum Staff
Moderator
So, when i use instance_create with the "with statement" it does reach out for every instance of my enemy and create the mask, but if the object already exists, then i need to store their IDs in a variable and then attach the mask to "variable.x , variable.y" ?
Correct.

with statements change the scope of execution to whatever you specify. If you specify an instance, it switches to that instance. If you specify an object, it switches to all instances of this object in the current room, one by one. Whatever variables you create will therefore be created for each instance, and whatever code you run runs for each instance. So, in other words, that code creates a collider instance and assigns its ID to a newly created mask_ID variable for every instance of obj_Monster_Parent.

If you don't store the ID, the instances of obj_Monster_Parent won't have any link to the colliders, so there's no way for them to refer to the collider that belongs to them because you haven't established the "belonging" portion. That's what storing the ID in a variable does.
 
Thanks a lot, you gave me exactly the direction i needed to solve many problems i was having!
I fixed my pathfinding code as well and now each monster inherits their own pathfinding code from the monster_parent! 😬

with (obj_Monster_Parent) {
cLine_ID = collision_line(x, y, obj_Player.x, obj_Player.y, obj_Wall_Collider, false, false);
cCircle_ID = collision_circle(x, y, 35, obj_Wall_Collider, false, false);
if (cLine_ID || cCircle_ID) {
monsterPathfinding = true;
scr_Pathplan(obj_Player.x, obj_Player.y);
}

else {
path_end();
monsterPathfinding = false;
move_towards_point(obj_Player.x, obj_Player.y, monsterMoveSpeed);
}
}

I had to gave each one their own collision_line and collision_circle to check the pathfinding, and looks like just by doing this it solved all the problems of my pathfinding code!
 
Top