Hi Everyone,
Problem - Objects created in actor constructor and stored in TMap are getting variables collected (set to random) after start of the game.
Details
In ATestActor I create 500 UTestObjects. Each of them has variable id=0 defined in UTestObject header. In “Begin Play” I go through whole map printing out ids of each UTestObject and getting values like -842150451 etc.
If I do the printing in constructor, it works and outputs 0 for each object.
Garbage collector should not collect those since I have pointers to them stored.
I am sure I am missing something trivial,
Thanks for any help,
Crispy
.
Code
ATestActor class
ATestActor::ATestActor()
{
for (int i = 0; i < 500; i++)
{
FName RendererName = FName(*("ObjectName" + FString::FromInt(i)));
map.Add(i, NewObject<UTestObject>(this, RendererName));
}
}
void ATestActor::BeginPlay()
{
Super::BeginPlay();
for (int i = 0; i < 500; i++)
{
int id = (*(map.Find(i)))->id;
UE_LOG(LogTemp, Warning, TEXT("id %d"), id);
}
}`
//Simple TestObject header
UCLASS()
class BUILDTEST_API UTestObject : public UObject
{
GENERATED_BODY()
public:
int id = 0;
};