Can globals in Godot randomly fail to update?

  • Thread starter Darkmisc
  • Start date
  • Tags
    godot
In summary, the topic discusses the potential issues with global variables in the Godot game engine, specifically addressing whether these variables can fail to update randomly. It highlights that while global variables are designed to maintain consistent values across different scenes and scripts, certain programming practices, such as improper initialization or scope management, can lead to unexpected behavior or failures in updating. The discussion emphasizes the importance of careful coding practices to ensure reliable functionality of global variables in game development.
  • #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:
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

FAQ: Can globals in Godot randomly fail to update?

1. What are globals in Godot?

Globals in Godot refer to global variables or singleton instances that can be accessed from anywhere in your project. They are often used to store data or states that need to be shared across different scenes or scripts without the need for passing references explicitly.

2. Can globals in Godot fail to update?

Yes, globals in Godot can fail to update under certain circumstances. This may happen if the global variable is not properly referenced or if the code that updates the global variable is not executed due to logic errors, incorrect signal connections, or other issues in the game loop.

3. What could cause a global variable to not update correctly?

Several factors can cause a global variable to not update correctly, including scope issues, where the variable is being overwritten unintentionally, or if the code that modifies the global variable is placed in a part of the script that does not get called (like in a condition that never evaluates to true).

4. How can I troubleshoot issues with global variables not updating?

To troubleshoot, you can use print statements or the debugger to check when and where the global variable is being updated. Ensure that the code responsible for updating the variable is being executed as expected, and check for any potential overwrites or conflicting assignments.

5. Are there best practices for using globals in Godot?

Yes, best practices include limiting the use of globals to necessary cases, keeping global variables organized within a dedicated singleton script, and ensuring that you have clear documentation on how and when globals are updated. This helps maintain clarity and reduces the risk of unintended behaviors.

Similar threads

Back
Top