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

Discussion in 'Programming' started by Just a game programmer, May 22, 2018.

  1. Just a game programmer

    Just a game programmer Member

    Apr 10, 2017
    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!
  2. samspade

    samspade Member

    Feb 26, 2017
    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:

    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).
  3. flerpyderp

    flerpyderp Member

    Apr 26, 2018
    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.

    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.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice