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

create instances only in specific area

TRP

Member
Hi there!

Having a bit of trouble making objects appear only in a certain area. Like below image

I'm using this:

instance_create_layer(random(room_width -500),random(room_height -500),"Instances",obj_enemy);

They create but everywhere. Even at the very edges of the room. I'm obviously missing something.

Thanks for any help!
question.png
 

TsukaYuriko

☄️
Forum Staff
Moderator
Right now you're only excluding the right border (room width minus 500 on the right) from the horizontal spawning... you'd also have to exclude the left border (leftmost point of the room plus 500). ;)
 

saffeine

Member
you're forgetting to offset from the top / left too. right now you're just subtracting from the right / bottom.

GML:
//    declare your bounds here.
var _top      = 400;
var _left     = 200;
var _right    = 800;
var _bottom   = 800;

//    now get the differences between the points, that's your effective spawn area.
//    once you've gotten the random position in that area, add the minimum positions.
var _x = random( _right - _left ) + _left;
var _y = random( _bottom - _top ) + _top;

//    spawn the object.
instance_create_layer( _x, _y, "Instances", obj_enemy );
edit: Tsuka was much quicker than me it seems. oh well, there's always next time 😔
 

Yal

🐧 *penguin noises*
GMC Elder
Try using random_range to have an easier time computing the limits:
GML:
instance_create_layer( random_range(500,room_width - 500), random_range(500,room_height - 500) , "Instances", obj_enemy );
 
Top