How to get the function names in a custom script as an array of strings?

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

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?

:bust_in_silhouette: Reply From: jgodfrey

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…

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.

toblin | 2020-06-03 11:37