UProperty changes on editor not reflected in execution c++

// Number of particles in the chain
UPROPERTY(EditAnywhere, Category = “General Properties”, meta = (DisplayName = “Number of Particles”))
int nParticles = 0;

// Number of particles in the chain
UPROPERTY(EditAnywhere, Category = "General Properties", meta = (DisplayName = "Number of Particles v2"))
int nParticlesV2;

My variable is shown on the editor and I change it. When I debug my code, the value on the editor is not reflected in the code while executing. It’s always 0. Initialized or not.

I just tested in a new empty Actor class. Added the uproperty the same way (V2). I compile, everything goes ok. Then I modify the property on the actor using the property inspector in the editor. Then I hit play. Printing the variable on the constructor of that actor prints 0… what’s happening here?

= 0; is setting a default value for the variable. That’s absolutely legit. For Unreal, though, if you do that, it will ignore the value on the editor. So we shouldn’t add any initialization at all. I got a longer answer, I’ll add it. Thanks for helping!

Things I learned from this error:

  • Never initialize or give a default value to any UProperty. Otherwise, it will ignore the value from the editor. The default value should be taken from the editor.

  • Never use the UProperty values in the constructor. Do it in BeginPlay() method. Otherwise, the values are not set from the editor.

You have

int nParticles = 0;

I’m not sure how it works in unreal, but if you want to setup default value, do it inside construction script.