Endless runner: Spawn fixed tiles randomly

Hello there!

I’m creating an infinite runner game for my students, so they can learn in a fun way the subject matter for a test. I used to work with Buildbox, but I’ve changed recently to UE because UE has more possibilities. Therefore I’m rather new to UE.

I’ve created an endless runner based on the tutorials on Youtube, but the game always spawns the same ‘MasterTile’. For my game I need to spawn different fixed tiles (I have a folder with the blueprints of MasterTile (=empty tile), Tile1, Tile2…) The game needs to spawn a Tile from this folder at random.

Can someone help me please?
Thanks in advance!
Michiel


Can you be more specific? I see you spawning 9 tiles, and I see you spawning a tile on overlap, but where is the selection?

Hey there @Drumaholic-88! Welcome to the community! So it looks like you have a for loop that runs the spawn tile command 9 times as the scene begins. However, we’d need to see how you’re determining which tile is spawned.

If your question is how to easily pull that off, it’s more dependent on how much control you want over the generation. If you don’t care how it generates and just want to pick a random tile (that is a child BP of the master tile all customized), then in the constructor for the master tile you could then spawn a random tile out of an array. It would look a bit like this (ignore that the classes all share the same name)

If that wasn’t your question, let us know and we can clarify whatever you need!

Thanks for the quick response!

I’ve tried your solution and it works almost good. Just one problem:

I’ve made Tile1, Tile2… childs from the Master Tile and added your array.
When I test it out, my third person character runs off the tiles after a bit. (It runs automatically forward). Also some tiles who were already loaded suddenly change in another tile. (For example: I’m running and Tile1 appears 3 tiles further. But when I pass the Tile where I’m standing on, it suddenly changes to Tile2.

Here are the Nodes:

Thanks for Helping!
My students will appreciate it!
Michiel

Yep!

So 2 things are going to be important here.
One of the things with endless runners before you get too deep, is they usually don’t move the player, they actually usually move the tiles or they reset the origin to avoid an issue called floating point precision going nuts, especially for mobile platforms. The further from the center of the world you get the worse the game breaks. 0,0,0 is the most stable location to work from. “Reality” starts breaking and numbers stop being exact past a certain distance. Unreal has multiple ways around it, and I’ll show you how to move the origin as it’ll be fine in this instance and you won’t have to mess with. I’d set the world location every 3 -5 kilometers from the last origin. Set World Origin Location | Unreal Engine Documentation

Then, for your tiling issue. The way I see it in your script as is, you’re spawning them directly on the location of the Mastertile derived class that you spawned already, one solution is to make sure all the tiles are uniform in size (which I’m sure they are) and add an offset so that they spawn in front of the location of the last spawned one, not directly on top of it so you don’t get that overlap. So after that Get World Location node, you can add a specific distance that is the distance between your tiles connection points through an add node before your setting of the next spawn location.

That and as of now you’re only running the for loop 9 times, which I assumed you did to test things quickly then stop.

Dear SupportiveEntity

Thanks for the quick response. I’ve tried some things out but I don’t understand it completely how I have to do it exactly right because my ‘technical term English’ is not so good. :slightly_smiling_face:

Is it possible to draw it out in nodes like you did the first time?

For the loop issue. I thought I was generating 10 tiles at once at the start and everytime I collided with the collision box the last one was destroyed and a new one generated. This for infinite times.

Thanks in advance!
Michiel

I can definitely show you the direction to go in. Full implementation would look a bit different than the snippets I put together. I have to make sure I help as many people as I can so when I have to work out full BPs I have to work on quicker questions in between, so I’ll prepare a quick one for you soon! Soon being very relative!

Thank You!

Can’t it be solved by looping for infinite times? (How can I set infinite for max?)
I’ve deed a test bij setting the max for the loop to 20, 30… and then it works quite well. The tiles are random (in this example Mastertile, Tile1, Tile2 & Tile3.)

I will also make my tiles longer. In this testgame the tiles are 10x10x0.1. In the real game they more will be like 100x10x0.1.

I definitely don’t recommend true infinite loops (one because the engine will be angry) but two because every recursive loop needs a break case, be it physical (tile only spawning the next tile once the player is on it a la subway surfer), or some other stimuli.

Basically, you can of course just continue spawning them and turn it off whenever, but you also need to think about other mechanics, like if your character has to slow down or speed up, then you need to be able to influence the rate at which the tiles spawn. For the test I set up to show you that I didn’t finish yet, I’ve got them stretched out, and a collider at the front to know the player has landed and to spawn the next tile. This way no matter what, the tile will only spawn the next one when it’s needed. Should cut down on extra processing you’d need to keep a handful up.

I was also going to recommend you stretch the tiles out large like subway surfer’s scenes but you’re way ahead of that.