Check if funcref refers to an existing function before call_func?

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

Hi, I have this snippet of code to run functions based on a fn_name stored in a string.

var fn = funcref(functions, fn_name) 
if fn == null: # this is never triggered
    print("COULD NOT FIND FUNCTION ")
    return
fn.call_func() # execute the function

If function doesn’t exist godot throws:

Invalid call. Nonexistent function 'call_func' in base 'FuncRef'.

Is there a way to test funcref refers to an existing function before the call to call_func?

:bust_in_silhouette: Reply From: coffeeDragon

As far as i know there is no way of doing exactly that, sadly.
I would instead make sure that the refrence is not null and has the desired function and then set the var to null if either of them aren’t the case.
You can use the :

object.has_method("method_name")

to check if a given object has the given function. See:

Example:

if object.has_method("damage")
   object.damage(amount)

luislodosm | 2020-04-12 12:42

:bust_in_silhouette: Reply From: avnih