Runner Random Spawner

brett-nortonbrett-norton Member, BASIC Posts: 64

Hi All,

Im currently making a sidescroller with a little cool twist. and im trying to achieve a simple random obstacle spawn code. The hero actror will have 0 velocity X and obstacles will be moving Velocity X -600 towards the actor.

Im planning on 3 types of obstacles "Low jump" High double jump" and "Slide Under" each actor will have 3 images so its not visually the same obstacle.

I would like a random selection to choose between actor Low / High / Slide and then choose one of the 3 images for that actor

And then Increase the Difficulty to a quicker Random selection.

Im really struggling to lay the foundations to this code could anyone help me with the best way please. Also im guessing maybe a table would be better suited?? I have 0% XP with tables but will some work into them if thats suggested.

Thanks in advance.


  • SocksSocks London, UK.Member Posts: 12,822
    edited May 2017

    Here's one approach, random obstacles - each with one of three images - obstacles gradually speed up.

  • imjustmikeimjustmike Member Posts: 450
    edited May 2017

    Not 100% sure what you mean by a quicker random selection? I assume you mean that the space between the obstacles is shorter? Or that they're moving quicker?

    Depends if you want it random and increasing in difficulty or to have 'patterns' that get harder over time. Both are easy to do.

    To randomly change images, do the following.

    Name your images low_jump_obstacle_1.png, low_jump_obstacle_2.png etc (repeat for the three obstacle types.

    Create your low jump obstacle actor.

    Add a self integer attribute, call it id. We'll use this to work out what image to display.

    Add a change attribute, and set to random(1,3).
    Add a second change attribute to change self.image to "low_jump_obstacle"".png"

    _These two behaviours together set a random number which then determines which image to to display. _

    Duplicate this actor and change the behaviours so they work for the other obstacle types.

    Random method for spawning
    Create a global integer attribute called difficulty.

    Create a spawner actor.

    Within the actor create a self integer attribute called obstacle_type

    In the actor create a rule that says when difficulty < 10

    Within that rule, add a timer that says every 4 seconds

    Within the timer add a change attribute that changes self.obstacle_type to random(1,3)

    Then, also within the timer, add a nested rule that say if obstacle_type = 1, spawn the low jump actor, otherwise, if obstacle_type = 2, spawn the double jump actor, otherwise if obstacle_type = 3 spawn the slide actor.

    Finally, within the timer, add a change attribute difficulty to difficulty + 1

    This combination of rules and behaviours will, for a given difficulty (in this example, under 10) spawn a random obstacle every 4 seconds and increase the difficulty. You can duplicate this rule set for different difficulty bands, for example, you could have it so after every 10 obstacles it gets harder. Or every 20. Obviously, the more frequent the difficulty bands increase the more rules you need. That's the downside to this approach - it's super quick to create but can get VERY messy with lots of rules. You could make this simpler with custom timers etc, but this should be enough for a quick prototype. If you wanted to make the obstacles move faster, you can constrain the speed to a multiple of difficulty, as it goes up, so will their speed

    Table method for spawning

    Create a global attribute called row

    Create a table with a column of integers. Give it say, 100 rows. On each row randomly type 0, 1, 2 or 3. Make sure you have many more 0s than any other number.

    Create a spawner actor.

    Create a self integer attribute called self.obstacle_type

    Add a timer that says every 1 seconds

    Add a change attribute that changes self.obstacle_type to tableCellValue(NAME OF YOUR TABLE,global.row,1)

    Add a nested rule that say if obstacle_type = 1, spawn the low jump actor, otherwise, if obstacle_type = 2, spawn the double jump actor, otherwise if obstacle_type = 3 spawn the slide actor, otherwise nothing.

    Then add a change attribute that changes global.row to global.row+1

    These behaviours will, every second, spawn whatever is on the current row, then change the row. If there is a 0 nothing will spawn. This is how you control the difficulty, placing more 0s to give the player more time. The upside to this is that you can play around with what is spawned, so if you want three low jumps in a row, you can. The downside is that you have to plan out the game in advance. If you want more than 100 obstacles, you can obviously change the number of rows to a higher number, but there is a cap on the row count. If you're going to reach that, simply add use another column and add some rules to determine which column to pull from based on the global.row attribute.

    Like the previous method, you could constrain the obstacle speed based on a multiple of the global.row attribute, as it goes up so will the speed.

    For BONUS points you could actually combine both methods, and use tables to create 'patterns' of obstacles, and then randomly (or non-randomly) select which pattern to spawn. The upside is that with a couple of different patterns you can create a game that feels different each time you play, and different throughout the game without having the plan every single obstacle in advance.


    There are probably some errors here, so don't follow it religiously and play around with it until it works, but hopefully a good starting point.

  • brett-nortonbrett-norton Member, BASIC Posts: 64

    Thanks guys that sure answered my question, Now i can get back to work. :) Really appreciate your help. If you ever need a large concrete structure im your go to man ha.

Sign In or Register to comment.