Passing values using OnConstruction

Is OnConstruction the same as blueprint construction script?
I was able to pass value to a float variable in blueprint but unable to do so in OnConstruction. It is as simple as Fval = 10, with the Fval in “.h” as “float Fval;” or “float Fval = 0;” . I did a UE_Log in OnConstruction to show the changes as Fval = 10, but it default back to 0 during begin play.

Hey pickersZ-

If you are attempting to set the default value of a variable through code, then you’ll want to do so in the constructor rather than the OnConstruction function. OnConstruction gets called when the actor is updated in the editor. This means when you move the actor in the viewport or compile the actor’s blueprint.

Cheers

I was trying to determine the peculiar behaviour of OnConstruction. After reading your reply and giving some thoughts about it. I tried the float variable again with UPROPERTY as i remembered OnConstruction was for changes to “exposed” editor values for that code UPROPERTY(EditAnywhere)?.

It showed the correct values in OnConstruction and BeginPlay. I am assuming same behaviour for UFUNCTION as well. I was puzzled by why some work and other dont. But things are getting clearer now. Thanks !

Here again i have tried to pass in values for a tarray of int32 and another tarray of a struct in OnConstruction. Apparently only values for tarray of int32 was captured but tarray of struct wasnt in begin play. Both having the UPROPERTY tag to prevent them from crashing. If anyone managed to pass values of tarray of struct thru OnConstruction, please advise!

Edit::
I have solved it “again”. UPROPERTY are required within the struct as well, not just the TArray. It didnt work the first time, deleting the actor and putting it back on scene didnt do the trick. Restarting the editor helped to refresh the changed value.