Developer Notes

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.

Description

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

Steps to Reproduce

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
7. PIE

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.

Have Comments or More Details?

Head over to the existing Questions & Answers thread and let us know what's up.

0
Login to Vote

Non-Issue
ComponentUE - Gameplay
Affects Versions4.9.24.104.12
CreatedJan 13, 2016
ResolvedJan 18, 2016
UpdatedJul 14, 2021
View Jira Issue