Calling BeginPlay on actors in order within GameMode

I’m trying to gather up certain actors within my game world and call their respective BeginPlay() functions in order to ensure consistent construction order. This is my C++ code in GameMode:


 #include "GameFramework/GameMode.h"
 #include "DBZGameMode.generated.h"
 class ADBZCameraFocusActor;
 class ASDBZLevelScriptActor;
 class DBZGAME_API ADBZGameMode : public AGameMode
     virtual void BeginPlay() override;
     ADBZCameraFocusActor* cameraActor=nullptr;
     ASDBZLevelScriptActor* level=nullptr;


 #include "DBZGame.h"
 #include "Cameras/DBZCameraFocusActor.h"
 #include "LevelScript/SDBZLevelScriptActor.h"
 #include "DBZGameMode.h"
 void ADBZGameMode::BeginPlay()
     for (TActorIterator<ADBZCameraFocusActor> actorItr(GetWorld()); actorItr; ++actorItr)
         cameraActor = *actorItr;
     for (TActorIterator<ASDBZLevelScriptActor> actorItr(GetWorld()); actorItr; ++actorItr)
         level = *actorItr;

as of right now the editor keeps crashing. There is only one instance of each in my game world (so they are blueprinted classes). My questions are is the editor crashing because I’m going about this in the wrong way (is this conflicting with another default construction behavior within game mode)? Or does it have something to do with my pointers? I feel my pointers should be fine so not sure if trying to call the BeginPlay() functions this way is a good thing? Thanks for any help.

AFAIK you should not rely on the order BeginPlay is called, neither on the order Actors are listed in TActorIterator. You also should not manually call BeginPlay either. You should use a custom event/function instead for such things.

Oh okay. I’m still pretty new to UE4 and figuring out how to think in UE4 is quite daunting. Do you have any quick ideas or know any articles on how I would go about implementing an event/function for construction order?

Create a spawner class which spawns said actors in whatever order you want, this is the best way from the information given

Okay great thanks so much!

There’s a failsafe which makes sure that BeginPlay() is only called once. So calling it manually is not recommended.

Since this is a thread that pops up on google while searching for a related issue, I’ll leave here a link for a solution to this problem :]