0 votes

Given a custom script that defines a number of functions:

# Custom_script.gd
func func_1( ... ):
    ...
func func_2( ... )
    ...
...

how can I get the names of the functions as an array of strings ['func_1', 'func_2', ... ] in another script?

in Engine by (141 points)

1 Answer

+1 vote
Best answer

Some quick experimentation indicates this might be most of what you want...

func _ready():
    print(get_method_names())

func get_method_names():
    var methods = []
    for fun in get_method_list():
        if fun["flags"] == METHOD_FLAG_NORMAL + METHOD_FLAG_FROM_SCRIPT:
            methods.append(fun["name"])
    return methods

Note, you'll need to have a reference to the script you're interested in. From there, you can call the scripts get_method_names method. Also, the flag references I have there seem to work in my tests, though there might be a better way to utilize them...

by (21,864 points)
selected by

Amazing dude! Thanks a lot! You even did all of the work for me =)

Works like a charm. I just add it to the script that contains the functions, and then remove "get_method_names" from the resulting array.

NOTE: To all those who try to use this: this does NOT work for a script that contains static functions, since get_method_list() can only be called in script instances.

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 Frequently asked questions and 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 [email protected] with your username.