0 votes

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?

in Engine by (49 points)

2 Answers

0 votes
Best answer

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")
by (9,784 points)
selected by

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.

I didn't test it. but as github commit, you can do it now with preload.
see https://github.com/godotengine/godot/issues/4756

+3 votes

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()
by (29,090 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.