# How to randomly spawn objects off-screen close to the player?

I am making a zombie game. I want to have it so zombies randomly spawn. My code for this is:

instance_create(
obj_player.x + irandom_range(-300, 300),
obj_player.y + irandom_range(-300, 300),
obj_zom_1 )


The problem with this is that zombies will sometimes spawn really close to the player. I want to have it so zombies spawn randomly but to NOT spawn on the visible screen, because seeing zombies materialize out of nowhere looks silly.

How would I do that?

I don't know the expected limits of your coordinates so I made them constant-like (i.e. screenLeftX). My snippet chooses a random edge of the screen and then a random value between the appropriate coordinate limits of that edge.

var positionAlongEdge = 0;
var which = irandom_range(1,4);
if (which == 1) //top edge
{
positionAlongEdge = irandom_range(screenLeftX, screenRightX);
instance_create(positionAlongEdge, screenTopY, obj_zom_1);
//If the shape of zombies causes them to appear on-screen (at the edge),
//you can push them further beyond the edge:
//instance_create(positionAlongEdge, screenTopY - 10, obj_zom_1);
//or
//instance_create(positionAlongEdge, screenTopY - zombieHeight, obj_zom_1);
}
else if (which == 2) //bottom edge
{
positionAlongEdge = irandom_range(screenLeftX, screenRightX);
instance_create(positionAlongEdge, screenBottomY, obj_zom_1);
}
else if (which == 3) //left edge
{
positionAlongEdge = irandom_range(screenTopY, screenBottomY);
instance_create(screenLeftX, positionAlongEdge, obj_zom_1);
}
else if (which == 4) //right edge
{
positionAlongEdge = irandom_range(screenTopY, screenBottomY);
instance_create(screenRightX, positionAlongEdge, obj_zom_1);
}

• Please explain your code and your train of thoughts. – Vaillancourt Mar 12 '16 at 14:03
• this is not uniform, you should take into account the size of those areas – Sopel Mar 12 '16 at 14:14
• It's giving me an error for the first line: " int where; " it says assignment operator expected. what? – DeathSwagga Mar 13 '16 at 1:36
• It still says assignment operator expected for " int positionAlongEdge; " but the error goes away if I put " = " between them (that messes it up though) so what do I do? – DeathSwagga Mar 13 '16 at 2:03
• @DeathSwagga, sorry again; I've never actually used game maker. It seems variables are just var typed. You may be forced to initialize it as well. I've edited the code. – Jon Mar 13 '16 at 3:22

I would have done more simply:

with obj_player //or in the right place in "step"
{
var xx=random_range(0,room_width);
var yy=random_range(0,room_height);
while point_distance(xx,yy,x,y)<300
{
var xx=random_range(0,room_width);
var yy=random_range(0,room_height);
}
instance_create(xx, yy, obj_zom_1);
}

• Please explain your code and your train of thoughts. – Vaillancourt Mar 12 '16 at 13:51
• This code made the zombies only appear on screen, which is kinda the opposite to what I was aiming to achieve. – DeathSwagga Mar 13 '16 at 1:34
• @DeathSwagga, he meant while distance < 300. This would try again until the point is at least 300 units away, but would still spawn zombies on-screen near the corners. – Jon Mar 13 '16 at 3:31
• @Jon Thank you very much corrected, I mixed up the sign. Yes, I have proposed the code for the "circle" around the player, and not a square. – Kosta Mar 13 '16 at 19:05
• @AlexandreVaillancourt, take two variables are randomly selected based on the size of the room, throws in an array and hung up so that we would find a point located at a distance 300 from the current object (the player). And then create an object at this point. – Kosta Mar 13 '16 at 19:08