I have been trying to create a building system. In my system, a preview of object that will be placed is displayed and position of this object is updated with a function called “UpdatePreviewTransform” which is tied to timer.
Everything works fine until garbage collection. But after GC my function is not being called anymore. My game does not crash or give any errors. It is just that timer stops calling the function.
My code in BuildingManager class is as follows.
// Spawn object CurrentBuilding = World->SpawnActor<ABuilding>(TempClass, BuildingLocation, FRotator::ZeroRotator); // Set timer to call UpdatePreviewTransform every 0.15 seconds. World->GetTimerManager().SetTimer(TimerHandle, this, &UBuildingManager::UpdatePreviewTransform, 0.15f, true);
// Just to see if method gets called or not UE_LOG(LogTemp, Warning, TEXT("Function called.")); ... // Updating position
But nothing is printed to console after GC. I forced GC with “GEngine->ForceGarbageCollection()” to see if GC is really the cause and confirmed that after first GC the problem occurs.
I did not placed this class into my world. So if I call GetWorld() it returns null. To solve that I pass pointer returned from GetWorld() to this object when creating BuildingManager class in my Character class.
What am I doing wrong?