Global variables not updating

Godot Version

4

Question

Hi,
I’ve been trying to figure this out myself but can’t seem to understand what the problem is.

I have 3 scripts. One for a letter. One for a letterbox. And a main scene which is a global script.

In the letter script I assign an address, which is a random number.

in the letter box script I assign an address through an export variable. (1,2,3,etc.)

Then, in the main script, I spawn a letter on a timer, and if they match they will be posted (I haven’t written this part yet.)

The problem I’m having is that the address value doesn’t get passed to the main script. And only the highest letterbox value gets passed.

I don’t know what I’m doing wrong but any help would be appreciated.

Thanks,

I don’t know what is wrong, but have you figured out the solution? I am mostly replying so if you didn’t figure it out others will see this.

There are several things that could potentially be wrong here. Showing the TestScene class would help.

In the main script, the problem seems to be that letter address is never assigned a value.

In spawn_letter, this line might help:

letterAddress = instance.address.text

In general though, you should be using a resource to pass data around. Embedding the data in a scene means you have to instantiate the scene to read the data.

If it’s a main scene then it shouldn’t be a global script and vice versa. If you’ve set a script as a global autoload and you also have it attached to main scene top node, then your script is running twice “at the same time”, which is almost guaranteed to result in bugs.