x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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?

Product Version: UE 4.11
Tags:
more ▼

asked May 06 '16 at 09:37 AM in C++ Programming

avatar image

BugSwat
200 9 13 19

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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()

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.

more ▼

answered May 10 '16 at 12:26 PM

avatar image

Zhi Kang Shao
496 9 10 30

avatar image BugSwat May 13 '16 at 08:57 AM

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

avatar image Zhi Kang Shao May 13 '16 at 03:15 PM

Glad to help. :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question