Question
Hello! First post on the forum.
Currently, I am trying to figure out how to connect variables between scenes so that the value remains constant between scripts.
I have already heard of autoloads, but I am a little confused on how to use them for this purpose, and there appear to be multiple downsides to using them for a lot of variables, so I was wondering if there were other methods to connecting variables across scripts. I am also curious if dictionaries could be used to circumvent having to create a large amount of autoloads.
I also heard about the possibility of connecting them with functions, but I am unsure how to set this up so that the value of the variable could still be changed.
I don’t quite know what you mean by “connecting them with functions”, but you can use setters and getters to run code whenever a property is set or accessed.
Example:
var two_plus_two = 4:
set(value):
if value != 4:
OS.crash("Two plus two must equal 4!")
two_plus_two = value
func _ready():
two_plus_two = 5 # Would crash the game
The idea of connecting the variables with a function was something that I had heard of in a tutorial I watched. The basic idea, from what I understood, was that it was a function that simply returned the value of a variable when called, and could be called from another script because it was a function.
Also, I looked into autoloads a little more and they actually seem to be the best option for what I’m trying to do, as I previously misunderstood autoloads for some reason only being able to store one variable per autoload. I don’t know why I thought this, but it resulted in me preferring to avoid them as I thought I would need a lot of autoloads instead of just one.
Sorry, to clarify, I meant that I’m unsure of how to set the values for a variable that is contained inside of a dictionary, or if that’s even possible.