@kryzmak said:
Edit2: As I now read the documentation regarding FuncRef
where it says that it is impossible to store functions as variables, I wonder why it then does work in my testings?
var test_var = test_func()
func _ready():
test_var
func test_func():
print("Why does this work?")
In tests that I did trying to make this all work, I had a similar thing happen. However, upon closer examination, it appears that test_func() is not being called during ready(), but actually being called up top at var test_var = test_func() . If you remove the test_var from ready(), it'll still result in test_func() being called and the print taking place.
@TwistedTwigleg said:
You can use a FuncRef
(documentation, GDScript function) to assign a function to a variable, which I think is what you are trying to achieve?
Then from there you could bind different functions to the FuncRef
so that it would call the function you need at the time. Using the example above, you could do something like this:
# Assuming this is a base class or something
var bake_function = funcref(self, "bake");
var is_cake = true;
func _ready():
if (is_cake == true):
bake_function = funcref(self, "bake_cake");
bake_function.call_func()
func bake():
print ("Baked something!");
func bake_cake():
print ("Baked a cake!");
The nice part about using a FuncRef
like this is that you don't have to know what function the FuncRef
is pointing to.
Hopefully this helps!
This helps. I'm building an engine for my game, not a game, right now. My goal in particular is to be able to call a function on IE parent Object A from child Object B, with Object B deciding which function that is without resorting to if-then statements. I now have this working, as follows, and would like to know if there is a way to do the following with only code on Object B:
On Object A we have:
#These variables must be declared or we get errors.
var dynamicfunctionname = ""
var dynamicfunction = funcref(self, dynamicfunctionname)
func updatedynamicfunctionname():
dynamicfunction = funcref(self, dynamicfunctionname)
#The below three functions represent functions we may want to call.
func cookie():
print("Cookies!")
func cake():
print("Cake!")
func muffin():
print("Muffins!")
On Object B (which happens to be a child of Object A just for the sake of this test, eventually I want to be able to reference other things than just parents but assume I can do it similarly to how I've done function names here):
export var dynamicfunctionname = ""
func _ready():
get_parent().dynamicfunctionname = dynamicfunctionname
get_parent().updatedynamicfunctionname()
func _process(delta):
get_parent().dynamicfunction.call_func()
The result is if "cake", "cookie", or "muffin" is put into the exported string variable on Object B through the editor, the output is correctly flooded with the print for that function.
This does work, but makes it so that any object whose functions I want to be able to access remotely through Strings must have
var dynamicfunctionname = ""
var dynamicfunction = funcref(self, dynamicfunctionname)
and some sort of function I could call to update the dynamicfunction with the new name , like:
func updatedynamicfunctionname():
dynamicfunction = funcref(self, dynamicfunctionname)
This is a moderately light-weight as a solution, I'm just wanting to know if I am missing anything simple that would allow me to avoid this. If I am understanding the requirements of funcref correctly, it would require adding the necessary variables remotely (both the funcref declaration, and the var String being used to point to the desired function.) Can variables be created on an object through pathing? Is there a way to modify the funcref for dynamicfunction on Object A from Object B? (This does not appear possible like a standard variable, IE get_parent().dynamicfunction = funcref(self, dynamicfunctionname), thus why I am having to pass it through a function on Object A, which may be better practice anyway but I'm trying to minimize.)
Meanwhile though, this all works fantastic of course if I am only concerned with accessing functions on Object B through variables defined on Object B, which alone is going to be extremely helpful, so thank you for the assistance!