Download

Cannot get correct console variable value when using a struct parameter

For the love of all the gods, can someone tell me what’s going on here.

I’m trying to get a console variable parameter based on a string value contained in a struct. So I have a struct called FCommandData which has an FString member Command. If I try to use that FString, it doesn’t get the correct value of the console parameter. If I use an FString manually defined to be the same value, then it works. Here’s some testing code:



        FString CommandString1 = CommandData->Command;
        FString CommandString2 = "a.AnimNode.LegIK.Enable";

        if (CommandString1 == CommandString2)
        {
            UE_LOG(LogTemp, Warning, TEXT("Strings are the same."));
        }

        static const auto CVar1 = IConsoleManager::Get().FindConsoleVariable(*CommandString1);
        int32 Value1 = CVar1->GetInt();
        UE_LOG(LogTemp, Warning, TEXT("%s: %i"), *CommandString1, Value1);

        static const auto CVar2 = IConsoleManager::Get().FindConsoleVariable(*CommandString2);
        int32 Value2 = CVar2->GetInt();
        UE_LOG(LogTemp, Warning, TEXT("%s: %i"), *CommandString2, Value2);

And here’s the output:


LogTemp: Warning: Strings are the same.
LogTemp: Warning: a.AnimNode.LegIK.Enable: 0
LogTemp: Warning: a.AnimNode.LegIK.Enable: 1

As you can see, the string values are the same. The correct value is 1 in this instance. As you can see, the manually defined FString gets that value, but the other version does not.

WHAT IS GOING ON HERE?!

OK, I have a slightly better understanding of the problem, but still no idea of a solution to this.

The problem is that this way of getting a console variable requires declaring it as a static variable:


static const auto CVar = IConsoleManager::Get().FindConsoleVariable(*CommandString1);

As the UE4 documentation states:

"The static there ensures the name search (implemented as map) is only done the first time this code is called. This is correct as the variable will never move and only gets destructed on engine shutdown."

But I am calling this code on a loop, bringing a different value into it each time. So what is happening is that the value of CVar is always the same. The first value that it gets from my loop is ‘0’. And so no matter what I try to get after that I will always get the value from the first time I called this code.

But I cannot seem to get the value of a console variable without doing it like that. All of these attempts:



IConsoleVariable* CVar = IConsoleManager::Get().FindConsoleVariable(*CommandString1);
or
const auto CVar = IConsoleManager::Get().FindConsoleVariable(*CommandString1);
or
auto CVar = IConsoleManager::Get().FindConsoleVariable(*CommandString1);

All leave me with CVar as a nullptr…

So how can I work around this? How can I get arbitrary console variables back from the Console Manager while using a variable to find them?

Scratch that, it actually does work with ‘auto CVar’. All good. Leaving this madness here in case anyone cares.