Server Replication Question (Spawn a Class)

/**
* Owner of this Actor, used primarily for replication (bNetUseOwnerRelevancy & bOnlyRelevantToOwner) and visibility (PrimitiveComponent bOwnerNoSee and bOnlyOwnerSee)
* @see SetOwner(), GetOwner()
/
UPROPERTY(replicated)
AActor
Owner;

The Actor class has an Owner that you can set which will be replicated. The owner can be anything, like an Actor or Pawn that represents your player. However, I don’t know if I would recommend the owner being the PlayerController (since the PlayerController is only replicated between the server and the owning player) or even the Pawn. Instead, you should have some replicated Actor represent your player and their info. A PlayerState may actually work well for this. Everyone has a replicated copy of every player’s PlayerState, and since the Owner is replicated they will be able to tell which PlayerState specifically owns the building.

GetPlayerController would only provide all the PlayerControllers to the server. For the client, as I mentioned above, they only have a copy of their own PlayerController, which is typically at the 0th index. However, I would not really rely on this method. The PlayerState’s Owner is conveniently the PlayerController.

You can get the PlayerState from your PlayerController also. You shouldn’t really need to “find” your PlayerController as it’s logic should be to just manipulate input.

As for letting the Server know who sent the request, well the server knows “who” sent it based on who owns the object the request is being made on. The Owner of a PlayerController is a UPlayer by the way. I’m not sure how to use that class though. But anyway, if the PlayerController makes a Server RPC (message), then that RPC is called on the replicated version of the PlayerController on the Server, and so the Server knows inherently which player or actor that is (and can also access that PlayerController’s PlayerState).

So basically, for what you want, the PlayerController should send a server message, which is to its replicated version on the server. The Server can then check the PlayerController’s PlayerState for things like money to build (which, by the way, you should probably use a DOREPLIFETIME_COND and COND_OwnerOnly for money in an RTS), and also use that PlayerState as the Owner of the newly built building. The building, an AActor, just has to have SetOwner called on it after creation and presumably in this case we SetOwner to PlayerState.

By the way, I didn’t really know most of this till you asked your question, so thank you for asking. You can find out a lot by searching the source code.

I hope this helps!