![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | SleepySheepy |
Can I return a ‘func’ type from a function?
Specifically, I would like to return a function (NOT IT’S VALUE!) from another function. Is this possible? Similar to how C# delegates would work.
For reference, I am using this unity tutorial, but converting it to gdscript, and am getting stuck on the section on setting up delegate functions.
I found FuncRef, but it doesn’t seem to be working properly with my library class.
class_name FunctionLibrary
func _init():
pass
func wave(x : float, t : float) -> float:
return sin(PI * x + t)
static func multiwave(x : float, t : float) -> float:
var y : float = sin(PI * (x + 0.5 * t));
y += 0.5 * sin(2.0 * PI * (x + t));
return y * (2.0 / 3.0)
static func ripple(x : float, t : float) -> float:
var d : float = abs(x)
var y : float = sin(PI * (4.0 * d - t));
return y / (1.0 + 10.0 * d)
func get_function(index : int):
if (index == 0):
return funcref(self, "wave")
elif (index == 1):
return funcref(self, "multiwave")
else:
return funcref(self, "ripple")
And I get the following error on the return lines of ‘get_function’:
Function “funcref()” not found in base self