GM:S 1.4 how to spawn objects outside room?[solved]

Discussion in 'Programming' started by Artwark, Aug 29, 2016.

  1. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    Ok so I got enemies that spawn and will move towards a certain object. How do I make them spawn outside the room? I tried the following.....

    Code:
    if --comet == 0//if variable comet reaches 0
    {
    
    //if !instance_exists(obj_comet)
    
    //instance_create(irandom_range(x+35,x+50),irandom_range(y+35,y+50),obj_comet);
    
    //instance_create(irandom_range(x-sprite_width,x+sprite_width),irandom_range(y-sprite_height,y+sprite_height),obj_comet)
    
    //instance_create(random(room_width-10),-20,obj_comet)
    
    instance_create(irandom_range(room_width,-65),irandom_range(room_height,-120),obj_comet)
    
    comet = 500;
    }
    
    I tried doing it so that it respawn from the minus of x2 and y2 but that's not working either. How to fix this?
     
  2. TheMatrixHasMe

    TheMatrixHasMe Member

    Joined:
    Jun 22, 2016
    Posts:
    176
    @Artwark
    How far outside the room boundaries do you need them to spawn? Right outside is fine?

    Edit: @Artwark Look below at whale_cancer's post, this is the way you should go.
     
    Last edited: Aug 29, 2016
  3. whale_cancer

    whale_cancer Member

    Joined:
    Jun 29, 2016
    Posts:
    399
    Something like...
    Code:
    xMod = irandom_range(0, 65)
    yMod = irandom_range(0, 120)
    
    tempRoll = irandom_range(0,3)
    
    if tempRoll == 1 instance_create(room_width + xMod, room_height + yMod, obj_comet)
    else if tempRoll == 2 instance_create(room_width + xMod, -yMod, obj_comet)
    else if tempRoll == 3 instance_create(-xMod, room_height + yMod, obj_comet)
    else instance_create(-xMod, -yMod, obj_comet)
     
    TheMatrixHasMe likes this.
  4. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    Ok. I want to understand what that logic does and compared to what I tried, how this works better than mine......

    Like how are you giving tempRoll == 1 if its value is from y? and with that, how is it able to change value for x?
     
  5. Stubbjax

    Stubbjax Member

    Joined:
    Jun 22, 2016
    Posts:
    95
    Your range is around the wrong way. It should always be the smallest value followed by the largest, otherwise you'll get extremely large positive or negative numbers.

    Change this:
    instance_create(irandom_range(room_width,-65),irandom_range(room_height,-120),obj_comet)

    To this:
    instance_create(irandom_range(-65,room_width),irandom_range(-120,room_height),obj_comet)
     
  6. whale_cancer

    whale_cancer Member

    Joined:
    Jun 29, 2016
    Posts:
    399
    I don't quite understand what you are asking.

    The logic of my code is as follows:

    Generate a random number to modify both the x and y value.

    Pick one of four options by using a random number from 0 to 3. These options cover spawning outside of the room in one of four directions (up and left, up and right, down and left, down and right). If you want it to be more complete, you could include 4 more options (left, up, right, down).

    Each option uses the randomly generated xMod and yMod to determine how far outside the map in that direction to create a new obj_comet.
     
  7. whale_cancer

    whale_cancer Member

    Joined:
    Jun 29, 2016
    Posts:
    399
    The issue with this fix is that you will only generate comets outside of the room to the left and up. I understand OP to want to generate them outside of the room generally speaking.
     
  8. Stubbjax

    Stubbjax Member

    Joined:
    Jun 22, 2016
    Posts:
    95
    Oh right, I understand now. In that case, the below code should do the trick and is hopefully relatively easy to understand:
    Code:
    var range = 65;
    var x_pos = choose(irandom_range(-range, 0), irandom_range(room_width, room_width + range));
    var y_pos = choose(irandom_range(-range, 0), irandom_range(room_height, room_height + range));
    
    instance_create(x_pos, y_pos, obj_comet);
    If you don't want your objects spawning too closely to the edges, you can add some padding too:
    Code:
    var range = 65;
    padding = 10;
    var x_pos = choose(irandom_range(-range, -padding), irandom_range(room_width + padding, room_width + range));
    var y_pos = choose(irandom_range(-range, -padding), irandom_range(room_height + padding, room_height + range));
    
    instance_create(x_pos, y_pos, obj_comet);
    Hope that helps!
     
    Artwark likes this.
  9. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    I want to understand how your code works so that I can understand GML language better. Like the 0 & 3 aren't x and y values for example.

    But thanks for your help!
     
  10. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,889
    tempRoll is just a random variable he made to pick a random side of the room to spawn the thing at. He's basically saying, "Pick a random side to spawn on and save that to the variable tempRoll." So 0 means spawn in one quadrant, 1 means spawn in another quadrant, 2 means spawn in a different quadrant, and 3 means spawn in the only other quadrant left.

    If you have n options to choose from, you can pick one of those options by using irandom(n-1). So if you have 4 options to choose from, you can use irandom(3). If you wanted to spawn in a particular octant (not sure if that's a word, but whatever), you'd use irandom(7).
     
  11. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    Ok, I tried doing what whale_cancer gave but the object only shows up at the top right corner and the bottom right corner......

    Here's the code
    Code:
    Step Event
    
    xmod = irandom_range(0,65);
    ymod = irandom_range(0,120);
    temproll = irandom_range(0,3);
    if --comet == 0//this is to give the time for the object to start spawning
    {
    if !instance_exists(obj_comet)
    //instance_create(irandom_range(x+35,x+50),irandom_range(y+35,y+50),obj_comet);
    //instance_create(irandom_range(x-sprite_width,x+sprite_width),irandom_range(y-sprite_height,y+sprite_height),obj_comet)
    //instance_create(random(room_width-10),-20,obj_comet)
    {
    if temproll == 1
    {
    instance_create(room_width + xmod, room_height + ymod, obj_comet)
    }
    else if temproll == 2
    {
    instance_create(room_width + xmod,-ymod, obj_comet)
    }
    else if temproll == 3
    {
    instance_create(-xmod, room_height + ymod, obj_comet)
    }
    }
    comet = 500;//to reset the time for the object to spawn
    }
    
    
    If it helps, here's the code for the object that's suppose to spawn

    Create event
    speed = 0.5

    Step event
    move_towards_point(obj_saturn.x,obj_saturn.y,1)
     
  12. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,959
    You are not handling the case where temproll is zero. After handling temproll == 3, you are missing from the earlier example:
    Code:
    else instance_create(-xmod, -ymod, obj_comet)
     
  13. Artwark

    Artwark Member

    Joined:
    Jul 11, 2016
    Posts:
    351
    But even after i added that code, it still only goes to the same directions......
     
  14. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,959
    I tested the code and it runs fine. Objects will (eventually) come out of all four corners. To test your code, remove the instance_exists check to allow for multiple simultaneous comets and change comet = 500 to comet = room_speed to spawn a comet every second.
     
    whale_cancer likes this.
  15. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,889
    Make sure your asteroids are moving toward the center of the screen. Maybe you set your asteroids to only move left, so all the asteroids that spawn on the left are moving further out of the room.
     
  16. MIchael PS

    MIchael PS Member

    Joined:
    Aug 12, 2017
    Posts:
    95
    Quite an old post ehh?
    I had the same problem, and I actually didn't want the enemies to be spawn in the corners.
    The solution is easier than it seems!!!

    You have to calculate the maximum diagonal distance from the center of the room to a corner.
    And create the objects in a random point of a circle that has a radius greater or equal to that maximum distance.
    So that all of your objects will be created outside the room.

    Code:
    var random_dir = random(360)
            instance_create(room_width/2+lengthdir_x("diagonal distance",random_dir),room_height/2+lengthdir_y("diagonal distance",random_dir),"your object");   
    upload_2018-7-18_17-32-58.png

    I am quite late but I hope that helped
     
    Parker likes this.

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