Introduction to GDScript in Godot 4 Part 1

Get started learning GDScript in this two-part tutorial. You’ll learn about the built-in script editor, using variables and player input. By Eric Van de Kerckhove.

4 (2) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Jump Using Player Input

The goal of the game will be flying from jumper to jumper, so you need a way of propelling the avatar upwards. The first jump will be performed with a mouse click, which means you need some way of reading the player’s input and act upon it.

While you can hard code mouse button polling, Godot has a useful input system that uses input actions to handle player input. Take a character moving forward for example, without input actions you’d have something like this:

if Input.is_physical_key_pressed(KEY_W) || Input.is_physical_key_pressed(KEY_UP) || Input.get_joy_axis(0, JOY_AXIS_LEFT_Y) < 0:
    move_forward()

With input actions, you can assign the W key, arrow up key and joystick up to an action and check that:

if Input.is_action_pressed("move_forward"):
    move_forward()

That’s a whole lot nicer, isn’t it? This allows you to reassign keys and buttons tied to an action from the editor or even during gameplay. You can also assign multiple inputs to the same action.
To add an action yourself, first select Project ▸ Project Settings… in the top menu to open the project settings.

Project settings

Now open the Input Map by clicking the corresponding tab at the top.

Input map

This where you can define new actions. Add a jump action by clicking the Add New Action field, typing “jump” followed by clicking the Add button to its right.

GIF of adding new action

With the jump action added, you can add input events to it by clicking the plus button next to its name. This opens the Event Configuration window. In here, select Mouse Buttons ▸ Left Mouse Button and click the OK button at the bottom to confirm.

Left mouse button, OK

Back in the Input Map, you can now see the jump action is tied to the left mouse button. Sweet!

Left mouse button, all devices

Go ahead and close the Project Settings window. Now open the player_avatar script again as it needs some additions to make the avatar jump.
To start with, add a new variable for the jump speed to the top of the script, below viewport_border:

@export var jump_speed : float = 2500.0

This is the vertical speed of the avatar when jumping, whenever it jumps from the ground or when it hits a jumper, this is the amount of pixels per second it will travel at. To use it, add the following function below the _process function:

func _jump() -> void:
    velocity.y = -jump_speed

This _jump function will change the Y velocity of the avatar to the value of the jump speed you just added, but negative. The value has to be negated as positive Y means down in Godot, while negative Y means up.
To call _jump, add this function above the function you just added:

func _process_input() -> void:
    if Input.is_action_just_pressed("jump"):
        _jump()

This function calls upon the Input class to check whether the jump action was just pressed and then calls the _jump() function if that’s the case. The is_action_just_pressed method checks for a single press, not a continuous hold, unlike is_action_pressed. To finish the function chain, add this line at the end of the _process function:

_process_input()

This will call the _process_input function every frame, which in turn checks for any inputs. While you could have put everything in _process, that makes it harder to track down the pieces of code and change them later on. Writing clean code is a skill in itself. :]
Time for another test drive, press F5 to play the project and try to make the player jump.

Robot jumping up and disappearing

I’d say that is a successful jump. It’s more like a rocket launch at the moment, but that’s fixable!
The reason why the avatar leaves the screen and never returns is because there’s no gravity applied to it. What goes up must come down after all. Add a new variable below jump_speed named gravity:

@export var gravity : float = 4000.0

This is a downward force that will be applied to the avatar. To apply it every frame, add this line to _process, right below velocity.x = 0:

velocity.y += delta * gravity

This will push the avatar down continuously, simulating gravity. Run the project again to see if this improves the jumping. You will notice the avatar falls down straight away and keeps falling, unless you click now and then to bounce it back up.

Robot constantly jumping and falling

This concludes the first part of the tutorial, what a cliffhanger! Or is it cliff-faller in this case? :]
Don’t worry, in the second part of this tutorial you’ll help the poor avatar out so it doesn’t have to jump all the time to prevent falling the endless abyss.

Where to Go From Here?

Thanks for reading this first part of the tutorial! You can download the project files using the link at the top and bottom of this page.
In the second part of this tutorial, you’ll learn how to use a finite state machine to neatly manage the avatar’s state, instantiate scenes using code, make the camera follow the avatar and much more! At the end, you’ll have a small game to build upon further.

We hope you enjoyed this first part of the tutorial. Leave a comment below or join the forum discussion to share your thoughts and ask any questions you might have!