Randomly spawn actor between 1 and 5 seconds. I have no Idea!
Hi! So, I'm trying to make a game with cars driving down a road that you have to avoid. I would like 10 lanes of cars, but right now I'm just working with one. I would like the cars to spawn randomly between 1 and 5 seconds apart, and more down their lane and off the screen. I have the car movement down perfectly, I just cant figure out how to create random spawning. The timer picks one value and sticks with it, which will not work in this game. I have a weird method involving my own version of a timer which I will upload, but it doesn't work. I'm stuck, please help me out. My entire game hinges on the ability to randomly spawn cars. Project File | Mirror
P.S. The artwork is just placeholder from another app. My artist (sister) is making custom pixel art for the app, and it will look 10000x better than it does right now! (It will even have a parallax background!)
Comments
@ex0syphen Okay so the best way I have found to deal with something like that is to remove the spawn actors and create a spawner that spawns the spawner. Each lane should obviously be there own variable and have the spawn time change when spawned that should give you cars spawning every 1-5 seconds. If you understood what I meant great if not just say so and I will try to answer as clearly as I can.
@klickink I sort of understand... but not really. Elaborate on the spawn time variable please.
Two possibilities I've used:
1) Create a spawn actor. You then set a attribute on the spawn actor called spawnTime. Set spawnTime to game.Time + floor(random(1,5))
Then check to see if the game.Time >= spawnTime, if it is, spawnactor and reset the spawnTime to game.Time + floor(random(1,5))
2) Do the same thing, but with the car actors. As each heads off the screen, set an active attribute to false, setthe coordinates to the other side of the screen (or wherever) just off screen, then a timeToWakeup attribute to game.Time + floor(random(1,5))
When game.Time > timeToWakeup then set active to true and enable your movement
I've personally switched to using the 1st method because everything is encapsulated and I know where all the spawning is happening.
I use it in my games - Zachary vs Aliens and SuperParker to spawn both the small monsters and the bosses
@AndrewAustin Something like this is what I meant but understand this is how I figured out how to fix that problem. There are usually several ways to get around a problem and this might not be the best one. This is similar to what I have used in the past but @ookami007 solutions could be what you want to use.
If its not clear the "spawn the spawner" actor is the only one you would put on the scene.
Create an integer game attribute ("RND")
Rule for the Spawner:
Timer - Every RND seconds
--Change Attribute RND to random (1,5)
--Spawn: CAR
--Destroy
--Spawn: Spawner