0 votes

I am trying to get the value of a bool loaded in a singleton pattern. This works:

    _gameVariables = GetNode<gameVariables>("/root/GameVariables");
    GD.Print(_gameVariables.boolExample);

This returns the correct value of the bool named "boolExample".

However, I also need to be able to change the reference of the bool that is checked from one instance of the script to an other. So here's what I tried that doesn't work:

    _gameVariables = GetNode<gameVariables>("/root/GameVariables");
    variableToCheck = "boolExample";
    bool value = Convert.ToBoolean("_gameVariables." + variableToCheck);

    GD.Print(value);

Although "GD.Print" never prints any value for it returns the following error "Boolean System.Boolean.Parse(System.ReadOnlySpan`1[System.Char] ): System.FormatException: String was not recognized as a valid Boolean."

What am I doing wrong ?

Thank you for your time.

Godot version 3.4.3
in Engine by (36 points)

Rather than trying to access values by constructing variable names from string references, you're probably better off storing all of the values you're interested in in a single, global (singleton) Dictionary where the key name is your "variable" and the key value is your value. That way, you just need a reference to the single Dictionary, and you can easily access any key (and therefore its value) by its string name.

That's actually a more elegant solution. I'm not used to the "etiquette" of that forum so I'll just post what I did here, based on your advice (which would be the answer of that question).

For future references:

I've created a dictionary loaded in "autoload":

gamevariables = new Godot.Collections.Dictionary
{
    { "ExampleKeyA", "true"},
    { "ExampleKeyB", "false"},
    { "ExampleKeyC", "true"},
};

Then, in a script that is meant to be instanciated, I've wrote this (working) code:

        [Export]
        public string variableToCheck;

        if (_gameVariables.gamevariables[variableToCheck].Equals("true"))
        {
            ///Do things
        }

Thank you jgodfrey.

Keep in mind, the values in your Dictionary can be a mix of types. So, rather than storing the boolean value as a string you can actually store it as a boolean - which makes testing it less verbose and (presumably) faster...

For example:

var dict = {"a": true, "b": 10, "c": "some string"}
if dict["a"]:
    print("TRUE")
else:
    print("FALSE")

1 Answer

0 votes
Best answer

Converting the initial comment above to an Answer for visibility on the main page:

Rather than trying to access values by constructing variable names from string references, you're probably better off storing all of the values you're interested in in a single, global (singleton) Dictionary where the key name is your "variable" and the key value is your value. That way, you just need a reference to the single Dictionary, and you can easily access any key (and therefore its value) by its string name.

(see conversation above for more details).

by (12,475 points)
selected by
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 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 webmaster@godotengine.org with your username.