Fluent Interface
2025/12/27
- Type
- Learning Resource
- Format
- Glossary Article
- Version
- General
- Subject Tags
- Created
- Updated
- 2026/02/16
- 2025/12/27
A fluent interface is a design pattern that allows you to chain method calls together. In Godot, it is useful for creating objects in a single line.
For example, suppose you wanted to create an item with a name, weight, and cost. This is how the script would look:
class_name Item extends Area3D
var name := ""
var weight := 1
var cost := 10To use it in another script in Godot, you would need to use new():
var item := Item.new()
func _ready() -> void:
item.name = "Sword"
item.weight = 2
item.cost = 20This is a bit bothersome because it splits the item definition across multiple places. You could make a constructor that takes all the parameters, 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_weightThis solves the problem:
var item := Item.new("Sword", 2, 20)But that makes it so you need to always pass arguments to Item when you create it. This could be limiting; in the current example, there are only 3 properties, but in a real game, you might have many more.
One solution to this is custom constructors. Another is to use a fluent interface. Here's how you could implement it:
class_name Item extends Area3D
var name := ""
var weight := 1
var cost := 10
func setup(properties: Dictionary) -> Item:
for key in properties.keys():
if key in self:
self[key] = properties[key]
return selfNow you can create an item like this:
var item := Item.new().setup({
"name": "Sword",
"weight": 2,
"cost": 20
})You can even chain more method calls together. For example, you could add an enchant() method to the Item class:
func enchant(enchantment: String) -> Item:
name = enchantment + " " + name
return selfAnd then use it:
var item := Item.new().setup({"name": "Sword"}).enchant("Magic")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…