Call a static function without instantiating object? (Autoload not working?)

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

I’m having a hard time understanding why I’m getting an “Invalid call. Nonexistant function ‘xxx’ in base ‘Nil’” error… So I have an autoloading object ‘Global’;

Global (autoload in the project settings):

extends Object
class_name Global
static func xxx(var x):
	return '1':

Method call:

Global.xxx(0); // "Invalid call. Nonexistant function 'xxx' in base 'Nil'"

If I try to call the function, I get an error which seems to imply that ‘Global’ is null/Nil… Well yeah, but since the function is static, isn’t it the point? Shouldn’t it work nonetheless?

:bust_in_silhouette: Reply From: Salvakiya

take a look here: Singletons (AutoLoad) — Godot Engine (3.1) documentation in English

an autoload singleton is actually just that… a singleton. there is one instance of this class when you run the game/app. the name given in the autoload menu is how you access that class. no need for a static function and you dont even need to give it a “class_name”

Thanks for your help! Re-read and reread and after much thinking, figured out why it wasn’t working in my case… Object!! Singleton must be a Node…!

nwgdusr999 | 2019-11-30 23:52

AHH! wish I could have caught that and been more specific. Thanks for sharing!

Salvakiya | 2019-12-01 00:23

:bust_in_silhouette: Reply From: nwgdusr999

After much confusion about my ‘Global’ singleton throwing compiling errors and crap, I just realized my mistake. It was type Object, which basically breaks everything… It really needs to be Node to be working as a Singleton…! As an Object, it is always ‘null’ globally; likely because it can’t be added to the root node. So, must be node!

I have just spent an hour with the exact same problem and exact same solution… I figured it out as well, I really think this should be noted in the docs, I will try adding it.

Adam Volný | 2019-12-07 16:51

If you want to maker a helper class. then use class_name or add singlton. You dont need use them both. And class_name with extends Object is working

yikescloud | 2022-10-30 05:43