Const or Var for Preloads?

Godot Version

4.3

Question

I notice in the docs they show using a var for preloads, like this…

var diamond = preload("res://diamond.tscn")

…whereas if you ctrl+drag a scene from the file browser to the script editor, it defaults to a const like this…

const diamond = preload("res://diamond.tscn")

Is one or the other of these correct? Does it depend? And if so, what does it depend on?

My intuition would be that since you almost certainly won’t be changing the file path at runtime, that const would be the way to go, but I’ve also noticed at times I’ve seemingly had issues when using consts and instancing scenes, especially when for example I instance a scene, queue_free it and then try to load a new instance of that scene. I’ve not been able to recreate this consistently, so I’m not sure what the actual source of the issue is.

At any rate, thanks for any insight!

1 Like

yeah the difference is that you can change the value of the variable, but as you said you mostlikely wont change it so you probably should always use constants.
About the other issue, if you encounter it again feel free to post about it on the forum

2 Likes

That makes sense. Thanks!

1 Like

Ive been using this engine for about a year or two now and i never knew about ctl+drag! Thank you for sharing that shortcut.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.