Classes describe an aggregate of ta fields such as variables and defines the operations, such as methods.
Think of a class as a blueprint for creating objects, with initial value states, and implementation behavior.
In GDScript, by default, all script classes are unnamed classes.
This means you can only reference a class by its relative path.
# Absolute Path on Windows "C:\Data\Filepath" # Relative Path through GDScript "res://path/to/file.gd" Use the extends keyword in a class file to let Godot know which Global Godot class your script file inherits from.
The extends keyword determines what Node functions are available to you.
You can learn more about the Godot Global Classes in the Godot Basics Series.
Classes can also have class/member variables and functions.
Class/member variables are available in the entire class file.
# Example.gd extends Node2D var playerHealth: int = 100 var playerSpeed: float = 5.0 func getPlayerHealth() -> int: return playerHealth If you omit the extends keyword, your class will inherit from the Reference global class:
# Example.gd # Extends from Global Reference Class var playerHealth: int = 100 var playerSpeed: float = 5.0 func getPlayerHealth() -> int: return playerHealth Use the class_name keyword to name a class and add it to the global scope.
The newly created class will be available in other .gd files and scenes in Godot.
#Player.gd extends Node2D class_name Player # class variables and functions #Scene.gd extends Node2D var playerInstanceObject = Player.new() A GDScript class can inherit from the following:
An inner class is a class inside of a class.
extends Node # class instance object var object = InnerClass.new() func _ready(): var getInnerValue = object.getA() print(getInnerValue) class InnerClass: #member variables var another = 100 func getAnother(): return another A virtual method is a method that can be redefined in a derived class.
It is used when a method’s basic functionality is the same, but sometimes more is needed in the derived class.
Classes | Godot GDScript Tutorial | Ep 15 video & article by Godot Tutorials is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License .