functionscriptinggodotgdscript

Can Godot and GDScript store functions in variables?


I am confused by part of the Godot Docs for the GDScript language. About midway down the page, in the "Referencing Functions" section, it says you can't store functions in variables, and then seems to immediately contradict itself.

Can Godot functions be stored in variables or not?

Referencing Functions

Contrary to Python, functions are not first class objects in GDScript. This means they cannot be stored in variables, passed as an argument to another function or be returned from other functions. This is for performance reasons.

To reference a function by name at runtime, (e.g. to store it in a variable, or pass it to another function as an argument) one must use the call or funcref helpers:


Solution

  • (Late) Update for Godot v4.0+

    Godot v4.0 added first-class functions and lambdas backed by a Callable object.

    Read up on the past release blogs for more information.

    Now, you can pass functions by name:

    func callback(p_arg: String) -> void:
        print("Argument: ", p_arg)
    
    func accept(p_callback) -> void:
        p_callback.call("accepted")
    
    func _ready() -> void:
        accept(callback) # Prints "Argument: accepted"
    

    And define lambdas/closures:

    var lambda = func a_debug_label(p_arg: String) -> void:
        print("Argument: ", p_arg)
    accept(lambda) # Prints: "Argument: accepted"
    
    accept(func(p_arg): print(p_arg)) # Prints: "accepted"
    

    Callables replace funcrefs:

    var callable = Callable(my_obj, "some_func_name")
    callable.call()
    

    Note that callables are objects that wrap the target object and function name. The callable itself is not a function. You must call some_callable.call(). Lastly, a lambda's name is not accessible. It is only for debugging purposes.


    GDScript functions are not objects like they are in python. So, you cannot directly reference a function.

    However, you can indirectly reference them by name using their associated instance.

    For example with the following function:

    func hello():
        print('Hello')
    

    You can call a function on an instance by name:

    call('hello') # prints 'Hello'
    

    You can store an instance and function name with funcref():

    var ref = funcref(hello_object_instance, 'hello')
    ref.call_func() # prints 'Hello'
    takes_func_ref_to_call_later(ref) # later, prints 'Hello'
    

    FuncRef.call_func() does the same thing as Object.call() it just wraps it in an object.

    Consequently, a common pattern for callback functions, as shown by Object.connect() and friends, is:

    func deferred_finish(param1, param2, callback_obj, callback_func):
        # ... do something
        callback_ref = funcref(callback_obj, callback_func)
    func _process(delta):
        if _finished:
            callback_ref.call_func()
    func _enter_tree():
        deferred_finish('hello', 'world', self, 'finished_callback')
    

    I hope this helps. Let me know if you would like any clarifications.