I used to have no problems with Unreal Engine lifecycle methods.
That being said for the following code.
void ASpawnerSupervisor::BeginPlay()
{
Super::BeginPlay();
// Clear singleton
ASpawnerSupervisor::ASSp_SpawnerSupervisor = nullptr;
// Reset singleton
GetSS();
ASSSLM1p_SpawnerSpaceShipLegionMark1 = NewObject<ASpawnerSpaceShipLegionMark1>(this); // Is this safe from begin play issue
ASPUHp_SpawnerPowerUpHeal = NewObject<ASpawnerPowerUpHeal>(this);
ASPUCWp_SpawnerPowerUpChangeWeapon = NewObject<ASpawnerPowerUpChangeWeapon>(this);
// >>> This should not be required this is weird <<<
// I have double check with logs and both ASPUHp_SpawnerPowerUpHeal and ASPUCWp_SpawnerPowerUpChangeWeapon
// are initalized...
ASPUHp_SpawnerPowerUpHeal->ForceBeginPlay();
ASPUCWp_SpawnerPowerUpChangeWeapon->ForceBeginPlay();
ASPUHp_SpawnerPowerUpHeal->ScheduledSpawn();
ASPUCWp_SpawnerPowerUpChangeWeapon->ScheduledSpawn();
}
All logs shows that everything is initialized properly (just for you know, I have also reput all concerned actors in game to make sure they do use my latests constructors)"
That being said, as you can see I need to call ‘ForceBeginPlay’ without it I can see that the log from the following code is never reached.
// Called when the game starts or when spawned
void ASpawnerPowerUpHeal::BeginPlay()
{
Super::BeginPlay();
f_ScheduledSpawnInterval = GetMS()->f_PowerUpHealInterval;
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, FString::Printf(TEXT("ASpawnerPowerUpHeal::BeginPlay f_ScheduledSpawnInterval = %f "), f_ScheduledSpawnInterval));
//f_ScheduledSpawnInterval = 5.0f;
}
void ASpawnerPowerUpHeal::ForceBeginPlay()
{
BeginPlay();
}
It is the first time I need to force call my BeginPlay and I don’t want the situation to be my new norm.
Anyone here can help me figure out how come I now need ForceBeginPlay() in order for BeginPlay() to be called? I except BeginPlay() to be automatically called after instantiating it.
Because you shouldn’t be calling NewObject to create Actors. Instead you should get a UWorld (AActor::GetWorld should be fine in this case) and call SpawnActor.
Also, you should consider a using a WorldSubsystem for your supervisor instead. They are a better approach to singletons in Unreal than Actors you have to place or spawn into the leve.