Any varible change being only visible through the function that changed the varible

Godot Version
4.1.2 stable Linux x86 64bit appimage

When trying to make a system to save a tilemap upon exiting the game, a dictionary meant to keep track of each tile returns it’s starting value despite a function being called to “update” said variable where the variable is changed only when it is looked at from that function and nothing else.

var tileset = {}
func update_tileset():
	for xy in get_used_cells(0):
		if xy not in tileset:
			tileset[xy] = solve_for_xy(xy)
		if tileset[xy][1]==0:
			set_cell(0, xy, 0, Vector2i(clamp(tileset[xy][0][0]-1, 0, 2), tileset[xy][0][1]))
			tileset[xy] = solve_for_xy(xy)
			print(tileset[xy])
		elif tileset[xy][1] < -50:
			if round(randi()%clamp(500+tileset[xy][1], 1, 500)) == 0:
				erase_cell(0, xy)
				tileset.erase(xy)
		tileset[xy][1] -= 1
	return tileset
		

when calling the function and looking at the return or the tileset it will be {} or the starting value of the tileset varible. Is there a way to fix this?

Hate to be the “did you turn it off and on again” but is the function ever called

yes the function is called every second, upon loading, and upon closing the game.

1 Like

put print commands throughout the function to check exactly what it’s doing

I did previously this is the result (sorry if this is little info) (it is returning what it should)
this is for a couple cycles:
[(1, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(1, 0), 1]
[(1, 0), 1]
[(1, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 1), 3]
[(1, 1), 3]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(1, 1), 3]
[(0, 0), 1]
[(0, 1), 3]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 2), 5]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(1, 2), 5]
[(1, 2), 5]
[(0, 2), 5]
[(0, 0), 1]
[(0, 1), 3]
[(0, 1), 3]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 0), 1]
[(0, 1), 3]
[(0, 0), 1]
[(0, 1), 3]
— Debugging process stopped —

Is the variable you’re printing any different to the one you’re trying to return? Also how are you calling it, are you checking the original variable or calling the function as a variable?

The variable is the same tileset one as the one being returned, I have tried both checking the original and calling the function, both return the starting value.

Could try typing it more staticly. Firstly putting the variable type of the function after the colon and put tileset in the brackets both in the function and when calling it. Even if it fixes nothing you might get useful error messages

var tile = {}
func update_tileset() -> Dictionary:
        tileset = tile
	for xy in get_used_cells(0):
		if xy not in tileset:
			tileset[xy] = solve_for_xy(xy)
		if tileset[xy][1]==0:
			set_cell(0, xy, 0, Vector2i(clamp(tileset[xy][0][0]-1, 0, 2), tileset[xy][0][1]))
			tileset[xy] = solve_for_xy(xy)
			print(tileset[xy])
		elif tileset[xy][1] < -50:
			if round(randi()%clamp(500+tileset[xy][1], 1, 500)) == 0:
				erase_cell(0, xy)
				tileset.erase(xy)
		tileset[xy][1] -= 1
	return tileset
func test_function():
        tile = update_tileset()
        print(tile)
        print(update_tileset())

I tried this /\ but the only thing that happened is when I set the variables the same name it returned null but when the names where separate it didn’t return any error and returned the same starting varible as before

var tile = {}
func update_tileset() -> Dictionary:
        tileset = tile

Nah this wouldn’t do anything I meant

var tileset = {}
func update_tileset(tileset) -> Dictionary:

and when calling the function write it as update_tileset(tileset)
But that probably wouldn’t do anything either…
Maybe var tileset:Dictionary = {}?

I tried that previously same result, however I haven’t tried the
var tileset:dictionary = {}

Edit: I just tried the var tileset:Dictionary, still nothing

replace to tile=tileset

Error message:
Trying to assign a value of type "nill" to a variable of type "dictionary"
Edit: that was because I forgot to remove something

nothing changed still the starting value.

try pass a tile as arg

tile is passed as an argument already, that is what the (tile) part is.

Found a solution, for some reason trying to access anything that is not being ran when a tab is closing will just get the default value of the thing you are trying to access.
In this case I tried to access the code file from the one with the save function when the notification of window close was given, thus when trying to access a variable of a file that has been unloaded it will just give the default value and not the one that has been edited.
To work around this I made the program save the file every second by calling the save function with the info I had to save so that it would write it into the save file which removes the need to save the game when the window is being closed.
This is not that efficient but at this point I spent about 3 days trying to fix this so I am not that bothered.

1 Like