Game Development and Procedural Generation

Michelle
4 min readSep 12, 2019

What the heck is it?

Pulling straight from Wikipedia — procedural generation is a method of creating data algorithmically as opposed to manually, typically through a combination of human-generated assets and algorithms coupled with computer-generated randomness and processing power.

Whoa! But what does that mean in layman’s terms, or better yet what does that mean in game development?

The procedural part, simply put, is the step-by-step process that is defined by a developer or designer. At the end of the process, unique content is generated.

In terms of gaming, this content could be an NPC, a dungeon, or a map with varying terrain.

Screenshot of No Man’s Sky game

Example

Let’s take for example, an NPC.

When designing a game, a large number of NPCs may be needed to populate a world. An NPC may have a name, a gender, an occupation, and hobbies.

To manually create an NPC, a game may have some code that looks similar to this:

karen = Npc.new({name: “Karen”, gender: “female”, occupation: “Soccer Mom”, hobbies: [“wine”, “speaking to the manager”]})

This method of creating content in any game would work, but now imagine if 5 NPCs were needed to populate a game world. What if 10 were needed? What if 100 were needed? What if 1000 were needed?!

Imagine having to type out the above snippet of code 1000 times. Here are some words of wisdom from Sweet Brown.

“Ain’t nobody got time for that!”

Very quickly, this task would not only be time consuming, but incredibly repetitive.

With procedural generation, creating 5 or 1000 NPCs can be as easy as one line of code.

Code creating 5 NPCs can look something like this now:

5.times do 
Npc.new(Npc.generate_npc)
end

There are things happening within the generate_npc class method that add values to the attributes defined above(name, gender, occupation, and hobbies), but that is where the procedural part of procedural generation comes in and that is also the part that developers and designers define.

Take a look at the code below:

class Npc  
attr_accessor :name, :gender, :occupation, :hobbies
def initialize(attributes)
attributes.each do |attribute, value|
self.send("#{attribute}=", value)
end
end
def self.generate_npc(gender = nil)
gender = gender || self.generate_gender
Npc.new({
gender: gender,
name: self.generate_name(gender),
occupation: self.generate_occupation,
hobbies: self.generate_hobbies
})
end
def self.generate_gender
["Male", "Female", "Non-binary"].sample
end
def self.generate_name(gender)
case gender
when "Male"
["Tom", "Robert", "John"].sample
when "Female"
["Lauren", "Becky", "Heather"].sample
else
["Sam", "Taylor", "Alex"].sample
end
end
def self.generate_occupation
["Firefighter", "Teacher", "Game Developer"].sample
end
def self.generate_hobbies
["Speaking to the manager", "Facebooking", "Bingo", "Video Games", "Eating"].sample(2)
end
end

Why are these examples in Ruby? Check out my article: 5 Reasons To Build Your First Game In Ruby as a Beginner Game Developer.

This code is dynamic in a way that it will allow manual creation of an NPC, like in the example above where a Karen was specifically created.

It also allows procedural generations of NPCs that is both quick and systematic.

Let’s break down the code a bit in the generate_npc class method and go line by line.

def self.generate_npc(gender = nil)    # the gender argument is optional, if no argument is given
# the generate_gender method is invoked, and that method
# will return a randomly chosen gender
gender = gender || self.generate_gender Npc.new({
gender: gender,
# Because we now have a gender, we can invoke another
# method called generate_name, which will choose a name
# according to gender
name: self.generate_name(gender), # generate_occupation and generate_hobbies are both
# invoked to generate random values for both these keys
occupation: self.generate_occupation,
hobbies: self.generate_hobbies
}) # The completed hash is now passed into the Npc.new initialize
# method, and an NPC is generated
end

Notice how gender needed to be clearly defined first before a name for the NPC could be generated. The name which would be chosen is based on gender. This is a specific step in the step-by-step generation process. This would not work if the generate_name method was invoked first, because it calls for a gender argument.

In Action

Below are 5 NPCs generated with our generate_npc class method:

=> [#<Npc:0x00007fa1c5386300
@gender="Non-binary",
@hobbies=
["Facebooking", "Speaking to the manager"],
@name="Alex",
@occupation="Teacher">,
#<Npc:0x00007fa1c5385e50
@gender="Male",
@hobbies=["Facebooking", "Eating"],
@name="John",
@occupation="Firefighter">,
#<Npc:0x00007fa1c53859a0
@gender="Female",
@hobbies=
["Facebooking", "Speaking to the manager"],
@name="Lauren",
@occupation="Police Officer">,
#<Npc:0x00007fa1c53854f0
@gender="Female",
@hobbies=["Facebooking", "Video Games"],
@name="Becky",
@occupation="Teacher">,
#<Npc:0x00007fa1c5385040
@gender="Non-binary",
@hobbies=
["Facebooking", "Speaking to the manager"],
@name="Taylor",
@occupation="Firefighter">]

Amazing! All five are unique with their own attributes. There are some overlapping common hobbies, but for more variety, the pool of data which is pulled from should ideally be larger.

Benefits

  1. Quickly create content to populate game world
  2. Unique content generation
  3. Replay-ability factor, each play-through is different

When To Use

Whether or not procedural generation will work for a game will depend entirely on the type of game and the experience a developer wants a player to have.

Good example:

FPS where the goal is to shoot as many enemies as possible in X amount of time. Procedural generation would work for generating random enemies with random graphical attributes or health points

Bad example:

A graphical novel that has a set number of characters with their own specific storylines/backgrounds

Mix-it-up:

An RPG where there are characters a player receives quests from that have set names, set inventories, etc, but also has procedurally generated dungeons and mobs within

More Info

Major games like Minecraft and No Man’s Sky use procedural generation to create content and it can be as simple or complex as a designer or developer wants it to be.

For more in-depth information, check out this amazing talk at GDC(Game Developer’s Conference) by Kate Compton. It’s packed full of information from someone extremely qualified to talk about procedural generation.

Procedural Generation talk at GDC by Kate Compton

--

--

Michelle

Software Engineer who calls her mom every day, cares about diversity and inclusion, and eats frozen blueberries. She/Her.