Init Function
2025/12/27
- Type
- Learning Resource
- Format
- Glossary Article
- Version
- General
- Subject Tags
- Created
- Updated
- 2026/02/16
- 2025/12/27
In Godot, objects are created in a few different ways:
var scene := SceneFile.instantiate())creates nodes in memory, and their children, as well as any associated resources.
var resource := load("resource.tres"))creates a resource in memory, and all sub-resources.
.new() in your own code, such as var timer := Timer.new().Every time an object is created through any method, the _init() function is called. In most languages, this is called the constructor function. _init() is used to set up an initial state, connect signals, and perform other setup tasks.
When using nodes, you might be more familiar with the _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.
When using nodes, you are forced to wait for them to be _ready() before you can interact with them. When creating objects in code, you can interact with them immediately after calling .new().
See the node lifecycle article for more information about the order in which functions are called.
_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()Calling var record := StopwatchRecord.new() would automatically call _init() and run the enclosed code.
It's possible to have parameters in _init():
class_name Person
var name := ""
var age := 0
func _init(initial_name: String, initial_age: int) -> void:
name = initial_name
age = initial_ageYou would then provide those values with var person := Person.new("Gandalf", 24000).
When Godot instantiates a scene, or loads a resource, it will not call new() with parameters, and so, any object that has parameters in _init() will fail to load.
Only use parameters in _init() if you're sure the object will only be created in code, through .new().
It's generally safer to either provide default arguments, or have a create() function.
Here's how you would do default arguments:
class_name Person
var name := ""
var age := 0
func _init(initial_name := "nobody", initial_age := 0) -> void:
name = initial_name
age = initial_ageIn this case, an empty .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 personNow, calling Person.new() still works, but you can get the new() functionality by using Person.create().
Don't stop here. Step-by-step tutorials are fun but they only take you so far.
Try one of our proven study programs to become an independent Gamedev truly capable of realizing the games you’ve always wanted to make.
Get help from peers and pros on GDQuest's Discord server!
20,000 membersJoin ServerThere are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!
Sponsor this library by learning gamedev with us onGDSchool
Learn MoreImprove and build on assets or suggest edits onGithub
Contributeshare this page and talk about GDQUest onRedditYoutubeTwitter…