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 can I get my own PlayerController on my Listen server?

Getting your own PC is easy on both standalone and client(they are actually the only PlayerControllers) but I cannot figure out how to get the PlayerController that belongs to the host in a Listen game

Product Version: Not Selected
more ▼

asked Dec 18 '14 at 03:56 AM in C++ Programming

avatar image

1 1 1 2

(comments are locked)
10|2000 characters needed characters left

3 answers: sort voted first

I know it's an old question, but I had encoutered the same kind of issue. So I'll to explain 2-3 things.

You have to separate two things : Controller ID and Controller Index.

The index is the position in the world player controller's array. It may change for the same controller especially in a Listen Server after a seamless travel with a controller swap (ex : if changing the class).

The ID (indentifier) of the controller is store in the LocalPlayer and is unique for each local controller, where 0 is the "main" controller that can be used for referencing HUD & Widgets.

So " the controller on index 0 is always "your player controller" " is not true. If you need the main controller, you have to add a little C++ function like so :

 APlayerController* UMyClass::GetMainController(const UObject* WorldContextObject)
     // Get world context (containing player controllers)
     if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull))
         // Loop on player controllers
         for (FConstPlayerControllerIterator Iterator = World->GetPlayerControllerIterator(); Iterator; ++Iterator)
             // Get player controller from iterator
             APlayerController* PlayerController = Iterator->Get();
             // Get local player if exist
             ULocalPlayer* LocalPlayer = PlayerController->GetLocalPlayer();
             // If it's local and id is 0, it's the main controller
             if (LocalPlayer != nullptr && LocalPlayer->GetControllerId() == 0)
                 return PlayerController;
     // Not found
     return nullptr;

Hope it'll help some other devs !

more ▼

answered Jun 25 '18 at 03:36 PM

avatar image

21 1 4

(comments are locked)
10|2000 characters needed characters left

Only the server has all the player controllers, so you can only get the APlayerController of the host on the actual host.

AFAIK the controller on index 0 is always "your player controller", so if you're calling the following in the server, it returns the host player controller:

 UGameplayStatics::GetPlayerController( GetWorld(), 0 ); 

If you do it on a client, it returns their player controller, and remember that in the clients the other player controllers do not exist

Also, in the change that server is dedicated, call IsLocalPlayerController() on the return from that function above, if it returns false it is a dedicated server (hence the host has no player controller).


Nuno Afonso

more ▼

answered Apr 19 '15 at 09:20 PM

avatar image

132 7 12 17

(comments are locked)
10|2000 characters needed characters left

Solution C++ Code For You

NAfonso's answer is very good.

I also just answered this over here!



more ▼

answered Apr 21 '15 at 08:49 AM

avatar image

10.5k 444 343 1081

(comments are locked)
10|2000 characters needed characters left
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