Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Hopefully this should be a simple one; I want to restrict the points that instances enter the screen from so they don't come in at the edges.

In Game Maker I'm using the following code instance_create(random(room_width), random(-100) - 50, obj_enemy1); to create the instance off screen (create(x, y, ...))

At the moment I'm just using the room_width to define the max width for the random on x, but ideally I want to find a way of defining a max AND min width for the random. I can't figure out how to restrict the range on the x axis to between say 100 and 350.

Any help would be appreciated.


share|improve this question
Hi guys, I've been tinkering with this this morning and have created this code, but it's still not working minrand = 150; maxrand = room_width - 150; randnumber = random(room_width); realrand = 0; if randnumber < minrand {realrand = 150} else {realrand = room_width - 150}; instance_create(realrand, random(-100) - 50, obj_enemy1); timer -= 10; alarm[0] = timer; randnumber = 0; still no luck :( – CLockeWork May 25 '13 at 9:22
If you need a random number between a and b, you should just do: number = a + random(b - a). – sam hocevar Jul 25 '13 at 21:45

4 Answers 4

after some poking around I've found a working solution, if anyone can suggest an easier way I'd greatly appreciate it.

//adds extra planes to the room

//create a collection of temp variables to difine the range as a min and max
//create a random number variable based on the room size
//find the location of the random value and if it falls outside of the min or max
   //move move it to the min or max
//create an enemy plane within the restricted x axis of the room and between 50 and
   //100 pixels above the screen

var minrand, maxrand, randnumber, realrand;
minrand = 50;
maxrand = room_width - 50;
randnumber = random(room_width);
realrand = 0;

if randnumber < minrand
realrand = minrand
   if randnumber > maxrand
   realrand = maxrand
    realrand = randnumber

 instance_create(realrand, random(-100) - 50, obj_enemy1);


share|improve this answer
Game Maker Language also has this function: random_range – Seth Battin May 30 '13 at 4:20

You can use the median function:

point = median(smallestValue, randomValue, largestValue)
share|improve this answer

Try this:

instance_create(random_range(50,room_width - 49), random(-100) - 50, obj_enemy1);

It does everything your script does, and it is more evenly balanced.

share|improve this answer

I think you may have overcomplicated a few things in your code.

So assuming you want enemies to come in, say, from the top of the screen downwards, then you can achieve spawning a single enemy with one line of code:


(This code assumes that you have views enabled and are using view 0, and that the origin point for the object's sprite is set at (0,0))

(Replace enemy.sprite.height and enemy.sprite.width with whatever the height and width of the enemy's sprite is. If the sprite_index is the exact same sprite for what's being shown on screen then I'd advise using sprite_get_width(obj_enemy.sprite_index))

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.