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

Legacy GM how to spawn objects outside room?[solved]

A

Artwark

Guest
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?
 
T

TheMatrixHasMe

Guest
@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 by a moderator:
W

whale_cancer

Guest
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)
 
A

Artwark

Guest
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)
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?
 

Stubbjax

Member
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)
 
W

whale_cancer

Guest
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?
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.
 
W

whale_cancer

Guest
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)
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.
 

Stubbjax

Member
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!
 
A

Artwark

Guest
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.
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!
 

TheouAegis

Member
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).
 
A

Artwark

Guest
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)
 

NightFrost

Member
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)
 
A

Artwark

Guest
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)
But even after i added that code, it still only goes to the same directions......
 

NightFrost

Member
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.
 

TheouAegis

Member
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.
 

MIchael PS

Member
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
 
Top