This maybe a question of semantics being that all types ultimately inherit from Object.
Now I include external scripts into others, so I have modules of reusable code I can include only when needed.
I normally use the following syntax defining the internal variable for the script as an Object.
@onready var irq: Object = preload("res://Scripts/SubSystems/irq_system.gd").new()
But I can also use the type Node as well.
@onready var irq: Node = preload("res://Scripts/SubSystems/irq_system.gd").new()
Both seem to work fine, but Iām just curious is there one I should be using over the other. These script have no requirement to be global, and I like the ability to include or not include modules as required. All of them are just scripts, no GUI or visible elements.
Technically, preload returns a Resource type (see docs).
But it doesnt really matter for gdscript, so use whatever.
You could even remove the : Object part entirely.
Itās is a class yes, and for readability sake and maintaining code in the future Iāve changed them to the class name instead of just Object or Node.
When you do a .get_class() on them though, it still returns that they are a Node, which is understandable given pretty much everything in Godot is a Node of some type.
Doesnāt seem to be any performance difference between using Object, Node or ClassName. The benefit though is coming back to it later, in maybe months, to try and figure out what it does, though I do heavily comment my code as well which is always a help.
If you do not want to use a class name you can const declare the preload separately and use it as a type definition. This will give you auto complete without a global class_name definition.
const IRQSystem = preload("res://Scripts/SubSystems/irq_system.gd")
var irq: IRQSystem = IRQSystem.new()
Object vs Node depends on what type the script extends from. If your IRQSystem is a Resource then Node should give a runtime error or incorrect autocomplete. Static types could improve performance as Godot improves the script compiler, but most benefit is from the extra syntax checks.
I do exactly as youāve shown for my System Error (BSOD) screen. Iām aware that the type depends on the parent type it extends from.
Iād certainly change things if I was loading resources, for a typical Godot type game project. I donāt use any resource, like images, sounds, 3D models etcā¦
I have to differ on one point, in my testing static typing variables certainly does greatly improve script performance, Iāve written a number of programs just to test this one fact, and my current project now relies heavily on it. The results of those test is the reason I have set the compiler to throw an error if any variable is not statically typed. But again everyone is allowed to do as they find the most comfortable. Iām certainly not qualified nor experienced enough in Godot to be imposing my way of doing thing.
My original question was really just one of semantics, over the syntactical use of Object, Node or Class, and if anyone has seen a benefit in using one over the other.
Iām using the Class name method for readability sake and maintaining code in the future, it also makes finding any references to those classes far easier with a search should I need to make a change.
I always use the most qualified type possible. Object gives very little definition, I would never use it. I use Node if I only care that the object is part of the scene tree, like to iterate over itās children.
The syntactical advantage of using the most qualified type is potential run-time speed improvments, autocomplete, and better compile-time bug catching.
If your irq_system.gd extends Object then declaring it as a Node should not work.
Resources are for more than models and textures, data outside the scene tree can be made a Resource. This allows it to be shared between nodes or edited easily, such as config resources.
Couldnāt agree more. And all my subsystem scripts extend āNodeā so no issue there, and Iām now using the Class Name in preference in my declarations.
I tend to preload it as a Node, never an object. Again that doesnāt change anything, I just think its a bit more readable when you instantiate it later on