0 votes

Hello community,

I have an AutoLoad Script "GlobaleVariable.gd" that has two dictionaries dictPersons and dictFriends. Both dictionaries are empty at start and get filled by function. dictPersons by a CSV reading function and dictFriends by values in code. This works good. Both globals are filled.
BUT:
I also assigned some (local) varables on different places in the main script "TestVars.gd" (outside/inside of "func _ready():" ) in the style: onready var test = GlobaleVariable.dictPersons.
I expected, these variables are identical to the global ones (like pointers to same memory address) but they aren't.
The local variables, that have been assigned to "dictPersons" before the filling function was called, are empty. The one, that was called after, is filled correctly.
I thought: Ok, maybe a lag in the update of the local variables.
BUT:
The variable, that has been assigned to GlobaleVariable.dictFriends before its filling function has been executed, is filled correctly.

Shortform: Why is TestA1 empty and TestF1 isn't.

Has someone an explanation?

Code: GlobaleVariable.gd

extends Node
onready var dictPersons = {}
onready var dictFriends = {}

Code: TestVars.gd

extends Node

onready var Test_A1 = GlobaleVariable.dictPersons
onready var Test_F1 = GlobaleVariable.dictFriends

var ScriptDict = {}
onready var Test_B1 = ScriptDict

func _ready():
    var Test_A2 = GlobaleVariable.dictPersons
    var Test_B2 = ScriptDict

    var FunctionDict = {}
    var Test_C2 = FunctionDict

    FunctionDict = fun_Read_CSV_To_Dict()
    var Test_C3 = FunctionDict

    fun_Add_Friends()

    print("After_Read_CSV_To_Dict")

    print("\n\r")
    print("Singleton: GlobaleVariable.dictPersons: ")
    print(GlobaleVariable.dictPersons)

    print("\n\r")
    print("Test_A1, member variable, assigned before .csv read: ")
    print(Test_A1)

    print("\n\r")
    print("Test_A2, function variable, assigned before read .csv:")
    print(Test_A2)

    print("\n\r")
    print("Member: ScriptDict: ")
    print(ScriptDict)

    print("\n\r")
    print("Test_B1, member variable, assigned before .csv read:  ")
    print(Test_B1)

    print("\n\r")
    print("Test_B2, function variable, assigned before read .csv:")
    print(Test_B2)

    print("\n\r")
    print("Inside Function: FunctionDict: ")
    print(FunctionDict)

    print("\n\r")
    print("Test_C2, function variable, assigned before read .csv:")
    print(Test_C2)

    print("\n\r")
    print("Test_C3, function variable, assigned after read .csv:")
    print(Test_C3)


    print("\n\r")
    print("Test_F1, member variable, assigned before the singleton has been changed. Similar to Test_A1 but not reading from .csv: ")
    print(Test_F1)

func fun_Add_Friends():
    GlobaleVariable.dictFriends["Name"]=["Tom", "Tina"]
    GlobaleVariable.dictFriends["Age"]=[12, 14]

func fun_Read_CSV_To_Dict():
    var file = File.new()
    var bolFirstLine = true
    var arrReadLine = []
    var dictDB = {}
    var arrHeadLine = []
    var strColumn = ""
    var strAgesFile = "res://Ages.csv"



    file.open(strAgesFile, file.READ)
    while !file.eof_reached():
        arrReadLine = file.get_csv_line("\t")
        if bolFirstLine == true: #Headline
            arrHeadLine = arrReadLine
            for intIndex in arrHeadLine.size():
                strColumn = arrHeadLine[intIndex]
                dictDB[strColumn] = []
            bolFirstLine = false
        else: #Dataline
            for intIndex in arrReadLine.size():
                if arrReadLine.size() == arrHeadLine.size(): #don't read empty or wrong lines
                    strColumn = arrHeadLine[intIndex]
                    dictDB[strColumn].append(arrReadLine[intIndex])
    file.close()
    GlobaleVariable.dictPersons = dictDB
    ScriptDict = dictDB
    return  dictDB

Output:

--- Debugging process started ---
Godot Engine v3.2.3.stable.official - https://godotengine.org
OpenGL ES 3.0 Renderer: Intel(R) HD Graphics 620

AfterReadCSVToDict

Singleton: GlobaleVariable.dictPersons:
{Age:[55, 48], Name:[Harry, Lisa]}

Test_A1, member variable, assigned before .csv read:
{}

Test_A2, function variable, assigned before read .csv:
{}

Member: ScriptDict:
{Age:[55, 48], Name:[Harry, Lisa]}

Test_B1, member variable, assigned before .csv read:
{}

Test_B2, function variable, assigned before read .csv:
{}

Inside Function: FunctionDict:
{Age:[55, 48], Name:[Harry, Lisa]}

Test_C2, function variable, assigned before read .csv:
{}

Test_C3, function variable, assigned after read .csv:
{Age:[55, 48], Name:[Harry, Lisa]}

TestF1, member variable, assigned before the singleton has been changed. Similar to TestA1 but not reading from .csv:
{Age:[12, 14], Name:[Tom, Tina]}
--- Debugging process stopped ---

Godot version Godot Engine v3.2.3.stable.official - https://godotengine.org OpenGL ES 3.0 Renderer: Intel(R) HD Graphics 620
in Engine by (12 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.