Identifying player, local multiplayer

This is really an xpost between blueprint and c++.

Our game has local split screen multiplayer (2,3 or 4). We have up to 4 characters (all the same character right now) in our game that upon colliding with collisionbox calls a function in our a custom game mode class, which inherits game mode.

void EndGameEvent(APlayerController *endPlayer);

What we would like to be able to do is to identify which controller number or viewport or something 1-4 or 0-3 so that we can say player 1 wins. We are looking for a way to access the controller ID but haven’t been able to find one.

Our original attempt to circumvent not being able to find the controller ID was to save a reference of all of the PlayerControllers in an array then pass in the PlayerController associated with the triggering of the event then compare.

int i = 0;
	bool isFound = false;
	
	for (; i < 3; i++)
	{
		if (pcArr[i] == endPlayer){
			isFound = true;
			break;
			
		}
	}

What we found doing this was that the playerController passed by the function either always finds a match (the same match regardless of the controller that caused function to be called) or it doesn’t find one. IE. It always says player 1 wins or it doesn’t find the player controller.

Ignoring the other strings on this blueprint (other failed attempts at fixing the problem), this is essentially how we are attempting to get the playerController that caused the event.

Does anyone know of a better way to identify which player caused the event? It really seems like controllerID is something that should be accessible to me but I’ve yet to find a way.

If you need any other information that may help you answer the question, please ask in a comment below.

Thanks,

Greg

#Player State

In the player state class there is a unique ID value that will help you

/** Unique id number. */
UPROPERTY(replicated)
int32 PlayerId;

#Controller ID

The controller ID is not part of UPlayer, it is in ULocalPlayer

so you must obtain a ULocalPlayer in order to access this value

It can be accessed from the Player Controller Class!

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//		   Get Local Player Controller ID
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int32 AVictoryGameSession::GetLocalPlayerControllerId(APlayerController* PC)
{
	check(PC);
	
	ULocalPlayer * LP = Cast<ULocalPlayer>(PC->Player);
	if(!LP) return -1;
	
	return LP->ControllerId;
}

If you cannot find anyway to do this in BP (if you need to do it there), let me know and I can make a node for it

:slight_smile:

Rama

1 Like

Hi Rama, thank you for your help. For whatever reason my home computer doesn’t like building the project in visual studio so I won’t be able to attempt anything in code until I go back up to the Uni tomorrow. I did however attempt some stuff in the blueprints (which my partner is far better at than me).

I’m not sure where to get the Player Index for the Get Player Controller node so I’ve just connected it to the Other Body Index part of OnComponentBeginOverlap. I then did a Get Player State from the return value of Get Player Controller. The Player State doesn’t have access to the PlayerId in the editor as far as I can tell (version 4.1). If this is the correct approach (in terms of where the index came from) I can build the node tomorrow for returning the PlayerId.

Also, I tried casts to PlayerState and casts to PlayerController based on the Other Actor that OnComponenyBeginOverlap provides but both casts fail printing “Hello” and “hello again”

Again, thank you. Your tutorials have been extremely helpful in breaking into the engine.

“Again, thank you. Your tutorials have been extremely helpful in breaking into the engine.”

Yay!

:slight_smile:

Yea I think I will need to make a BP node for this…

#Two BP Nodes For You

I have added two BP nodes to my Victory BP Nodes Plugin!

Get Controller ID ~ takes in player controller and gives the ULocalPlayer’s ControllerID

Get Player State PlayerID ~ returns the replicated unique PlayerID for the supplied Player Controller!

bool PlayerController_GetControllerID(APlayerController* ThePC, int32& ControllerID);
	
bool PlayerState_GetPlayerID(APlayerController* ThePC, int32& PlayerID);

#Download

You can download from original post of this thread!

Thank you Rama, this is perfect! (I had no idea about your plugin either, it looks great)

I don’t know how to mark this as answered so if one of the mods want to do that it would be much appreciated.

you’re welcome!

To mark this answered you click the check box next to my original answer :slight_smile:

:slight_smile:

Thanks Rama, your plugins have helped me tackle a very difficult problem i was having with my project. Literally tearing my hair out trying to find a fix and your plugins did the trick. Thanks again for your help.

1 Like


I hope you can help me or give me some ideas, thank you very much!