Can't call inner class from singleton

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Arc Futahito
:warning: Old Version Published before Godot 3 was released.

I have a class in my singleton that I need to access from another node. I set up a reference to it with get_node(“/root/global”).Character.new()
Trying to get variables from it just throws me “Identifier not found” error.

I know you can set a separate gd file as a class and “extends” from that, but it’s a really small bit I’d like to have in global for convenience.

Is there a way to call inner class from other gdscript?

:bust_in_silhouette: Reply From: volzhs

It’s not about calling inner class.
But this can be solved.

in global, add function to return Character.new()

func new_character():
    return Character.new()

and use this function

var new_char = global.new_character()
new_char.set_name("tom")

Thanks a lot. Tried it yesterday but didn’t work for some reason (that’s what I get for writing code past midnight) but now everything is fine. That should do it.

Arc Futahito | 2016-06-12 09:48

I didn’t test it. but as github commit, you can do it now with preload.
see Instancing inner classes directly from 'preload()' gives errors in editor, but works. · Issue #4756 · godotengine/godot · GitHub

volzhs | 2016-06-13 14:13

:bust_in_silhouette: Reply From: Zylann

You don’t need the script to be a singleton in order to access the inner class. By using preload() you can instance it this way:

var Global = preload("res://your/global.gd")
var character = Global.Character.new()