Script Preloads

Godot Version

4.3 Stable

Question

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.

In my testing using Object is marginally quicker.

Kindly

Ryn

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.

1 Like

Thanks,

I am aware I can remove the type, but I prefer to have all my variables statically typed and Iā€™ve set the system to generate an error if not.

I just did a get_class() on one that was typed as an Object and it return a class of Node, so Iā€™ve changed all references to node.

I was just curious, wasnā€™t having any issueā€¦ Thanks for the feedback.

Kindly

Run

I presume your IRQSystem is a class. If so you can be more specific (as Node is very general) like this:

@onready var irq: IRQSystem = preload("res://Scripts/SubSystems/irq_system.gd").new()

Presuming your class is ā€œIRQSystemā€.

1 Like

Hi, Thanks for the reply,

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.

Cheersā€¦

Kindly

Ryn

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.

2 Likes

Thanks @gertkeno,

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. :slight_smile:

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.

Kindly

Ryn

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.

1 Like

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.

Thanks Everyone for your insights.

Kindly

Ryn

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

1 Like