Categories
personal blog procedural generation series tutorial

Procedural Generation in GMS #1: An Introduction

This is the first entry in a series about creating procedurally generated content using GMS 2.3.


THE ENTIRE SERIES

More to come…


Note: None of the code for any of these proc-gen tutorials is highly optimised. My goal was to get the concepts across as simply as possible, rather than to make them run as fast as possible. Use the following as a springboard for further learning and experimentation, rather than as the best solution possible.

Also, if you are a complete beginner, it’s probably best to start at the first entry and work your way through, as further into the series we get, the more knowledge from previous entries is assumed.

If you like this free content, please consider wishlisting my game, Alchementalist on Steam! It really helps me with the Steam algorithm!


In my badly lit and overly stuffy coding space, I’ve run into many problems while coding but none more so than the black arts of procedural generation. However, I have spent quite a lot of time slowly working out these problems and I’m determined to find a way that my disgusting proclivity towards this dark magic actually becomes useful to the world in general.

So I’ve decided that I’m going to do a tutorial series on procedural generation in GML using GMS 2.3, to teach what I’ve learned in my winding and precarious path towards making games.

Proc-gen is one of my favourite areas of game design; it’s complex, mysterious, difficult and yet, very rewarding. Hopefully by the end of this series you’ll have the skill-set you need to branch out on your own and start making all sorts of interesting games in GMS2.

A note to begin with: I’m going to be working entirely in GMS 2.3 and taking full advantage of the new features that GML has in regards to that version. This means, while it’s not impossible to convert the code to a backwards compatible version, it will be very difficult unless you are quite confident working with GML.


Contents

WHAT IS PROCEDURAL GENERATION?

WHY IS PROC-GEN?

WHEN, EXACTLY, SHOULD WE USE PROC-GEN?

THE NEXT STEP


WHAT IS PROCEDURAL GENERATION?

Procedural generation (or proc-gen) is all about getting the computer to build things for you. This could mean the game map, items, backstories, even the entire game and rules itself. You do this by taking advantage of the fact that computers can generate “random” numbers (or rather, pseudo-random numbers, to be more precise, but I’m going to call them random from now on for brevity’s sake) and using these numbers to let the computer make decisions.

The most basic possible example of proc-gen would be something like this:

var _roll = irandom(1);
if (_roll == 0) {
	// Do something
}
else {
	// Do something else
}

That right there…That’s procedural generation. An extremely simple example, but it is procedural generation nonetheless. By using and manipulating random numbers in a variety of ways, we can make the computer build some rather complex things with no input from the developer (well…rather, a lot of front-loaded input and then “no” input after that).


WHY IS PROC-GEN?

So why would we want to make the computer build things, rather than us as designers? The most common answer to this is: content.

By using procedural generation, we can give a game a lot more bang for it’s development buck. Let’s take the example of a solo indie dev (something I’m rather familiar with). You, as a solo developer, will only be able to create X different weapons for your game before you have to release it and earn some moolah. Or you might only have time to develop a world that is X size. X here depends on how hard you work and how much time you have, but it is always pretty finite. A proper application of procedural content basically lets you create an infinite variety of guns, or an infinite world (“infinite”, so to speak, but definitely not actually infinite, in terms of player experience). By letting the computer make decisions, and mixing and matching those decisions together, you can increase the amount of content that the game can offer by a lot while maintaining roughly the same workload as if you weren’t using proc-gen.

There is a problem here though and one that is not trivial. You can think of a naive implementation of procedural generation as “the space of all possible combinations”. The unfortunate thing is that that space contains a lot of bogus content. You could randomly generate each pixel on the screen every frame and that’s a lot of content, but it’s not interesting or useful content. So part of the art of procedural generation is shaping the randomness into something that’s not only palatable to the average gamer, but juicy and taste-filled.

We can think of it as a series of nested circles:

We want all the things the computer builds to be within that black circle. No-one wants to play an infinite landscape if it’s the same town over and over again. No-one wants an infinite series of guns if the difference is that the ammo capacity changes. It’s the getting inside of that little black circle that is the trick.

So there’s a trade-off to be made when working in proc-gen. You can create a huge number of things quickly once it’s all done, but you need to spend the time working out what is interesting and how you can achieve that. You have to spend a lot of time tweaking and crafting algorithms, and building things for those algorithms to use, so that the content the computer spits out is interesting. Using proc-gen doesn’t necessarily reduce the workload you would normally spend working on a game, instead it allows that workload to stretch further in certain cases. Proc-gen is definitely not the be all end all of game programming. There’s plenty of things that don’t work with proc-gen (but there’s also plenty of things that only work with proc-gen). After saying all that, what proc-gen does is allow you to create more content within a specific time-frame, if used intelligently and crafted well.


WHEN, EXACTLY, SHOULD WE USE PROC-GEN?

So, we now understand what proc-gen is and what it isn’t. Let’s go over some of the actual things you might use proc-gen for. This is not an exhaustive list, by any means. In fact, you could probably procedurally generate things that you could use procedural generation for and spend a long time letting it run before it starts running out of things to generate. In any case, here’s a short list of some of the things we might wish to use proc-gen for:

Map Generation

This is the proc-gen classic. Building maps. Smart use of proc-gen gives you an essentially limitless scale of map creation. If you need more, just generate more. No Man’s Sky is an example of this. There is no way that any team in the world could have created that game without using procedural generation.

Items

We can take items and mutate/insert characteristics to create the potentially for almost limitless variation in items. Borderlands is a great example of this, with it’s purported 17,750,000 weapon variations. Impossible to create with any team.

Enemies

As goes for items, so goes for enemies. We can create a few characteristics for enemies and then build each enemy out of those characteristics. These might include enemy behaviour (is it aggressive, defensive, passive, easily scared, etc), weapons they are carrying, spells/abilities they may have, even the look of enemies if we want to drill down that deeply. The Middle-Earth: Shadow of Mordor games are a good example of this.

Goals

This one might seem a little bit weird, but by combining a few different techniques we can even build the goals of a level with some proc-gen involved. A simple example would be generating rooms inside a dungeon in a roguelike. We can keep track of when/where we generate a chest containing a key and then generate a door that that key opens afterwards, ensuring the key is always accessible before you hit the door, but the key/door combo is always found in a different place in each generated dungeon. If you abstract away what key and door means but keep the mechanic of first X then Y, this becomes a lot more interesting. An example of this (and an indie game) is Tom Francis’s Heat Signature, which procedurally generates the goals for each ship (along with proc-genning a bunch of other things).

And Many More…

As I said before, this list is by no means complete, there’s tons of things that you can apply proc-gen to in order to increase the longevity of the experience for the player. All it takes is some thought and clever application of numbers and basically anything in your game can become a part of the proc-gen umbrella.


THE NEXT STEP

Ok, so that was a lot to unpack as an introduction. I’m still working on the rest of the series and exactly how it should all fit together/what order I should go in, but I think we’ll spend the next segment getting comfortable with a few of the data structures and general coding outlines that we’re going to be using a lot for proc-gen work, then we’ll move on to the exciting step of actually starting to generate some things procedurally.

Read on with #2: Learning the Basics


Sign up to get notified whenever I upload a blog post, so you don’t miss any of the next installments!

By RefresherTowel

I'm a solo indie game developer, based in Innisfail, Australia.

Leave a comment