Autoloads not working

Godot Version

4.2.2

Question

i have an autoload called shipSaving, and an array in it called savedCells. I can access the variable in the same script and get a correct result, but when i try to access it in another script with print(shipSaving.savedCells), it just prints an empty array

Can you paste your script that accesses it incorrectly?

extends node

func _process(delta):
     print(shipSaving.savedCells)

Could you give more information about your array that in your autoload script?

It’s an empty array that has a vector3i added to it every time the user places something.

Can you paste the code that is responsible for that?

func _on_block_placing_new_tile():
	var usedCells = tilemap.get_used_cells(0)
	for i in usedCells:
		var usedCellsId = tilemap.get_cell_source_id(0, Vector2i(i.x, i.y))
		if savedCells.has(Vector3i(i.x, i.y, usedCellsId)):
			pass
		else:
			savedCells.append(Vector3i(i.x, i.y, usedCellsId))

some of the code is odd because i was trying to find ways to fix it

There could be lots of scenarios, like: you forgot to call this function to add your tiles. Also it could be that you’re pasting not tiles. Could you provide more information? Maybe everything that regulates it and contacts with it

Is it possible you have more than one instance of shipSaving running?
One instance is getting the items added and the other just sits empty.
You can check this out by clicking on remote in the editor while running the game and looking for a second instance of shipSaving (or whatever the script is called)

Is this function you show in the autoload script?
func _on_block_placing_new_tile():

It will be easier to find the issue if you post the whole project to GitHub.

1 Like

maybe there is another savedCells variable in this script? Try ctrl+clicking savedCells here and see where it takes you, paste the location it ends up if you please

I have figured it out. something about the variable being changed in a signal function didn’t let it update for all the other things trying to access it, i’m just moving it to the process function

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