Howdy! I’m trying to make a general script library full of functions that can be accessed from other scripts. I have library.gd which has the static function test_func(). In my dialog manager, which is an autoloaded script, library.gd is preloaded in as const my_library and script name “test_func()” is sent in as a string named current_script. I try calling test_func() using the following code:
var callable = Callable(my_library,current_script)
callable.call_deferred()
I get no errors, but nothing happens. When using the debugger, I realized that it says callable is null::null. What does this mean and how do I get it to not be null? Thanks in advance!
Thank you, but when I do this it says “Cannot call non-static function “call_deferred()” on the class “…library.gd” directly. Make an instance instead”. That’s why I created a callable in the first place
My main issue is that the variable callable I’m trying to instantiate isn’t doing anything, and when I mouse over it while debugging, it just says callable: null::null. Why is that?
That’ll do it, the script isn’t an instance of itself on it’s own. If you have a “my_library” Node like an autoload, or stored a my_library.new() this will work.
Also Callable’s second argument should be a StringName
my_library is just a script stands for the code file, not an object of this script. Consider you have a character_stats.gd file with some data defined. When you loaded that, you got a Script object, which is a definition of the stats object, and can be new() to get a stats object.
This is very helpful information, but unfortunately I’m still a noob at this. I made a new node, Library, with the library.gd script attached and made it autoloaded. Now in the script where I’m trying to make the variable referencing it, my_library, I don’t seem to know how to create the reference.
I’m using:
var my_library = Library.new()
But it says "nonexistent function ‘new’ in base ‘Nil’. What’s the correct way to create the reference variable?
Your library.gd does not have a function named “current_script”`, only “test_func” could work.
func callable_example() -> void:
var callable = Callable(Library, "test_func")
callable.call_deferred()
Looks like you have a parameter named current_script, I missed that first time around, apologies. But the stack variables shows it has parenthesis appended to it. let’s try changing the execute_script("test_func") function without parenthesis and current_script without quotes.
Thanks a bunch! The () at the end of test_func was the problem. The script works now and I can pass in different function names into current_script so that I can call any script from the script library! I really appreciate all the help!