Client's uses the server's player controller (UE5)

I am trying to create a multiplayer game where one of the players is the host and I’m having a problem setting the player controllers.

When joining the server, a new player controller is created for the client, but the client thinks that the server’s controller is the local controller.

I printed the reference to 'self ’ on the player controller ,after making sure it is the local controller, and it returns the server’s controller on both the client and the server.

I was stuck on this issue for a while now so any help would be appreciated

Clients do not get a reference to any other controller. Only the server (host) has copies of all controllers.

Is Server is a better flag to check against.

That’s my point. The host’s controller is PC_0 and when the client joins the server a new controller, PC_1, is created.
But for some reason the client thinks that PC_0 is it’s local player controller, and PC_1 is not used as a controller for either the client or the server.

because it is, as @Rev0verDrive said they have access to no other controller so from the clients point of view only PC_0 exists.

in short don’t use GetPlayerController for anything multiplayer related. Either pass through a Ref or better yet use PlayerState

If you keep in mind that the client only has his controller, and it always has index 0 - then this can be used in many places and it will be the most convenient(?). :roll_eyes:
This isn’t the first time I’ve seen GetPlayerController being avoided, but I’m not entirely sure why…

because its possible (although unlikely) a listen server wont be index 0 which could cause a really hard to track down bug.

or if you do a ServerRPC and just by habit use index 0 you could get the wrong player

Ok, I see that the server sees the client’s controller as PC_1 and that the client sees his own controller as PC_0.
But I can’t move the player on the client, and when I’m trying to attack on the client it attacks with the host’s character. How is it possible for the client to control the host’s character?

we’d have to see your code but it just means that it has the wrong reference somewhere, kinda like i said above you could be using GetPlayerCharacter(0)

1 Like

I followed Kekdot’s multiplayer tutorial for this: https://www.youtube.com/watch?v=YUPZ1j_9Vzw&list=PLNb7FZ2Nw2HTBgWggHGaMtHAOygKcXIyW&index=4

When the player controller is created on the new session it calls the gamemode (from a server event) to spawn a player.
Then the gamemode spawns the character and posses it with the given player controller:

Because it’s always a generic reference to the first player on the server side.

Lets say I have a global function (client and server use) that uses the camera manager camera location.

Get Player Controller (0) -> Camera Manager -> Get Camera Location

On clients it works perfectly, but on the server it always returns player 0’s camera location.

Camera Components on Server and Sims do not update their relative positions. This is why you can’t simply drag the camera component into your graph for server logic. Variables get updated, but the component itself doesn’t (e.g. Set relative location and rotation).


Using index nodes forces you to write duplicate code for the server, but modifying it to reference controller, character etc the correct way.

None of these should be used in multiplayer games.

:+1:
On Listen Servers during server travel a “Joined Client” can in fact become PC_0. While the host ends up PC_1.

show us where the function is called and what controller is it given?

It’s on the image I posted earlier. The image shows a function on the game mode that is called from the player controller. This function spawns a character and and posses it with the controller that called it.
I am giving the controller by using the self reference on the controller and not by using get player controller(0)

i mean show us that code, so its on the PC and calls the GM? still show us and we can look for errors

Sorry for the delay I was away from home.
Here is a screenshot of the blueprint on the PC that calls the player spawn.
Any insight would be appreciated.

your spawn looks fine so

maybe the issues is in your attack/move commands

The attack and move commands are handled in the player character blueprint
I didn’t change much in the move move commands from the third person character blueprint and the attack commands are just a button press input in the blueprint.

It should play an animation and create an object that is replicated for the server (a fireball as an example).
I tested it again and the client’s input didn’t affect the host’s character. Maybe it was a missinput on my part.

so whats the issue then? movement isnt working?

start by debugging the key press?
you could also debug the event possess just in case something is overriding it

Yes, I can’t move the character on my client.
I tried to debug the movement input and the node EnhancedInputAction isn’t even called from the client. I compared it with a simple ‘W’ key and it seems to be working fine.

Also I checked in the gamemode and each character is possessed by the correct player controller.

About the attack I think that it is a different issue. I’ll update if I found anything.

yeah the ability (attack) is likely a bad reference

with the Input Action check where you add the InputMappingContext to the Input Subsystem