How to know if listenserver/client, owner for rpc?

How do I find out if an actors code is running on the listenserver or on an client?
And how do I know if an actor is able to rpc to the server?

Just to stop the always coming wrong answers (in other questions), I will tell you some things:

1.)

The instance that spawned the class is
always ROLE_Authority. If an Actor is
spawned on the client, the client is
the authority. – OmaManfred

2.)
bool AActor::HasAuthority() const
{
return (Role == ROLE_Authority);
}


Now back to my two (three) questions:

  1. How do I know I am currently the listen server?
  2. How do I know I am currently a client?
  3. How do I know if I am able to rpc to a listen server?

To answer questions 1 and 2: you can use AActor::GetNetMode() which returns whether you are a listen server, dedicated server or network client.

if (GetNetMode() == ENetMode::NM_ListenServer)
{
}

The ENetMode values are:

  • NM_Standalone = 0
  • NM_DedicatedServer = 1
  • NM_ListenServer = 2
  • NM_Client = 3 // is only when you are actually connected to a server

The ordering of the ENetMode values allows you to check multiple things at once: GetNetMode() <= 2 means you are the server or standalone, so you have ‘global authority’.

To answer question 3, if you’re the server you can always execute server RPCs. If you’re a network client, you can only use server RPCs on actors owned by the local player’s player controller. So you can grab a reference to that player controller and check whether that’s the actor’s owner.

    if (GetOwner() == GetWorld()->GetFirstPlayerController())
	{
	}

The default game mode code automatically makes the player’s default Pawn owned by the player’s player controller, allowing you to call RPCs on the pawn. To make more actors on which a network client can call RPC’s, the server has to set those actors’ owners to a player controller.

Thank you very much.
This is a so well formulated answer, I am very thankful.
And it is so easy. That is great too :slight_smile:

Glad to help. :slight_smile:

Here is one more possibility way to check by calling UGameInstace::IsDedicatedServerInstance(). This is not detailed like the answer above, but I think you should also know about it. That is works if the game world is not ready yet.