How to use FAutoConsoleVariableRef with class member variable

I am trying to use FAutoConsoleVariableRef with a existing class member variable to prevent the GetValueOnGameThread() call in every tick of my application. I want to use my class variable as a cache for the value which should simply be updated if the value changes from the console. However, I get a compiler error when trying to use FAutoConsoleVariableRef. Stripped down example PlayerCharacter.h/.cpp:

class APlayerCharacter : public ACharacter
{
private:
    FAutoConsoleVariableRef CVar_MyVar;

public:
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
    float MyVar = 1.0f;

    // ...
};

APlayerCharacter::APlayerCharacter(/* ... */) :
        Super(/* ... */),
        CVar_MyVar(TEXT("MyVar"),
                MyVar,
                TEXT("Some Description"),
                ECVF_Scalibility | ECVF_RenderThreadSafe)
{
    // ...
}

So I want to initialize the variable with the constructor like one would usually do. But for some reason, UE generates a file called PlayerCharacter.gen.cpp which generates another constructor which overwrites my own one without initializing the CVar_MyVar which ultimately results in this error:

PlayerCharacter.gen.cpp:110:32: error: 
      constructor for 'APlayerCharacter' must explicitly initialize the member 'CVar_MyVar' which does not have a default constructor
        DEFINE_VTABLE_PTR_HELPER_CTOR(APlayerCharacter);
                                      ^
PlayerCharacter.h:19:26: note: member is declared here
        FAutoConsoleVariableRef CVar_MyVar;
                                ^
/Engine/Source/Runtime/Core/Public/HAL/IConsoleManager.h:824:16: note: 
      'FAutoConsoleVariableRef' declared here
class CORE_API FAutoConsoleVariableRef : private FAutoConsoleObject
               ^
1 error generated.

Any ideas on how to fix this are much appreciated.

Six years too late, but for everybody else interested, you need to initialize it in .h file (declaration of the class), so in your case

{
private:
    FAutoConsoleVariableRef CVar_MyVar = FAutoConsoleVariableRef(TEXT("MyVar"), MyVar, TEXT("Some Description"), ECVF_Scalibility | ECVF_RenderThreadSafe);
1 Like