Can globals in Godot randomly fail to update?

  • Thread starter Thread starter Darkmisc
  • Start date Start date
  • Tags Tags
    godot
AI Thread Summary
The discussion centers around a game development issue related to managing global variables in Godot. The user is creating a game to memorize pairs of items, specifically countries and their capitals, stored in two arrays. They encountered a problem where the global dictionary sometimes appeared empty, despite the arrays containing data. After modifying the save function to explicitly update the global dictionary whenever the arrays change, the issue seemed resolved, but the user sought confirmation on whether global values fail to update automatically.Responses highlighted that global variables can lead to unexpected behavior if not managed carefully. It was suggested that the user might have inadvertently assigned new instances to the arrays without updating the global dictionary accordingly. The consensus is that using global variables should be avoided when possible, and that local functions should operate on parameters instead. Additionally, recommendations were made for improving programming skills, including taking foundational courses like CS50 and utilizing Godot's documentation, which emphasizes object-oriented programming principles.
Darkmisc
Messages
222
Reaction score
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:
[CODE lang="python" title="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[/CODE]

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?

[CODE title="new save function" highlight="2,3"]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[/CODE]Thanks
 
Technology news on Phys.org
Its hard to tell without seeing your other code, but one could speculate that you somewhere else set you Global.dict to one set of array instances and then later change your Global.arrayA to another instance (e.g. you do Global.arrayA = ... without also doing Global.dict = { "arrayA": Global.array, ...} right after. If so you should either remember to set Global.dict every time, or only update your Global.arrayA instance without reassinging a new instance to it (e.g. using only insert/update/remove of elements), or even better, if you only use Global.dict for saving just drop it from Global and do file.store_var({ "arrayA": Global.arrayA, "arrayB": Global.arrayB }) every time.

By the way, the common consensus is that global variables is something to be avoided if possible. You will likely benefit if you make your local functions work on parameters instead of globals.
 
Darkmisc said:
Do global values sometimes fail to update automatically?
Given the "level" of your questions I think it is fair to assume that when you get unexpected behavior for your own code in Godot it is almost certainly because you made a code or design bug, possibly based on a false understanding of how code works, i.e. the relevant consequences for each specific piece of code.
 
Filip Larsen said:
Its hard to tell without seeing your other code, but one could speculate that you somewhere else set you Global.dict to one set of array instances and then later change your Global.arrayA to another instance (e.g. you do Global.arrayA = ... without also doing Global.dict = { "arrayA": Global.array, ...} right after.
That's what I did. I didn't know I had to manually update Global.dict whenever I changed Global.arrayA (or arrayB).

Now, I'm surprised it worked at all.

I think it makes sense why lines 2 and 3 fix the problem now.
 
  • Like
Likes Filip Larsen
If you are using globals then it is insane to have the same variable represented twice as both arrayA and dict['arrayA']. And these are terrible names for variables.

Filip Larsen said:
By the way, the common consensus is that global variables is something to be avoided if possible. You will likely benefit if you make your local functions work on parameters instead of globals.

Absolutely - and this is something that the makers of the Godot engine try to reinforce. I don't think you will find a single example at https://docs.godotengine.org/ that uses globals, and the tutorial starts off with lots of good advice like: https://docs.godotengine.org/en/sta...odot.html#what-do-i-need-to-know-to-use-godot

To make the most of [Godot], you need good programming foundations. While we try to make the engine accessible, you will benefit a lot from knowing how to think like a programmer first.

Godot relies on the object-oriented programming paradigm. Being comfortable with concepts such as classes and objects will help you code efficiently in it.

If you are entirely new to programming, we recommend following the CS50 open courseware from Harvard University. It's a great free course that will teach you everything you need to know to be off to a good start. It will save you countless hours and hurdles learning any game engine afterward.

I suggest that you go back to the beginning of your journey, follow CS50 and the GDScript introduction and examples and learn how to program properly instead of asking us to fix your code that is broken by design.
 
  • Like
Likes Darkmisc and Filip Larsen
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top