Custom Constructors
2025/12/27
- Type
- Learning Resource
- Format
- Glossary Article
- Version
- General
- Subject Tags
- Created
- Updated
- 2026/02/16
- 2025/12/27
In Godot, you call .new() on a class to create a new instance. This invokes the _init() method, which is the constructor of the class.
Often, you might be interested in passing different arguments to the constructor. A good way of handling this is to make your own custom constructors. For example, suppose you have an Item class like so:
class_name Item extends Area3D
var name := ""
var weight := 1
var cost := 10
func _init(p_name: String, p_weight: float, p_cost: int) -> void:
name = p_name
cost = p_cost
weight = p_weightIf most items in your game have a weight of 1 and a cost of 10, you can create a convenience method to create items with default values:
class_name Item
# ...
static func create_default(p_name: String) -> Item:
return Item.new(p_name, 1, 10)Or maybe, if you wanted all properties to be optional, you could use a dictionary to set properties:
class_name Item
# ...
static func from_dictionary(props: Dictionary) -> Item:
var item = Item.new()
for key in props.keys():
if key in item:
item[key] = props[key]
return itemNow you have three ways to create an item:
func _ready() -> void:
var sword = Item.new("Sword", 2, 20)
var default_sword = Item.create_default("Sword")
var sword_from_dict = Item.from_dictionary({
"name": "Sword",
"weight": 2,
"cost": 20
})You're not limited to new()! This frees you from having to remember the order of arguments when creating instances, and also allows you to keep _init() clean and simple.
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…