• 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]Summon monsters in random but different places in rooms

sinigrimi

Member
I am generating random room with monsters and I need them to not spawn on each other. It works but looks terribly big, can it be simplified?
That's what I'm doing:


Script-Code:
Code:
randomize();
             var x1 = 160 + random(room_width - 320);
             var y1 = 160 + random(room_height - 320);
             var x2 = 160 + random(room_width - 320);
             var y2 = 160 + random(room_height - 320);
             var x3 = 160 + random(room_width - 320);
             var y3 = 160 + random(room_height - 320);
             var x4 = 160 + random(room_width - 320);
             var y4 = 160 + random(room_height - 320);
             var x5 = 160 + random(room_width - 320);
             var y5 = 160 + random(room_height - 320);
if irandom(10) = 0{
     instance_create_layer(x1, y1,  "Instances",  oBee);
     instance_create_layer(x2, y2, "Instances",  oBee);
     instance_create_layer(x3, y3, "Instances",  oBee);
     instance_create_layer(x4, y4, "Instances",  oBee);
     instance_create_layer(x5, y5, "Instances",  oBee);
}
 
Last edited:

rIKmAN

Member
Please just post your code in between [code] and [/code] tags, or use the Insert button in the toolbar above the textbox and choose code.

There is no need for all the multicoloured text and it makes it unreadable for some people as shown in your other thread.
 

sinigrimi

Member
Please just post your code in between [code] and [/code] tags, or use the Insert button in the toolbar above the textbox and choose code.

There is no need for all the multicoloured text and it makes it unreadable for some people as shown in your other thread.
Ok, i changed
 

Alice

Darts addict
Forum Staff
Moderator
Loops are your friends. You might want to read up the manual on repeat, for, while and do/until loops. Your use-case is simple enough to use plain repeat loop.

I assume the "if irandom(10) = 0" is meant to make sure that bees are the enemy type you want to create - at least, that's what I assume from your other thread. I decided to extract it to enemyType variable and use choose(...) random function instead, but you're free to determine enemyType however you need.

Also, I took the libery of converting min + random(max - min) notation to irandom_range(min, max) notation.

The simplified code is as follows:
Code:
var enemyType = choose(oBee, oRat, oSpider, ...);
repeat (5) {
    var xx = irandom_range(160, room_width - 160);
    var yy = irandom_range(160, room_height - 160);
    instance_create_layer(xx, yy, "Instances",  enemyType);
}
Another thing: note that there's still chance that enemies will spawn in a way that they'll overlap, but it's extremely unlikely that they'll appear at the very same position.

--- EDIT ---
I changed variable names from x/y to xx/yy, to avoid collisions with native x/y variables.
 
Last edited:
Top