Gameplay Question

Let’s say I have a 40 x 40 floor and bunch of stationary actors on it that I would like UE to load when a 3 player game starts.

I want to create this floor and the stationary actors in C++, and make it available to all the 3 players.

In what class should I be doing this?

  • GameMode? According to documentation, this class has game rules. So doesn’t sound like the right place.
  • GameState? This one keeps track of stats in an ongoing game. Not good for this purpose
  • Player Controller? Nope. That wouldn’t make sense, as I want this static world to be generated for all players. I don’t want one PC instance to create it and share it with others.

OR

  • Do I just create a custom class that creates these and instantiate it from GameMode like as such:

    PlayerControllerClass = AMyPC::StaticClass();
    GameStateClass = AMyGS::StaticClass();
    SomeCustomClass = AMyCustomClass:StaticClass();

Hello, Firat

Please note that the convenient way to implement the required functionality would be to create floor and place your Actors inside the Level Editor.

Spawning Actors from C++ usually makes sense when they are not stationary, for example when a projectile is fired.
Nevertheless, you can spawn your Actors from GameMode, for example in BeginPlay() function.

However, spawning is only appropriate when a World exists, so you should perform a check before spawning:

 UWorld* const World = GetWorld();

 if (World)
 {
     World->SpawnActor<ACustomActor>(ACustomActor::StaticClass());
 }

Thus, the best solution in this situation would be to create the level and place all stationary Actors in the Editor.
Then you can load the map when appropriate conditions are met.

Hope this helped!

Have a great day!

Thank you. That certainly helped.

(I tried to mark this as “answered” but was not able to figure out how to do it)