Spawning actor in BeginPlay in Multiplayer with c++

I’m working on a multiplayer game and I’m trying to spawn an actor in another actor’s BeginPlay() function using c++,
but the BeginPlay() were called more than once (depends on how many clients you have), so then I’ve got multiple actors spawned in the world.
But if I use blueprint BeginPlay Event instead, then everything works just fine.
So what did I miss? Is it OK to spawn an actor in another actor’s BeginPlay() function in multiplayer using c++?

sorry for my bad English :slight_smile:

BeginPlay is called on each actor when that actor enters the game world.
So if more than one instance of that actor exists already or gets spawned, then its class’ beginplay will be called that many times too, once for each time an actor of its class enters the world.

Which class’ beginplay are you using?

It depends on the network role of the class you are using to spawn the other actors. NetRole works (very loosely) as follows.

ROLE_authority = Server
ROLE_AutonomousProxy = Client owned actor where the player has control, but server has ultimate authority
ROLE_SimulatedProxy = Your client has no control over this actor. It is simulating what the server says
ROLE_None = this actor no longer exists on the server. Like a rag doll which can be completely different between clients with no replication. It has been, in UE4 terms, “Torn off”

So, in your case, you should wrap your code with the following check to ensure that only the server spawns actors. Just keep in mind that actors who are maintained through seamless travel (e.g. PlayerControllers do not get a second BeginPlay invocation after moving to a new level).

void AMyActor::BeginPlay()
{
     if(HasAuthority())
     {
           // spawn you actors
      }
}

Thanks for your answer! I’m gonna try it out.