Implementing Procedural Generation

This page only focuses on implementing not on how a noise function works.

This page is unfinished and under-construction. Additionally the calc extension has been replaced with the extended math support extension.

You want to have some kind of high-level overview description here. This should be a 1-2 line summation of this entire article.

Part 1: Creating a Grid

To do procedural generation, first there must be something to generate. Assuming the goal is to generate terrain, this is where the grid comes into play. It will hold all of the terrain pieces.

Step 1: Creating the Grid Objects

The following steps will help prepare the grid objects, which will be used as part of the generation.

  1. Within the scene, create a new sprite object and give it a name.
    • In this example the object will be called origin, as it will serve as the start of the procedural world.
  2. Create a second object, and give it a name.
    • For this example the object is named tile, as this object will be what is placed to make the grid.
  3. For both objects' animations, give them a black square.

Step 2: Creating the Initialization Events

We now need to set up the initial events to prepare terrain generation.

  1. Access the event sheet.
  2. Add a new event.
  3. Click Add condition.
    1. Within the condition list, select At the beginning of the scene.
    2. Click OK.
  4. Within the same event, click Add action.
    1. Search for Scene Variable.
    2. Select Value of a scene variable.
    3. Within the Variable field, type x.
    4. Within the Modificaiton's sign, select =.
    5. Within the Value field, type 1.
    6. Click OK.
  5. Repeat all of #4 above, but with a Variable name of y.
    • These variables will handle the placement of tiles on the x and y axis. The events should look like this once complete:

Step 3: Create Events to Generate Tiles in a Column

Next a set of actions should be set up to allow testing tile generation.

  1. At the bottom of the event sheet, click Add a new event.
  2. Within the new event, click Add condition.
    1. Search for released.
    2. Within the results, select Mouse button released.
    3. In the Button to test field, select Left (primary).
    4. Click OK.
  3. Within the action field, click Add action.
    1. Search for create.
    2. Select Create an object from the list.
      • Note: If using the new condition/action editor, the tile object may need to be selected from the list before the normal Create an object action is avaliable
    3. Within the Object to create field, ensure that tile is selected.
    4. In the X position field, type 32.
    5. In the Y position field, type 32 * Variable(y), this will automatically populate the value of the scene variable Y.
    6. Click OK.
  4. Within the action field, click Add action.
    1. Search for Scene Variable.
    2. Select Value of a scene variable.
    3. Within the Variable field, type y.
    4. Within the Modificaiton's sign field, select +.
    5. Within the Value field, type 1.
    6. Click OK
  5. The final result should look like:
  6. Preview the program. If the above steps were completed successfully, when the left mouse button is clicked a new tile should be created in a column.
  7. However, the column keeps going on down off the screen. What would be ideal is if once the column got to a certain size it created a new column.
    • An example of how it should behave currently:

Step 4: Create Events to Generate Tiles in a Rows and Columns

Creating new columns

  1. In the first event click add action.
    1. Search for Scene Variable.
    2. Select Value of a scene variable
    3. For the Variable field enter sizeX
    4. Change the modification's sign to =
    5. Change the Value field to 4
  2. Repeat all of step #1 above, but change the variable name to sizeY
    • These variables will control the size of the grid on the x and y axis.The event should look like this once complete:
  3. Right click on the create tile event.
    1. Then select Add Sub Event.
  4. Within the the sub event, click Add condition.
    1. Search for Scene Variable.
    2. Within the results, select Value of a scene variable.
    3. In the Variable field, select y.
    4. In the Sign of the test field, select > (greater then).
    5. finally, in the Value to compare field, input Variable(sizeY).
    6. Click OK.
  5. Within the action field, click Add action
    1. Find Scene Variable.
    2. For the Variable field, select y.
    3. For the Modification's sign field, select = (set to).
    4. For the Value field, input 1.
  6. Add another Change the scene variable action
    1. In the Variable field, select x.
    2. In the Modification's sign field, select + (add).
    3. In the Value field, enter 1
    • Now the grid is incremented in the x dimension
    • The events should look like the following image:

Step 5: Making the Grid the Right Size

This next set section will add the last event to prevent the grid from going too far on the x-axis.

  1. Right click on the scene variable y > Variable(sizeY) event and click Add Sub Event.
    1. Click Copy and then Paste the event.
    2. Move the new event and make it a Sub Event of the original event.
  2. Select the condition.
    1. Change the Variable field to x.
    2. Keep the Sign of the test field the same.
    3. Change the Value to compare field to Variable(sizeX).
  3. Select the Change the scene variable y action.
    1. Change the Value field to -1.
  4. Select the Change the scene variable x action.
    1. Change the Modification's sign field to = (set to).
    2. Change the Value field to -1.
  5. Select the Left mouse button was released event.
    1. Select the Create object action
    2. Change the X position field to 32 * Variable(x)

Step 6: Speeding up Tile Placement

Placing tiles by hand is good for testing, but for a game tiles need to be placed quickly. To do this a while event is used.

  1. Click the Add button at the bottom of the event sheet.
    1. Select the While event.
  2. UnderWhile these conditions are true, click add condition.
    1. Find Value of a scene variable.
    2. Within the Variable field, type .
    3. Within the Sign of the test field, select > (greater then).
    4. Within the Value to compare field, type 0.
  3. Repeat all of the above step, but change the variable y.
  4. Cut the actions from the Left mouse button was released event and paste it into the while event.
  5. Drag The scene variable y > Variable(sizeY) event and make it a sub event of the while event.
  6. Lastly, delete the Left mouse button was released event.

Step 6: Controlling Grid Placement

This section covers changing where the grid is created from.

  1. Select the while event.
    1. Select the Create object tile at position action.
    2. Change the X position field to origin.X() + 32 * Variable(x).
    3. Change the Y position field to origin.Y() + 32 * Variable(y).
    4. Place the origin object in the scene.

Part 2: Adding Noise

Step 1: Understanding Noise

Understanding how noise works is important, but not necessary to doing procedural generation. For those interested in knowing more read this.

Step 2: Setting the Noise Seed

  1. Click Add a new event,
    1. Drag the new event and place it above the while event.
  2. Click Add condition.
    1. Search for At the beginning of the scene.
    2. Once found select it.
  3. Then click Add action.
    1. Search for the Set the seed used for noise generation.
    2. Once found click it.
    3. Set the seed value to two.
    • The seed can be any value between 1 and 65536, this makes for 2¹⁶ different seed combinations.
The noise seed only applies to the current scene. If you're doing procedural generation in more than one scene you must set the noise seed for each scene.

Step 3: Displaying the Seed

This section covers how to display the seed using the currentSeed() expression.

  1. Go to the scene view and create a new text object.
    • In this example it will be called seed and will hold the current seed.
  2. Be sure to place the object into the scene.
  3. Move back to the event sheet.
  4. At the bottom of the event sheet find Add event and click it.
    1. Drag it above the while event and let go.
  5. Click Add condition.
    1. Within the condition list, select At the beginning of the scene.
    2. Click OK.
  6. Then click Add action.
    1. Search for Modify the text.
    2. For the Modification's sign, select = (set to).
    3. Set the valuefield to “seed: ” + ToString(Noise::currentSeed()).
    • The currentSeed() expression always holds the current seed for the scene.

Step 4: Applying Random Values to the Tiles

In order to visually see the noise values, the opacity of the tile must be changed creating a grayscale version of the procedurally generated world.

  1. Change variable sizeX and sizeY to both be set to 16.
    • The grid created will now be 16 tiles by 16 tiles for a total of 256 tiles.
  2. Click on Add action in the while event.
    1. Find theChange sprite opacity action.
    2. Within the Modification's sign field, select = (set to).
    3. Within the Value field, enter RandomInRange(0, 255).
    • Opacity is measured on a scale of 0 to 255 with 0 being fully transparent and 255 being fully opaque, which is why the random number must be between 0 and 255.

Step 5: Understanding Randomness

Run the program.

  • Assume the lighter a square is the lower its is and the darker it is the higher it is.
  • Notice how each square does not smoothly transition to its neighbouring tiles.
  • This is because random values are generated without consideration for the values generated before or the value that will be generated after.
  • This is why noise must be used instead of random.

Step 6: Implementing Noise

To get a smooth gradient of opacity randomInRange() must be swapped out with a noise function. For this example noise::perlin2() will be used.

  1. Select the Change the opacity action.
    1. Change the Value field to Noise::perlin2(Variable(x) / 10, Variable(y) / 10).
    • Noise functions take coordinates instead of min and max values like random.
    • Variables x and y must be divided by 10 for the function to work correctly.
  2. Run the program, notice how no tiles are visible.
    1. Change the value field to the following ExtendedMath::Map(Noise::perlin2(Variable(x) / 10, Variable(y) / 10), -1, 1, 0, 255).
    • The tiles are not visible because, a noise function returns a value between -1 and 1, but opacity is measured between 0 and 255.
    • For more information on the map function read this
    • Run the program and notice how all the tiles smoothly transition between each other and look like they belong.

Step 8: Creating Different Worlds

  1. Select the set noise seed action.
    1. Change the value field, to a randomInRange(1, 65536).
  2. Run the program a few times to see some examples of procedural generation.

Part 3: Colouring

The last step in procedural generation is to take the black and white grid, and then add colour to it.

To keep the code organized the last two steps have been placed into a group. This step is optional.

Go to your tile object and add animations for each type of terrain wanted. For this example blue, yellow, green, and grey animations will be added. To represent water, sand, grass, dark grass and mountains. But, any colour or sprite could be used. Then duplicate the tile object and name it colour. Change the first animation (the black one) to a transparent square of the same size.

Back in the event sheets, goto the create tile event and copy the create tile action and paste it right below that. Then change the object to colour.

Next, create a new event then give it the condition of the left mouse button released. Then, create a sub event on the mouse button released event with the condition of pick all tile objects and the opacity of tile is < 127.5. Finally, add an action to change tile animation to 1.

Now when you run the program the lowest parts of the grid are coloured blue. There are many improvements that can be made to this event to improve the look. First, add a pick all colour objects condition underneath the pick all tiles condition. Then, adding a colour is in collision with tile condition.

Copy and paste the sub event 3 more times. For the two middle events add an additional opacity of tile condition. Change the first sub event’s opacity to < 64.75. For the second sub event change opacity to > 64.75 and < 127.5. For the third > 127.5 and < 191.25. For the last sub event change the opacity to > 191.25.

Then change the object in the change animation action from tile to colour. Also, for each sub event set the animation one higher than the event above it. Lastly, add a new action to delete the tile object for each event.

Run the program a few times. A large amount of the world’s generated end up being made up of the green and yellow colours. Even though only 1 half of the tile opacity should be blue or grey. This is because noise functions tend to return many values around 127.5 and very few around 0 and 255.

An easy solution to this is to half the range of the two middle sub events and double the range of the first and last sub events. By adjusting what part of the opacity will make what colour you can create very different types of worlds.

Now when the program is run a more balanced world is created.

If you do not like the look of the solid colours an easy way to implement shading is to remove all the delete tile actions and replace it with a change tile animation action to the same number as the animation action for the colour object.

Lastly, remove the left mouse button release condition and replace it with variable x < 0 and variable y < 0 conditions make sure to also add a trigger once condition. Now the colouring will happen automatically.

Conclusion

This concludes the tutorial on implementing procedural generation in GDevelop.

The code and assets used can be found here