Game development with Godot 🤖

Skip to content

This is a machine-translated text that may contain errors!

Where does this information come from?

Much of this is just a retelling of Godot’s official documentation. Below you will find some useful links to the official documentation (which I generally recommend over this one on Piggy, but if you prefer this one, feel free to use it!)

Useful Resources:

What about graphics, do I have to create them myself?

No! You can find a lot of free assets here:

Other game engines?

If you prefer, you can use other game engines. Examples of other game engines are:

There are many others as well, feel free to search if you want to try something else!

What is Godot?

Godot (pronounced Guh-doh), is a ‘Game Engine’. To put it simply, it’s a program that lets you create games (or regular programs if you want). Godot can do almost everything other game engines can do, both 2D and 3D.

Examples of games made in Godot: Godot Showcase

It’s not just Indie games that have been made in Godot, Sonic Colors Ultimate was made with Godot!

How do you get your hands on Godot?

You can either download Godot from:

Just download and install (or run on steam).

Set up a project!

When you start Godot, you get this window:

Godot Project

Here you are asked about the programming language, GDScript or C#. GDScript is very similar to Python.

Differences between GDScript and Python (without colors):

def hello():
    text = "Hello world!"

    print(text)
func hello():
    var text = "Hello world!"

    print(text)

For variables in Python you only need to write the name of the variable. In GDScript you must write var first. To create functions you write func instead of def.

Scenes & Nodes

One of the most important concepts in Godot is Scenes and Nodes. We can start with nodes. A node is an object in Godot, and it can represent anything. It can be something representing a player, an enemy, a button in a menu, text on the screen, anything at all. Scenes are a collection of nodes.

Here we will create a very simple example.

Part 1a - Set up the “player” scene

At the top of the Godot window, click the “2D” button to change the view to a 2D view.

On the left side of the window, you see the following interface:

Create Scene

Click the “Other Node” button, and search for “CharacterBody2D”, select it and click “Create”. This is a node that is used for a 2D player. You might see that there is a warning triangle ⚠️ next to the “CharacterBody2D” node. This is because it is missing some things it would like to have.

If you right-click on the node, there is a “+ Add Child Node…” button. Use this and add two nodes, Sprite2D and CollisionShape2D. Sprite2D is used to add some graphics to the player, while the other is used to check for collisions. You can also give them names if it makes it easier to keep track of things. I have given my CharacterBody2D the name “Player”. The scene should look like this now:

Scene currently

Part 1b - Fixing ⚠️ on CollsionShape2D

The warning triangle in this case is that the CollsionShape2D lacks actual collision. You can fix this by pressing the node (to the left), and a panel will appear on the right side. Here you get a bunch of information about the node you can change. Feel free to play around with what’s there. But the one we want to focus on is “Shape”, set this to for example RectangleShape2D. It’s not very important, we are not going to use the collisions here.

Part 1c - Adding a Sprite, Graphic

If you click on Sprite2D on the left, the field on the right appears, where it says “Texture”. Here you can put in an image for the player. Just drag and drop an image into the field.

This is what it will look like after adding a sprite.

Sprite menu

Part 2 - Adding Input, Controls

At the top of the window is a menu, “Scene - Project - Debug - Editor - Help”. Press “Project” and then “Project Settings”. Here you will get up a menu with lots of settings. Press “Input Map”. Here you can set up keys on the keyboard.

  • In the “Add New Action” field, type “left” then press “Add”.
  • Then add “right”, “up” and “down”.
  • These become known as “Actions”
  • On each “Action” you can add buttons by pressing the + button to the right.

Plus button

  • Here you just need to press the keyboard, then press Add.
  • Add buttons to all actions.

Part 3 - Adding a script to control the player.

To be able to add game logic, i.e. do something with the player, the background, or whatever it may be in the game, we need a script. Scripts are code, and can be written in two languages, GDScript or C#, GDScript is default.

  • Press on the “CharacterBody2D” (I have called it “Player”) node.
  • Press the “Attach Script” button (see below)

Attach script button

  • Here you will get up a window where you can choose language (choose GDScript) and a path (just let it be called what is there, but you can give it a name), then press further. Here you get up a “Script” window. There is not much here to begin with.

extends CharacterBody2D
# Dette er en karakter som kan bevege seg
# This is a character that can move

The only thing this code says at the moment is that the code should belong to a node of the type “CharacterBody2D”, which is our player.

We will add a function here that we will use to manipulate the player:

extends CharacterBody2D

func _physics_process(delta: float) -> void:
   # return

Physics Process?

_physics_process is a function that updates every single “frame,” meaning around 60 times per second (default). There is another function that is simply called _process, which updates the entire way. If you want the player to move at a smooth pace, use _physics_process.

This is where we will add code that moves the player.

Part 4 - Basic input

In part 2 you added input buttons, now we will use them. There is a built-in object called Input that we can use to check if the player has pressed what we have set up in “Input Map”.

Try adding this code into the _physics_process code:

if Input.is_action_pressed('right'):
   # Hvis høyre tast trykkes, sett x-hastigheten til 100
   velocity.x = 100

# Flytt karakteren og håndter kollisjoner
move_and_slide()

What is move_and_slide()?

move_and_slide() is a built-in function in Godot that is used when we actually want to move what we apply it to. Without this, the player will not move.

Warning

Whitespace on the lines is very important, this is similar to in Python.

What happens when you start the program by pressing the play button in the Godot window, then pressing “right”?

If nothing happens now:

  • Have you remembered to set something up in “Input Map”?
  • Have you written right and not Right? That is, is what you wrote in the code the same as the name in the input map?

The entire code so far
extends CharacterBody2D

func _physics_process(delta: float) -> void:
    if Input.is_action_pressed('right'):
        velocity.x = 100

    move_and_slide()

Try now to add code for 'left', 'up', 'down'.

What must velocity.x be for left? What about up and down?

Entire code now
extends CharacterBody2D

func _physics_process(delta: float) -> void:
    if Input.is_action_pressed('right'):
        velocity.x = 100
    if Input.is_action_pressed('left'):
        velocity.x = -100
    if Input.is_action_pressed('down'):
        velocity.y = 100
    if Input.is_action_pressed('up'):
        velocity.y = -100

    move_and_slide()

Part 5 - Fixing the Code

You might notice that the player doesn’t stop when you release a direction. We can fix that now!

Before all the if-statements, add a line that sets the velocity to 0. You can do this by writing velocity = Vector2()

All speeds and directions in Godot are vectors, this is a math concept we won’t go into now, but if you want to learn more about what this means, you can go here: Wikipedia vectors.

Entire code now
extends CharacterBody2D

func _physics_process(delta: float) -> void:
    velovity = Vector2()

    if Input.is_action_pressed('right'):
        velocity.x = 100
    if Input.is_action_pressed('left'):
        velocity.x = -100
    if Input.is_action_pressed('down'):
        velocity.y = 100
    if Input.is_action_pressed('up'):
        velocity.y = -100

    move_and_slide()

When you start the game now you can move the player around:

Further, you can fix the code to make the speed not just a number, but can be stored elsewhere.

You can for example (before the function) add a constant that keeps track of the speed.

Entire code at the end with const
extends CharacterBody2D

const SPEED = 100

func _physics_process(delta: float) -> void:
    velocity = Vector2()

    if Input.is_action_pressed('right'):
        velocity.x = SPEED
    if Input.is_action_pressed('left'):
        velocity.x = -SPEED
    if Input.is_action_pressed('down'):
        velocity.y = SPEED
    if Input.is_action_pressed('up'):
        velocity.y = -SPEED

    move_and_slide()

Part 6 - Play Around Yourself!

If you go back to the first part, Useful Resources, you can find what you can continue to play with.

After this, you can try to create your own game. What you create is up to you! If you want to create something completely new, do it! If you want to try to emulate a game that already exists, do it! The best way to learn is to try!