Course contentsShow
← Unreal Engine Game Development
Lesson 740 of 88010. Networking and ReplicationFree lesson

Implementing Server-Side Actor Spawning Logic

Develop techniques to spawn actors exclusively on the server to ensure consistent state synchronization across all connected client instances.

In this lesson, you will learn how to centralize actor spawning on the server to ensure that every connected client maintains a synchronized and authoritative game state.

The Authority of the Server

In a networked Unreal Engine environment, the server is the single source of truth. If a client were allowed to spawn an actor locally (e.g., via GetWorld()->SpawnActor()), that actor would only exist in the memory of that specific client. It would not be replicated to other players, and the server would remain blissfully unaware of its existence, leading to desynchronization and security vulnerabilities.

Enforcing Server-Side Spawning

To ensure consistency, you must restrict the spawning logic to code paths that only execute on the server. We do this by checking the HasAuthority() function, which returns true only if the current execution context is the server (or a standalone game).

Consider this implementation for spawning a projectile:

void AMyWeapon::FireWeapon()
{
    // Ensure only the server handles the spawning logic
    if (HasAuthority())
    {
        FActorSpawnParameters SpawnParams;
        SpawnParams.Owner = this;
        SpawnParams.Instigator = GetInstigator();

        GetWorld()->SpawnActor<AProjectile>(ProjectileClass, SpawnLocation, SpawnRotation, SpawnParams);
    }
}

Why this works

When you call SpawnActor on the server, the Unreal Engine networking layer automatically handles the "spawn packet." It sends instructions to all connected clients telling them to spawn a proxy of that actor. Because the server initiated the spawn, it acts as the authoritative entity for that actor’s life cycle, including its replicated variables and movement.

If you have a client-side action that needs to trigger a spawn—such as a player pressing a button to drop an item—you should never call the spawn logic directly from the client. Instead, use a Server RPC (Server-Side Remote Procedure Call) to request that the server execute the spawn logic for you. Always remember to validate the parameters within that RPC to prevent clients from spawning unauthorized items or spawning them at illegal coordinates.

Key Takeaway: Always gate your spawning logic behind HasAuthority() or execute it exclusively within Server RPCs to ensure the server remains the sole architect of the game world.