Scripts at a high level are units of logic you can attach to Game Objects. They allow you to implement functionality within your game. The language scripts are written is known as GDScript.

Now, there are some interesting points to remember about scripts — some secrets. As you get further into scripting you find out that they are classes!

Scripts Are Classes

Similar to a programming language like Java, a script is considered a class and extends some other class in most cases. A class at a high level means that a script shares its functionality with its parent. There are good examples of this that will come up in your projects such as enemies and players.

A player needs to have hit points, the ability to take damage, and the ability to move. An enemy needs similar functionality. Therefore it would make sense for both the enemy and player to have the same script that extends KinematicBody2D. Why? Because the KinematicBody2D class has functions and properties that let you easily implement movement and still write functionality for handling hp. Here’s an example script to do just that.


extends KinematicBody2D
export var speed:int = 400
export var health:int = 10
func _physics_process(delta:float)->void:
func process_inputs(delta)->void:
var velocity = Vector2(0, 0)
if Input.is_action_pressed("ui_right"):
velocity.x = 1
if Input.is_action_pressed("ui_left"):
velocity.x = 1
if Input.is_action_pressed("ui_down"):
velocity.y = 1
if Input.is_action_pressed("ui_up"):
velocity.y = 1
if Input.is_action_just_pressed("ui_fire"):
process_movement(delta, velocity)
func process_movement(delta:float, velocity:Vector2)->void:
var size = get_viewport().size
var temp = velocity * (speed * delta)
func add_health(value:int)->void:
func is_alive()->bool:
return health > 0

As you can see, we have implemented both movement and handling health points in a single script. With a few modifications, we can use this on enemies to implement the same behavior. Note the extend keyword; that means any object in our game that uses this script must be of the KinematicBody2D type.

Now, there are a lot more interesting things about scripts, which we’ll get into in future posts. Stay tuned for now and good luck making your game!

%d bloggers like this: