Making variables change across scenes

Godot Version

4.3 stable

Question

I’m very new to Godot, and I’ve been having trouble getting variable changes to move across scenes. For example,

main

var example = false
func _ready():
example = true

other file

func _ready():
print(example)

The print always outputs false, no matter what I do. Is there any way to fix this?
P.S. I’ve tried using autoload, I still have the same problem.

Your other file doesn’t have an example variable so it should give a syntax error. If you use a global/autoload then you need to specify the global’s name then it’s property, even then on _ready() it may be a race condition

func _ready() -> void:
    print(MyGlobal.example)

My example wasn’t near detailed enough, so I’ll just paste in part of my actual code.

global storage

sq_ready :=false
func _on_start_timer_timeout() → void:
sq_ready = true

main

func _init():
if varStor.sq_ready == true:
print(“block ready”)
while true:
if varStor.activePieces.front() == pieceName:
attachBlock(square_block, in_play_piece)
elif varStor.activePieces.back() == pieceName:
attachBlock(square_block, next_piece)

Could you format your code paste?

sq_ready :=false

func _on_start_timer_timeout() → void:
    sq_ready = true

 func _init():
	if varStor.sq_ready == true:
		print("block ready")
		while true:
			if varStor.activePieces.front() == pieceName:
				attachBlock(square_block, in_play_piece)
			elif varStor.activePieces.back() == pieceName:
				attachBlock(square_block, next_piece)

Is this really one script? the first line is invalid, you probably ment to add var before.

var sq_ready := false

But in your original post you mentioned that this, and the timeout function are part of the global script, I’m guessing named varStor

# varStor.gd
var sq_ready := false

func _on_start_timer_timeout() -> void:
    sq_ready = true

This is fine and sq_ready is accessable, but your _init function would freeze your game and I’m not sure what you want from it, the while true: will never end the frame. You are quite lucky sq_ready is false to start, can you explain what you intend to do and why you reached this _init conclusion?

I’m using _init because I wanted a way to run code for only the instances of an object and not the main object, and that’s as close as I could get.

_init will not do that, it runs for all objects before _ready(), it’s quite prone to causing issues instead of helping.

Instances are no different from nodes place in-editor, why do you want to run code only for instances? Could you share more about this problem? What specifically do you want to do for a “main object” that shouldn’t happen for instances? Are you duplicating the “main object”?

I want to be able to create copies of an object that can do things while the original is unaffected. The “main object” is the one that is being duplicated.

Then outside of specific resources that should be true, duplicates do not edit the original. You should mark any resources the original instance has “Local to Scene” to avoid them being shared among the copies. There are also some flags you could specify when duplicating

What specifically among your duplicates is being shared?

var clone = original.duplicate(Node.DUPLICATE_USE_INSTANTIATION)
add_sibling(clone)

I worded my answer wrong. Nothing is being shared among the duplicates. The methods being called in the duplicates are also running on the original, and many of said methods delete crucial parts of the object, such as a specific hitbox that needs to be deleted at a certain point.

Also, can we please go back to the initial problem of variable changes not registering across scenes?

Globals do transfer between scenes, your last code paste should work, what is happening vs what do you expect to happen?

When I run the code, it’s supposed to change sq_ready to true when the timer goes off, which it does, but that change doesn’t register anywhere else for some reason. It registers in varStor, but nowhere else.

right, but if it’s a global you can access varStor.sq_ready anywhere else correct?

I’m not getting an errors, and sq_ready gives me a value anywhere, it just never changes from the value at definition: false.

How are you testing this? If you add a print statement after the start timer, and a print statement to these _init functions what order does it say?

A print statement that triggers when the timer goes off works every time, but the one in _init never triggers unless I set the value to true manually

if _init is never run, could you use _ready instead?

I tried that after you brought up that _init doesn’t do what I thought, and it gave me the same outcome

_ready is never run either? What does this script extend? could you paste the entire script with your print statements?