I need to determine which connection (which client and/or server) spawned a certain actor. The problem is that this actor is automatically possessed by an AI controller when it spawns, which overrides the “owner” spawn parameter, and prevents me from using GetOwner() to determine the owning connection.
Is there a way to get the owning connection some other way? Also, once I get the owning player, how can I use that information to say “do this if you’re the owning player, do this if you’re not”?
The simplest solution we have found is to add a pointer to the owning player and assigning the owning player when the server spawns any actor.
UClass()
class SomeActorClass : public AActor
{
…
UPROPERTY(Replicated)
TWeakObjectPtr OwningPlayer = nullptr;
…
}
When the server spawns an actor, use deferred spawning so that you can set the owning parameters before the actor is finished spawning. Below is the actual spawning code I use for multiplayer games.