Can globals in Godot randomly fail to update?

  • Thread starter Darkmisc
  • Start date
  • Tags
    godot
  • #1
Darkmisc
204
27
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:
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
Thanks
 
Technology news on Phys.org
  • #2
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.
 
  • Like
Likes Darkmisc
  • #3
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.
 
  • Like
Likes Darkmisc
  • #4
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
  • #5
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

Similar threads

  • Programming and Computer Science
Replies
1
Views
546
Back
Top