+1 vote

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?

in Engine by (204 points)

2 Answers

+1 vote

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:
http://docs.godotengine.org/en/3.0/classes/class_object.html#class-object-has-method

by (262 points)

Example:

if object.has_method("damage")
   object.damage(amount)
0 votes
by (149 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.