Creating an automated level generator

Content is always a huge deal in games. And with content I mean everything: story, items, power-ups, and of course, levels.
Levels are extremely hard to make. Depending on the type of game you are making, you may be able to create random levels automatically. This was the case in our CrazyColors game.
However, chances are you’ll need to create your levels manually. We do on Spell Run.
But, we can do some coding to have a script that creates the barebones structure of a level for us, from which we can continue, improve, tweak and publish; instead of starting from scratch.
If you did things right on your game, you should have accessible functions to the same actions the player will perform during the game. In our case, such actions involve, mainly, tile creation.
In our game, the level has some pre-existing tiles with pickups.
Those pre-existing tiles are the same the user can create while playing: straight, left turn or right turn.

Player and pre-existing tiles can be a left tile, a straight tile, or a right tile

We have encapsulated tile creation into a function we call and specify the type of tile we want to create:

voidCreate(TileTypetileType);

One thing we can do is, instead of manually dragging and placing each tile we want to place, we can create a script that runs on the Unity Editor, and simply use the arrow keys to insert the tiles and create the path.
That way, we would be “creating” the level by using the arrows keys. Here’s a sample on how to accomplish that:

In a lot of cases, that alone is enough to save time to create the structure. But for Spell Run, we would have to delete most of the tiles, because we only want a few scattered through the level. That’s the key of our game!
So we need to create a smarter script. We want our script to:

Create scattered tiles through the level

Allow us to define the extension of the level

Allow us to tweak the level’s difficulty

So, here’s the pseudo code of what we want:

Create the first initial tile at position (0,0, 0)

Create the level exit at a random position very close to the end of the level

Define a random number of tiles our level will have and iterate that number

Get a random position to place the tile

If that position is already used, get another one until its safe to place

Place the tile

Move our position to the last tile placed

That’s it. When we code that, we’ll have the structure of our level already done, from which we can build upon and tweak.
Automated level generation is used in many, many games either during runtime (to randomize a level), or during edition, to further tweak the levels and ensure they are usable. RTS games generate random levels all the time, with some parameters to ensure balance if they are multiplayer.
Think of a way to do it in your game and save tons of hours of work.
In our case, once we run this level generator, we then tweak the level and also play it to make sure it’s beatable! Because if it’s not, we need to generate another or add more tiles to allow the user to reach the exit.
Level testing (as general testing also) is also a huge deal on games. You need to test for bugs, successfulness, enjoyment, etc.
However, in our next chapter, we’ll talk about how to create a script that will play the level for us and give us a rough sense if the level is, at least, beatable or not.
That way, at least, we can re-create the levels that our script tells us are not beatable, instead of play them 20-30 times until we realize they cannot be beaten.
Don’t forget to follow us on twitter for news regarding articles and game development [twitter-follow screen_name=’indelvestudios’]