What it is FuncRef/any examples?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By The_Black_Chess_King

In the documentation it says:


Description: In GDScript, functions are not first-class objects. This means it is impossible to store them directly as variables, return them from another function, or pass them as arguments.
However, by creating a FuncRef using the @GDScript.funcref function, a reference to a function in a given object can be created, passed around and called.

I didn’t understood. It’s like editing functions on the fly or passing functions as an argument?

:bust_in_silhouette: Reply From: Zylann

It allows to store a function into a single variable, so that only this variable will be required to call it on the proper object. It can then be passed as a single argument to other functions as well.
It is not “editing” functions, just passing them around.


obj.say_hello("message", 42)

Is the same as:

var f = funcref(obj, "say_hello")
f.call_func("message", 42)