Class Objects
A class object is an instance of a class. A class object can also be referred to as a class instance, instanced object, or class object.
#Player.gd
extends Node2D
class_name Player
# class variables and functions
#Scene.gd
extends Node2D
var playerInstanceObject = Player.new()
Class Constructors
The class constructor is a particular function in which it is called every time a class object is created.
You define a class constructor using the _init()
method:
#Player.gd
extends Node2D
class_name Player
var playerHealth: int
# Class Constructor
_init():
playerHealth = 100
#Scene.gd
extends Node2D
var playerInstanceObject = Player.new()
print(playerInstanceObject.playerHealth) # 100
You are also able to pass arguments into the constructor as well:
#Player.gd
extends Node2D
class_name Player
var playerHealth: int
# Class Constructor
_init(startingHealth:int = 100):
playerHealth = startingHealth
#Scene.gd
extends Node2D
var playerInstanceObject = Player.new(200)
print(playerInstanceObject.playerHealth) # 200
Resources
Github Project:
Godot Tutorials Resources