Download

Varible Bug

Edit:
It seems compiling blueprints again after compiling code fixes the problem. But I still wonder why this happens?

Hello,

I have following structure in my Character.h file:


    
...

UPROPERTY(EditDefaultsOnly, Category = "ObjectInteraction")
float MaxUseDistance;

int Gold;
int Experience;
int Level;

...


I display values of Gold, Experience and Level in my UI.

If I declare another variable before Gold, values for these three variables get messed up and I get garbage values such as “1.120.403.456”.

For example, following code causes the bug I said:


    
...

UPROPERTY(EditDefaultsOnly, Category = "ObjectInteraction")
float MaxUseDistance;

int a;

int Gold;
int Experience;
int Level;

...


However, if I declare a variable after “int Level;” line, I get no problems.
What may be the cause of this? Is it a bug or something that I miss? Can UPROPERTY macro have something to do with it?

I initialize these variables in the constructor of Character class.

I realized that If I log these values to console then correct values are logged. I used blueprints to display these values on user interface which seems to cause problems. I tried putting “int Gold” line under “int Level” line and character level started to display gold, seems like a pointer which points wrong location. But only wrong in blueprints.

In C++, unlike other languages you may have used?, declaring a variable does not initialize it to zero. You should set it’s value either in the Constructor or using “int Gold = 0;” at declaration time. (i’m not sure exactly what the specific differences between doing those two things are, i always use declaration initialization)