Randomizing Spawn throughout the game [SOLVED]

F

Fyre

Guest
Hello!
For my game I'd like to spawn cars in randomized places (either a or b), randomized gaps between the spawns and then also randomized speed (altough I havent written the code for that yet)
However, I encountered the issue that it is random at the start of the game but then it continues to continue with the same values. (Always spwaning at the same place in the same frequency during the run of the game)

In the create Event:
Code:
randomize();
{
alarm[2] = random_range(1,6) * room_speed;
spawnrate = random_range(60,200);
alarm[0] = spawnrate;
spawnplace = irandom_range(1,2)
}
Alarm[0]
Code:
if(spawnplace %2 == 1)
{
instance_create_layer(-100, 270 , "CarLayer", obj_car);
}
else
{
instance_create_layer(-100, 510 , "CarLayer", obj_car);
alarm[0] = spawnrate;
}
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Add a
Code:
randomize();
prior to first random_* call for the values to be randomized each run.
 

TheouAegis

Member
Add a
Code:
randomize();
prior to first random_* call for the values to be randomized each run.
He does have a randomize() in his create event.

Curious what is in his alarm[2] since he went through the trouble of randomizing that as well.

if(spawnplace %2 == 1) { instance_create_layer(-100, 270 , "CarLayer", obj_car); } else { instance_create_layer(-100, 510 , "CarLayer", obj_car); alarm[0] = spawnrate; }
You need to move the alarm[0]=spawnrate OUTSIDE of the brackets.

spawnplace = irandom_range(1,2)
Why not just use irandom(1) here?
 
B

Bayesian

Guest
You need to move the alarm[0]=spawnrate OUTSIDE of the brackets.
You also need to recalculate the value of spawnrate and spawnplace so it can be different each time. Setting it in the create event only runs once when the object is created so it will continuously use the same values.
 
F

Fyre

Guest
Curious what is in his alarm[2] since he went through the trouble of randomizing that as well.
To be honest, I just noticed its unecessity when I copied my code here, I guess I tried something out and forgot to delete that too. Not sure anymore but its gone now anyway.

You need to move the alarm[0]=spawnrate OUTSIDE of the brackets..
Okay, I did that.

Why not just use irandom(1) here?
irandom_range made more sense for me when I've read it up, Im really new to programming. ^^'
 
F

Fyre

Guest
You also need to recalculate the value of spawnrate and spawnplace so it can be different each time. Setting it in the create event only runs once when the object is created so it will continuously use the same values.
Ah, I see!
Then which event would be suited better?
I'd say step or draw..
Im sorry, Im really new to Game Maker and programming in general >_<'
 
Last edited by a moderator:
B

Bayesian

Guest
Then which event would be suited better?
Like TheouAegis said put it inside the alarm outside the brackets

Code:
if(spawnplace %2 == 1)
{
instance_create_layer(-100, 270 , "CarLayer", obj_car);
}
else
{
instance_create_layer(-100, 510 , "CarLayer", obj_car);
}

spawnrate = random_range(60,200);
alarm[0] = spawnrate;
spawnplace = irandom_range(1,2)
 
Top