I am having a brain aneurism. The “const OBJ_WATER_JUG = preload” calls I had in multiple scenes are all fine and were fine, but then I tried adding a new const and now anywhere I add a const that didnt exist before has an issue with the object its trying to get. Its weird because Im just adding a reference to it, Im not even using it in any code. The error I get is telling me that (its somewhat convoluted to explain) The func_Referenced_node has a node called “Components”, this node holds a bunch of generic nodes with behavior scripts (take damage, etc). I am getting an error that the scripts in the component nodes are failing to recognize properties such as “Thing_type” in the parent node of the packed scene. it only calls if an instance of the scene actually exists in world, but there is no connection between the script I am referencing the file from and the actual existent object that I dragged in from files. Its difficult to explain in text so I will attach images that explain this more clearly. This makes no sense at all to me and I am completely stuck. Please help
basically, if any instance of the object exists in the current scene, as an instantiated packed thingey (isnt local, has the little movie clip icon) AND there is any reference to the object in any script I have tried, even if the variable that script puts it into (for example, if “const VARIABLE = preload(“thing”), and “VARIABLE” is never used anywhere but to declare itself), then the object script, the components of those instance things, or something breaks, and now the component pieces of the instance cannot see the variables declared in the parent object node (for example, the RigidBody3D “Water Jug” has a variable in its script called “Thing_type”. It’s .name, .linear_velocity, etc are readable, but component scripts crash out trying to read .Thing_type, etc)
It is a good idea to avoid preload unless the resource is very small and has no dependencies. Using preload in a script, even if the resulting value is never used, will require the designated resource with the script before the game even begins.
For resources and packed scenes you should almost always use load, and @export does a similar thing, for large scenes it’s much better to @export_file("*.tscn") as a string and load that when applicable.
Preloading scripts allows you to use them as a static type without a class_name, this is the only case I can think of where preload is absolutely needed, and it’s only particularly useful for making self-contained addons.
load: I cant seem to find resources on it since its a pretty generic word, most stuff that comes up has to do with save/loading game progress
@export: As I understood it, @export allows for variables that can vary between nodes with the same script by being set on the node itself? I just tried it and its not working, the variable turns null
@export var bullet: PackedScene
# can be converted to
@export_file("*.tscn") var bullet: String
# then used like so
var scene: PackedScene = load(bullet)
var instance: Node = scene.instantiate()
Preloads → Loads
const bullet = preload("res://bullet.tscn")
# can be converted to
var bullet = load("res://bullet.tscn")
# Following the previous example it may be even better to use a const path
const bullet_path = "res://bullet.tscn"
# then used similarly
var scene: PackedScene = load(bullet_path)
var instance: Node = scene.instantiate()
You’re correct that’s what exports are good for, the syntax is slightly incorrect and you must assign the value in the inspector.