Classes

Classes combine data and functions. In the Godot API (Application Programming Interface) there are many pre-defined classes. These classes are documented in the API section of the official documentation.

When we want to use a class we usually create a new instance of it. This new instance of the class is an object with a unique reference. For example, the Nodes of a scene in Godot are instances of classes.

We create a new scene by extending an existing class such as Node2D. That's why the first line of code says what class is being extended.

The data of a class are stored in variables that we call properties. And the functions are called methods. When we extend a class, we gain access to it's properties and methods. Also, we can add our own properties and methods to extend the functionality.

Some methods may be replaced by our own such as the _ready and _process functions. These are called virtual methods and have a name prefixed by an underscore. In most of the code examples so far, we replaced the _ready function in the extended Node2D class.

Once we have saved a scene, we may reuse it in other scenes by creating new instances of it. In fact, we have created a new unnamed class. To give our class a name, we may use the 'class_name' keyword to register it as a new type in Godot’s editor.

Example of extending and naming a new class:

extends Node2D

class_name Motorcycle

# Add properties
export(String) var make = "Kawasaki"
export(int) var cc = 900
export(Color, RGB) var color = ColorN("Ninja Green")
var fuel = 0.0
var speed = 0.0

# Override virtual methods
func _ready():
	add_fuel(17.3)

func _process(delta):
	if fuel > 0.0:
		speed += delta
		fuel -= delta
		print(speed, "km/h")

# Add a new method
func add_fuel(litres):
	fuel += litres

Well that's it for the basics of GDScript programming. Please use the tutorials here as a future reference to refresh your knowledge.

The next stage is to start creating your own games. I am starting to add tutorials on common ploblems that need to be solved when coding games, so be sure to check out Godot Solutions.