- #1
Darkmisc
- 220
- 31
- TL;DR Summary
- I had a save function that would randomly fail to save. This function was supposed to save a dictionary from globals, but it would randomly save as empty. I think I've fixed the problem, but I'd be more confident if someone could confirm that my assessment of the cause is correct.
Hi everyone
I'm making a game for memorising pairs of things entered by the user (e.g. countries and their capitals).
If we're using countries and capitals, I'll store countries as an array in Globals.arrayA. Capitals get stored as Globals.arrayB.
The two arrays are stored in globals as
var dict = {"arrayA": arrayA,
"arrayB": arrayB}
My original save function was:
Sometimes, Global.dict would print out as empty, while Global.arrayA would print out with items in the array. Global.arrayA does get emptied (by code note shown here), but it should always be full again when the user hits save (the save button is disabled if Global.array < 3).
This led me to guess that for some reason, Global.dict sometimes thought Global.arrayA was empty, even though it wasn't.
I added lines 2 and 3 to the function, and so far haven't encountered the error again. However, since the error seemed to arise randomly, I'd feel more confident that I've solved the issue if someone could confirm that my guess about the cause was correct.
Do global values sometimes fail to update automatically?
Thanks
I'm making a game for memorising pairs of things entered by the user (e.g. countries and their capitals).
If we're using countries and capitals, I'll store countries as an array in Globals.arrayA. Capitals get stored as Globals.arrayB.
The two arrays are stored in globals as
var dict = {"arrayA": arrayA,
"arrayB": arrayB}
My original save function was:
original save function:
func save():
print("func_save before save Global_dict=", Global.dict)
print("func_save. before save. arrayA = ", Global.arrayA)
if Global.arrayA.size()>2:
var file = FileAccess.open(save_path, FileAccess.WRITE)
file.store_var(Global.dict)
print("func_save after save Global_dict=", Global.dict) print(save_path)
print("func_save arrayA= ", Global.arrayA)
print("func_save arrayB= ", Global.arrayB)
$Label2.text=str(Global.dict)
else:
$AcceptDialog.visible=true
Sometimes, Global.dict would print out as empty, while Global.arrayA would print out with items in the array. Global.arrayA does get emptied (by code note shown here), but it should always be full again when the user hits save (the save button is disabled if Global.array < 3).
This led me to guess that for some reason, Global.dict sometimes thought Global.arrayA was empty, even though it wasn't.
I added lines 2 and 3 to the function, and so far haven't encountered the error again. However, since the error seemed to arise randomly, I'd feel more confident that I've solved the issue if someone could confirm that my guess about the cause was correct.
Do global values sometimes fail to update automatically?
new save function:
func save():
Global.dict = {"arrayA": Global.arrayA,
"arrayB": Global.arrayB}
print("func_save before save Global_dict=", Global.dict)
print("func_save. before save. arrayA = ", Global.arrayA)
if Global.arrayA.size()>2:
var file = FileAccess.open(save_path, FileAccess.WRITE)
file.store_var(Global.dict)
print("func_save after save Global_dict=", Global.dict) print(save_path)
print("func_save arrayA= ", Global.arrayA)
print("func_save arrayB= ", Global.arrayB)
$Label2.text=str(Global.dict)
else:
$AcceptDialog.visible=true