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

[SOLVED]do loop not working in repeat loop v1.4

S

SimonB

Guest
I have the following code in the create event of a controller object:

var rock
repeat(6)
{
rock=instance_create(random(room_width),random(room_height),object_rocket);
do
{
rock.x = random(room_width);
rock.y = random(room_height);
}
until (place_empty(rock.x,rock.y));
}


It doesn't work as I expect it to and the rockets overlap with other objects in the room. I can get it to work by putting the do loop in the create event of the rocket, but I want to understand why this doesn't work. By playing around with it, I'm pretty certain it just changes the position once then repeats, rather than looping through until nothing overlaps. If someone can explain it to me please, I'd be very grateful. My apologies if it's staring me in the face and I just can't see it!
 
T

TimothyAllen

Guest
You're making the same mistake that lots of new GM user make. place_empty (and the like) use the calling instance's mask. So if your controller has no mask (or any mask that's different then the rocks) then place_empty will not detect collisions the way you are expecting. Easily solved in many ways, but now that you know the issue, ill let you find a solution.
 
S

SimonB

Guest
It seems so obvious now you say it! But not so much before... Thank you for taking the time to help.
 

TheouAegis

Member
Use

irandom(room_width) & ~15
and
irandom(room_height) & ~15

ItIt snaps your rocks to a 16x16 grid and prevents pixel resizing.
 
S

SimonB

Guest
Thank you for the additional help. irandom I know, but not &~15. Presumably I'd use &~(n-1) for a nxn grid? A quick look in the manual tells me that ~ negates the next value bitwise but I don't really understand what's happening here. If you were able to explain please, it would be really helpful.
 

Tornado

Member
Last edited:
S

SimonB

Guest
That's really cool - thank you for explaining. Looks like this would only work with grid sizes that are powers of 2, but really helpful :)
 

Tornado

Member
The general raster formula for any raster size would be I guess:
(irandom(...) div N) * N
for example if N is 20:
(6 div 20) * 20 = 0
(22 div 20) * 20 = 20
(50 div 20) * 20 = 40
(70 div 20) * 20 = 60
...
...
 
S

SimonB

Guest
That's great - I sometimes use a 40x40 grid, so very helpful to know. Thanks again for your help.
 
Top