See all glossary terms

Constructor method

The constructor method is a function that initializes a new class instance. In GDScript, you can define the constructor of a class by overriding the _init() method.
When you create a new class instance, Godot automatically calls the constructor method. You can use this method to set up the object's initial state and perform other setup tasks.
Note that _init() runs as soon as you create an instance of the class. For example, if you create a script called HurtBox3D with the following code:
class_name HurtBox3D extends Area3D

signal took_hit(hit_box: HitBox3D)

func _init() -> void:
	monitoring = true
	monitorable = true
	area_entered.connect(func _on_area_entered(area: Area3D) -> void:
		if area is HitBox3D:
			took_hit.emit(area)
	)
The _init() method will run whenever you write the code var hurt_box = HurtBox3D.new() in your project. It will also run for any HurtBox3D node you add to your scene in the editor.
The constructor runs before nodes are ready
The constructor method _init() runs before adding nodes to the scene tree. So, accessing other nodes or resources that have not been loaded yet in the _init() method is not safe.
If you need to access the properties of other nodes, the constructor method is not the right place to do it. Instead, use the _ready() method or signals like ready or tree_entered. For more information, see our study guide on Node creation and the ready function.

Passing arguments to the constructor

You can pass arguments to the constructor method when you create a new class instance. To do this, you need to define a constructor method that accepts parameters.
For example, here's a HitBox3D class that represent an area that deals damage when another object enters it. You can define a constructor method that takes a damage parameter:
class_name HitBox3D extends Area3D

var damage := 1

func _init(p_damage: int) -> void:
	damage = p_damage
When you create a new instance of the HitBox3D class, you can pass the damage value as an argument. The following line creates a new HitBox3D instance that deals 5 damage:
var hit_box := HitBox3D.new(5)
Why did you name the parameter p_damage and not just damage?
Using the prefix p_ in front of parameter names is a convention used in some programming languages like C++. It differentiates the parameter from the class property with the same name.
You could also write the code like this in GDScript:
func _init(damage: int) -> void:
	self.damage = damage
In GDScript, local variables and parameters take precedence over class properties with the same name. So, you can use the same name for the parameter and the class property and use the self keyword to refer to the class property when needed.
Either convention is fine. You'll see both used in the Godot community.

Gotcha: Avoid using init with arguments

If you create a script for a node or resource that you invoke in code, there is no problem, you can call the constructor with the correct parameters.
But Godot will always call _init() with no arguments. Therefore, if you count on Godot's loading mechanisms, any script you write should have empty _init() methods (or use optional arguments). If you do use arguments, you won't be able to use load(), preload(), or the editor's scene instancing system.
_init() with arguments also pollutes subclasses; If you inherit your own class, you will have to call up the parent's constructor with the correct arguments.
To work around this limitation, you can use custom constructor or fluent interfaces.

See Also

Related terms in the Glossary