Godot 3.2 Guide To Build Your 2D Platformer Game – Part 3 (Spawning Platforms Dynamically)

This is the third part of the on going series on how to build a 2D Platformer Game using Godot Game Engine. If you miss the last part you can read it here. Godot 3.2 Guide To Build Your 2D Platformer Game – Part 2 (Creating a Platform)


Add a new folder inside the Scenes folder and call it Controllers. I call it controllers, because I will put the Scenes that can affect the game on this folder.

Inside the Controllers folder add new Scene called PlatformSpawner

Click the 2D Scene then rename the Node to PlatformSpawner.

Click the PlatformSpawner then click to add script icon

Click the folder icon and navigate to Scripts folder.

Then click Open

Click Create

Then we need to update the code in the PlatformSpawner to this

You can copy this code to PlatformSpawner.gd

extends Node

export (Array, PackedScene) var scenes

const X_MIN_DISTANCE = 100
const X_MAX_DISTANCE = 150

const Y_MIN_DISTANCE = -50
const Y_MAX_DISTANCE = 50

const INITIAL_X_SPAWN = 100

var spawn_x
var spawn_y

func _ready():
	spawn_y = get_viewport().get_viewport().size.y - INITIAL_Y_SPAWN_MARGIN
	spawn_x = INITIAL_X_SPAWN
	Signals.connect("create_new_platform", self, "create_new_platform")
func _spawn_initial_platforms():
	for counter in range(INITIAL_PLATFORMS_COUNT):
func _spawn_platform():
	var index
	var new_platform
	index = rand_range(0, scenes.size())
	new_platform = scenes[index].instance()
	var spawn_position = Vector2(spawn_x, spawn_y)	
	new_platform.position = spawn_position	
	var new_spawn_x = new_platform.sprite_width + rand_range(X_MIN_DISTANCE, X_MAX_DISTANCE)
	spawn_x = spawn_x + new_spawn_x
	var new_spawn_y = rand_range(Y_MIN_DISTANCE, Y_MAX_DISTANCE)
	spawn_y = spawn_y + new_spawn_y

	if spawn_y < 300:
		spawn_y = 300
	if spawn_y > 600:
		spawn_y = 600

func create_new_platform():


I know that’s a lot of code, but I will explain the code to you.

This is the first part of the code, we have variable declarations here.

The export (Array, PackedScene) var scenes will hold all the scenes or platforms that we want to generate to the screen.

Then I want to have a random distance between the platforms, that’s why I declare X_MIN_DISTANCE and X_MAX_DISTANCE. I also don’t want the platforms to be on the same level, that’s why I also randomize it using Y_MIN_DISTANCE and Y_MAX_DISTANCE. They can’t be too high from the previous one otherwise the player would not be able to jump from one platform to another. That’s why you need to be careful on this. I already did a trial and error here, that’s why I know this value will work.

Then we have the number of platforms that will generated when the game loaded. After that we have the initial position of the first platform. For the Y spawn, you might notice that I put margin. That is because we will calculate the position of spawn_y by subtracting the height of the screen by the INITIAL_Y_SPAWN_MARGIN.

The spawn_x and spawn_y will hold the value, on where we will render the platform.

When the scene was loaded, it will call the function _ready()

Here you can see that we set the value of spawn_x and spawn_y then call a function to render the initial platforms.

Then in the next line we connect to the signal that we emit earlier in our platform. The create_new_platform signal.

Then this function is responsible for creating the initial platforms.

Then this is the actual code to spawn a platform

The last one is the function that will be called when we received a signal for create_new_platform


Now, Time to test our PlatformSpawner. Go to Game Scene. Click the Game tab then click the 2D to go to grid view.

If you don’t have the Game scene open, then you can open it form the Scenes in the FileSystem

Click on this chain icon.

Then select the PlatformSpawner.tscn then click Open

Click on PlatformSpawner then in Inspector tab click the Scenes input which currently, should have Array(size 0)

Change the size to 2

Then drag the GroundGrass to empty field of the scenes input.

Do the same to GroundGrassSmall

You should now have this for PlatformSpawner Scenes input


Click on the play button

If this is your first time running your project it will ask you to select the main scene. Click Select

Select Game.tscn then click Open

You should have now something like this


I think they are too big. Open the GroundGrass scene. Make sure you are selecting the GroundGrass node. Then go to Transform in the right side panel of the window. Change the Scale x to 0.5 and y to 0.5

Do the same to GroundGrassSmall

Now you should have something like this


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s