var scene := SceneFile.instantiate()
)
creates nodes in memory, and their children, as well as any associated resources.var resource := load("resourse.tres")
)
creates a resource in memory, and all sub-resources..new()
in your own code, such as var timer := Timer.new()
._init()
function is called. In most languages, this is called the constructor function. _init()
is used to set up an initial state, connecting signals, and other setup tasks._ready()
function. _ready()
is called after _init()
, and is used to set up the node's state after it's been added to the scene tree, and all its children have been added too._ready()
before you can interact with them. When creating objects in code, you can interact with them immediately after calling .new()
._init()
can be used to set up an initial state:class_name StopwatchRecord
var date := ""
func _init() -> void:
date = Time.get_datetime_string_from_system()
var record := StopwatchRecord.new()
would automatically call _init()
and run the enclosed code._init()
:class_name Person
var name := ""
var age := 0
func _init(initial_name: String, initial_age: int) -> void:
name = initial_name
age = initial_age
var person := Person.new("Gandalf", 24000)
.new()
with parameters, and so, any object that has parameters in _init()
will fail to load._init()
in parameters if you're sure the object will only be created in code, through .new()
.create()
function.class_name Person
var name := ""
var age := 0
func _init(initial_name := "nobody", initial_age := 0) -> void:
name = initial_name
age = initial_age
.new()
wouldn't trigger an error; And here's a create()
function:class_name Person
var name := ""
var age := 0
static func create(initial_name: String, initial_age: int) -> Person:
var person := Person.new()
person.name = initial_name
person.age = initial_age
return person
Person.new()
still works, but you can get the new()
functionality by using Person.create()
.