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.
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.
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(?).
This isn’t the first time I’ve seen GetPlayerController being avoided, but I’m not entirely sure why…
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)
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.
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)
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.
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.
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.