Unable to get the property value in c++ which was previously set in bluprint Editor

Hi, I am not quite familiar with the loading order between c++ code and unreal blueprint, I have the following 2 properties as C++ protected fields:

    UPROPERTY(EditAnywhere,BlueprintReadWrite, Category="Health")
	float MaxHealth;
	UPROPERTY(EditAnywhere,BlueprintReadWrite, Category="Health")
	float CurrentHealth;

I set both properties value as 100.f in the constructor,

later changed their value to 200.f in blueprint editor,

then I run the game, in the breakpoint inside the BeginPlay function, I noticed the value of both properties were still the original 100.f.

Is there a way to load the new value which set in blueprint? I tried to set properties as BlueprintReadWrite, but looks like it doesn’t work, Can someone tells me how to solve it?

Are you spawning an instance of your blueprint class, or an instance of your native class (AObstacleActor) ?
If you want your actor to have the default values of the blueprint class, you need to spawn the blueprint class.
If you are spawning from blueprint, it’s pretty trivial.
If you are spawning from c++ it’s a bit more annoying. You can use LoadClass to get the dynamic class, for prototyping :

UClass* BPClass = LoadClass<AObstacleActor>(nullptr, TEXT("/Game/Blueprints/BP_MyClass.BP_MyClass_C"));

AObstacleActor* Actor = GetWorld()->SpawnActor<AObstacleActor>(BPClass , Transform);

Further down the line it is better practice to avoid hardcoded paths in code.
It would be better to declare an obstacle class property somewhere, such as in GameMode class :

TSubclassOf<AObstacleActor> ObstacleClass;

Then in child blueprint gamemode, point that property to the blueprint obstacle class.
Then you can spawn it from c++ easily like so :

AObstacleActor* Actor = GetWorld()->SpawnActor<AObstacleActor>(ObstacleClass, Transform);
1 Like

Thanks a lot for solving my problem, especially the explanation of difference between native class and blueprint class, now I know the reason it doesn’t work is because I was spawning the actor with the native class, instead I should use the blueprint class, since the parameter modification was on blueprint. and I will try your advice about finding a best practice to maintain the whole code structure.