Menu

GDScript Lambdas and Callable: func(), call() and bind()

Inline functions in Godot 4: write a lambda with func, store and call it, pass functions to map, filter and signals, add arguments with bind(), and understand how lambdas capture variables.

This page includes runnable editors. Edit, run, and see output instantly.

A lambda is a function without a name, written where you need it. It is a value of type Callable:

42
slime is hurt

Call a stored lambda with .call(), not with plain parentheses. A short lambda fits on one line after the :; a longer one is indented like any function body.

Passing Functions to Other Functions

Array methods, sorting and signals all take a Callable. A lambda keeps the logic where it is used:

["golem", "slime"]
["bat", "slime", "golem"]

Methods as Callables

A method name without parentheses is also a Callable. So is Callable(object, "method_name"):

Hello, Ana!
greet true
Hi, Bo!
Hey, Cy!

Prefer the direct form (greet): the editor checks that the method exists. Callable(self, "greet") uses a string, so a typo only fails when it runs.

bind(): Adding Arguments Ahead of Time

bind() returns a new Callable that remembers extra arguments. They are added after the ones passed to call():

Ana bought potion for 5
Bo bought potion for 5

This is the usual way to tell which button sent a signal:

func _ready():
	for button in $Menu.get_children():
		button.pressed.connect(_on_menu_pressed.bind(button.name))

func _on_menu_pressed(button_name: StringName) -> void:
	print("Pressed ", button_name)

How Lambdas Capture Variables

A lambda can read the local variables around it. It copies their values when the lambda is created:

11
0
["hit"]

To have a lambda change state, use a member variable of the script, or an array or dictionary, which are shared rather than copied.

Lambdas as Signal Handlers

A lambda can be connected straight to a signal, which saves a separate method for a one-line reaction:

+10
+5
Total: 15

Use a named method instead when you will need to disconnect() the handler later: you can only disconnect a Callable you still hold.

call_deferred()

call_deferred() runs the Callable at the end of the current frame instead of now. Godot asks for it when you change the scene tree during physics callbacks:

now
at the end of the frame

In a game, add_child.call_deferred(bullet) inside a collision callback is the common case.

Frequently Asked Questions

How do I write a lambda in GDScript?

Use func without a name where a value goes: var double = func(x): return x * 2. Call it with double.call(5). A lambda may span several indented lines like any function.

What is a Callable in Godot?

A Callable is a function stored as a value. A method name without parentheses (take_damage) and a lambda are both Callables. You call one with .call(), pass it to map() or sort_custom(), or connect it to a signal.

What does bind() do in Godot?

bind() returns a new Callable with extra arguments stored in it. They are added after the arguments given to call(). It is how you pass extra data to a signal handler: button.pressed.connect(_on_pressed.bind("start")).

Why does my lambda not change a local variable?

A lambda captures local variables by value when it is created. Assigning to the copy inside the lambda does not change the outer variable. Arrays and dictionaries are shared, so changing their contents does work, and member variables of the script work too.

Coddy programming languages illustration

Learn to code with Coddy

Get started