Creating the player script
This is the moment we have been training for. We already know how to do this! So, start by creating a new script that is attached to the Player
node:
- Right-click the
Player
node and select Attach Script:
Figure 6.18 – Attaching a script to the Player node
- In the dialogue that pops up, call the script
player.gd
:
Figure 6.19 – Calling the script player.gd
- We’ll keep it simple for now and just add some code to manage the health of the player:
extends Node2D const MAX_HEALTH: int = 10 var health: int = 10 func add__health_points(difference: int): health += difference health = clamp(health, 0, MAX_HEALTH)
The
clamp()
function we use in theadd_health_points()
function takes a numeric value as the first argument and keeps it in between the second two numeric parameters.
This way, the health is always between 0
and MAX_HEALTH...