与Python相反,函数不是GDScript中的第一类对象。这意味着它们不能存储在变量中,不能作为参数传递给另一个函数,也不能从其他函数返回。这是出于性能原因。
若要在运行时按名称引用一个函数(例如,将其存储在一个变量中,或将其作为参数传递给另一个函数),必须使用``call``或``funcref`` 帮助器:
# Call a function by name in one step.my_node.call("my_function", args) # Store a function reference. var my_func = funcref(my_node, "my_function") # Call stored function reference. my_func.call_func(args)
Remember that default functions, like _init
, and most notifications, such as _enter_tree
, _exit_tree
, _process
, _physics_process
, etc. are called in all base classes automatically. So there is only a need to call the function explicitly when overloading them in some way.