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

Need help with random spawning planets

T

ThatAussieKiwi

Guest
Hello,

I would first like to say I'm am very new to GameMaker so I am sorry if this is something very simple. So what I want to happen is for the game to randomly select a planet out of 3 designs and then randomly spawn it into the game every minute or so. I know from browsing the forums that people normal post their code as a starting point but I am just clueless on where to start.

Thanks for taking the time to read this and helping me🙂
 
Hello,

I would first like to say I'm am very new to GameMaker so I am sorry if this is something very simple. So what I want to happen is for the game to randomly select a planet out of 3 designs and then randomly spawn it into the game every minute or so. I know from browsing the forums that people normal post their code as a starting point but I am just clueless on where to start.

Thanks for taking the time to read this and helping me🙂
Hi ThatAussieKiwi, are the planets in 3 different objects or in one with different sprites images?
You can easily spawn every x minute by using the built-in GMS alarm system.
Here's the documentation:

Here's a quick code implementation:
GML:
//STEP EVENT =======================

// If the alarm is off, then turn it on
if (alarm[0] == -1) {
    // Set alarm 0 to 1 minute
    alarm[0] = room_speed * 60;
}

// ALARM[0] EVENT =================================

// Choose a random x and y position - in this case a random spot for the whole size of the room
var xPos = random(room_width);
var yPos = random(room_height);

// Two methods:
// 1) In case you have one object for the planets
var planet = instance_create_layer(xPos, yPos, "Instances", oPlanet);
planet.image_index = choose(0, 1, 2);

// 2) In case you have three different objects for each planet
var planet = choose(oPlanet0, oPlanet1, oPlanet2);
instance_create_layer(xPos, yPos, "Instances", planet);
 
T

ThatAussieKiwi

Guest
They would be three different objects/imported images. Also, would I also use alarms to slow how fast my player will shoot a bullet. For example, would I use an alarm to delay the bullet from spawning by 0.1 or so milliseconds?

Anyway, thank you so much for the reply!
 

FrostyCat

Redemption Seeker
I strongly suggest that you walk through FriendlyCosmonaut's Space Rocks tutorial to pick up the basics you missed, and use the Manual's GML Overview section to learn the language. The part for picking random sprites is covered at 12:57 (Part 2), and capping the rate of fire is the same idea with alarms:

Create:
GML:
canFire = true;
fireInterval = room_speed/10;
Step:
GML:
if (keyboard_check(vk_space) && canFire) {
    /* Insert shooting code here */
    canFire = false;
    alarm[1] = fireInterval;
}
Alarm 1:
GML:
canFire = true;
 
T

ThatAussieKiwi

Guest
Thank you so much for the reply, I will go and watch that tutorial now🙂
 
P

PyxelJock

Guest
Another way to do an internal alarm is below, so you don't even need to drag an alarm into your instance. This way you can also keep track via naming convention of each alarm other than alarm[0]. alarm[1].

GML:
//Create
MakePlanet = 0;//Create timer, starting at 0 makes the timer able to fire off immediately

//Step
MakePlanet --;//Reduce timer by 1 each frame/room speed

If(MakePlanet <= 0){//Check If timer is less than or equal to 0
    //Do some code here, or create planets.
    MakePlanet = 100;//Reset Timer
}
 
T

ThatAussieKiwi

Guest
Thank you for explaining another way but just from looking at the first one that was recommended it makes more sense to me but both are still very helpful and I will try both and see which one works the best🙂
 
Top