Download

Proper way to get reference to local PlayerController in multiplayer

Hey,

I’m trying to get a proper reference to my local PlayerController in multiplayer. There must be an error in my code below:

MyCharacter.cpp:


void AMyCharacter::PlaceObject(){
if (Controller != NULL && Controller->IsLocalPlayerController() && Role < ROLE_Authority){
	AMyPlayerController* MyControllerPointer = Cast<AMyPlayerController>(Controller);
}}

The client that executes PlaceObject() first will be just fine. However all other client screens will turn black. Please tell me what am I doing wrong? :frowning:

if im correct you have to iterate through the controllers otherwise they will always call to the player controller in slot 0 (hence why the first to do it goes fine), im not too sure honestly im bad at programming, please correct me if im wrong D:

Since that method is in your Character you can cast the controller to your player controller.
Here is a snip of code as a example.



	// Inside your AYourCharacter:: class.
	AMyPlayerController* PC = Cast<AMyPlayerController>(Controller);
	if (PC)
	{
		/* PC is valid and players controller. */

		if (PC->IsLocalController()) // Is local controller?
			PC->ClientMessage("IsLocalController()", NAME_None, 3.f);

		if (PC->IsLocalPlayerController()) // Is the local player controller?
			PC->ClientMessage("IsLocalPlayerController()", NAME_None, 3.f);


		// To get Pawn/ Character from your controller.
		AMyPlayerClass* Player = Cast<AMyPlayerClass>(PC->GetControlledPawn());
		if (Player)
		{
			// And we are back with the character class from your Player Controller.
		}
	}

Hope it helps, and happy new year.
WCode