Startup event for actors spawned by a mutator
Posted: Mon Sep 01, 2014 10:16 am
I had this strange issue which seems like the gun of an AutoTurret which i replaced didn't vanished but sticked around.. however further investigation revealed a far larger general problem.
When you use a Mutator to replace (preplaced) actors on a map, or otherwise spawn actors during *BeginPlay() the *BeginPlay() methods will be called multiple times. A quick setup is attached in TF.zip to observe this. However the log yields:
I guess that when you spawn the actor during PostBeginPlay() just PostBeginPlay() and PostPostBeginPlay() events are run two times.
Anyway, this can lead to very very subtile bugs. e.g. the AutoTurret spawns it's gun twice, pawns will add themself two times to the PawnList, etc.
I guess i will have to start checking over all my actors startup event flow to make sure that everythings fine, probably this will fix some very strange bugs i still encounter.
When you use a Mutator to replace (preplaced) actors on a map, or otherwise spawn actors during *BeginPlay() the *BeginPlay() methods will be called multiple times. A quick setup is attached in TF.zip to observe this. However the log yields:
Why this happens is simple: When you call spawn() it will call all the *BeginPlay() events. And add 01_NYC_UNATCOIsland.TFCrateExplosiveSmall0 to the actor list over which is just iterated for the PreBeginPlay() calls (which will call mutators CheckReplacement() function if prequisites are met). So PreBeginPlay() will even be called for this new actor. Then the other events are run. So the code will run twice.ScriptLog: 01_NYC_UNATCOIsland.TFCrateExplosiveSmall0.PreBeginPlay()
ScriptLog: 01_NYC_UNATCOIsland.TFCrateExplosiveSmall0.BeginPlay()
ScriptLog: 01_NYC_UNATCOIsland.TFCrateExplosiveSmall0.PostBeginPlay()
ScriptLog: 01_NYC_UNATCOIsland.TFCrateExplosiveSmall0.PostPostBeginPlay()
[..]
ScriptLog: 01_NYC_UNATCOIsland.TFCrateExplosiveSmall0.PreBeginPlay()
ScriptLog: 01_NYC_UNATCOIsland.TFCrateExplosiveSmall0.BeginPlay()
ScriptLog: 01_NYC_UNATCOIsland.TFCrateExplosiveSmall0.PostBeginPlay()
ScriptLog: 01_NYC_UNATCOIsland.TFCrateExplosiveSmall0.PostPostBeginPlay()
I guess that when you spawn the actor during PostBeginPlay() just PostBeginPlay() and PostPostBeginPlay() events are run two times.
Anyway, this can lead to very very subtile bugs. e.g. the AutoTurret spawns it's gun twice, pawns will add themself two times to the PawnList, etc.
I guess i will have to start checking over all my actors startup event flow to make sure that everythings fine, probably this will fix some very strange bugs i still encounter.