• 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!

GameMaker [ SOLVED ] repeat/for loop not working as intended

P

_Proxy

Guest
This is the code:
Code:
repeat (instance_number(obj_work_table))
{
 var xx = obj_work_table.x;
 var yy = obj_work_table.y;
 var notif = obj_notif_arrow_down;
 var notif_x = xx;
 var notif_y = yy - 450;
 // Create the arrow notification above the workbench
 notif_work_table = instance_create_depth(notif_x, notif_y, -1000, notif);
 // Make it wave up and down
 notif_work_table.wave_motion = true;
 notif_work_table.wave_y1 = yy - 450;
 notif_work_table.wave_y2 = yy - 400;         
}
I have 2 tables (obj_work_table) in my room (rm_house). What I am trying to do is create a notification (obj_notif_arrow) on top of both of the tables, but for some reason it is creating both of the notifications on one of the tables. Here is a link for you to see my create event and step event [ https://pastebin.com/2TynNWZb ]
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
You are referencing the table by OBJECT id and not INSTANCE id... You need to get the instance ID for each instance, otherwise how will GMS2 know which instance of object "obj_work_table" you are referring to when you do "xx = obj_work_table.x"?

This is easily resolved however by using the "with" construct...

Code:
with(obj_work_table)
{
 var notif = obj_notif_arrow_down;
 var notif_x = x;
 var notif_y = y - 450;
 // Create the arrow notification above the workbench
 notif_work_table = instance_create_depth(notif_x, notif_y, -1000, notif);
 // Make it wave up and down
 notif_work_table.wave_motion = true;
 notif_work_table.wave_y1 = yy - 450;
 notif_work_table.wave_y2 = yy - 400;       
}
Using "with" here will loop through each instance of the object in the room and then run the code on that instance so you don't need to worry about counting the instances, getting ids, etc... ;)
 
Last edited:
Top