Callable is null

Godot Version

4.2.2.stable

Question

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!

Can you please explain it a bit clearly It would be better if you could provide some code and debugger output you are getting

You don’t have to write it this way, you can use my_library.call_deferred(current_script) which is shorter and somewhat safer.

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

Sorry, here’s the code while I’m debugging:

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?

printing (my_library) returns <GDScript#-9223372008266398480>

my_library is declared at the top of this dialog manager’s script with this line:
const my_library = preload(“res://code/lonely code/library.gd”)

Am I missing anything?

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

Callable(my_library_instance, "current_script")
2 Likes

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?

If it is an Autoload you do not need to create a instance of it Library will work (on ready)

func callable_example() -> void:
    var callable = Callable(Library, "current_script")
    callable.call_deferred()

I think I have it set up the same way, and yet the callable instance is still null for some reason

This is the code I’m trying to execute from the library script, it’s very simple.

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.

1 Like

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!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.