How to easily access different class

Godot Version

4.2.2 stable

Question

how to easily access different class

how am I doing so far, and it’s probably wrong way even it it works, …

I make scene Tools with simple node
I make GDScript Tools and attach it to Tools scene
I make singleton Tools

inside GDScript Tools I make class NullList:

and to access NullList I do:

var example = Tools.NullList.new()

so this procedure works
and my question is how do I do it that I would need to do only

var example = NullList.new()

is it possible?

If NullList is an inner class of Tools, I don’t think you can access it without accessing Tools first. If you want to access it directly, you need to create a separate script with the class_name NullList. You don’t need to make NullList a singleton.

An example of the first line of NullList script:

class_name NullList extends Node

Now you should be able to access it directly (NullList.new())

And if you don’t want to “pollute” the global namespace you can omit the class_name declaration and manually import the class where you need it.

const NullList := preload("res://null_list.gd")

var null_list: NullList = NullList.new()
1 Like