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

GML How do yow set it so that the enemy only spawns outside the view.

My game has a fairly small room. I have the enemies spawn on a random position, but they sometimes spawn right in front of the player. How can I make it so that they only spawn outside the view? Thank you!
 

samspade

Member
Room or view? Both are fairly easy but slightly different. If you want them outside of the room then you just have to set coordinates that are either less than 0 or greater than room_width/height. If you want them outside of a view it is essentially the same thing except they need to be less than the view's x/y location or greater than the view's x/y location plus view width/height. The way to get these variables is slightly different in GMS and GMS2 as GMS2 uses cameras.

A very simple and limited example of spawning outside of the room:

Code:
var xx = choose(random_range(-100, 0), random_range(room_width, room_width + 100);
var yy = choose(random_range(-100, 0), random_range(room_height, room_height + 100);

instance_create(xx, yy, object);
//instance_create_layer(xx, yy, layer, object); in GMS 2
This code will spawn an object in one of the four corners outside of the room (you could graph these points to see what I mean).
 

flerpyderp

Member
The following code picks a random value between a specified range in all four directions outside of the view, then picks whether the final coordinate is to the left or right of the view, and whether it is above or below it.

Code:
var low, high, left, right, up, down, getX, getY;

low= 10; //The range outside the view, change these to suit
high= 30;

left = view_xview - random_range(low,high);
right = view_wview + random_range(low,high);
above = view_yview - random_range(low,high);
below = view_hview + random_range(low,high);

getX = choose(left, right);
getY = choose(above, below);
Use getX and getY as the coordinates for the spawn.
 
Top