The RoomColor property is marked as a UPROPERTY, which means it will be copied from the Class Default Object after the Constructor runs on the instance. Generating the random number in PostInitProperties rather than the constructor should resolve the issue.
When generating a random number using FMath::RandHelper() in the constructor of an actor, attempting to print that same number on BeginPlay returns a different value than the constructor.
However, the value printed on BeginPlay will always be the same in each editor session. For example, if it prints 1 the first time, it will continue to be 1 each time you PIE until you restart the editor.
Found in 4.10.2 binary. Reproduced in 4.9.2 and Main CL 2826455
1. Open the editor
2. Create a C++ Actor Class
3. Add this code to the header
UPROPERTY() int32 RandomNumber;
4. Add this code to the .cpp
*Constructor* RandomNumber = FMath::RandHelper(6); UE_LOG(LogTemp, Warning, TEXT("Constructor Number: %d"), RandomNumber); *Begin Play* UE_LOG(LogTemp, Warning, TEXT("Begin Play Number: %d"), RandomNumber);
5. In the editor, open the Output Log
6. Place an instance of the actor class in the level
Result: Constructor will print a randomized value to the output log each time you PIE, while BeginPlay will print a different value than the Constructor, but the same value as the first time you ran PIE during that editor session.
Expected: BeginPlay would print the same number as the constructor, and a different number each time you PIE.
Head over to the existing Questions & Answers thread and let us know what's up.
|Component||UE - Gameplay|
|Affects Versions||4.9.2, 4.10, 4.12|
|Created||Jan 13, 2016|
|Resolved||Jan 18, 2016|
|Updated||Jul 14, 2021|