Hello all!
I feel like this is a pretty simple one, but I can’t wrap my head around it.
In a multiplayer game with a dedicated server and multiple players in the level, what will GetPlayerController(0) return?
What if it’s not a dedicated server game?
Is the answer different when the code runs on the server or on each of the clients?
My first guess is that the first player spawned into the level has the index 0. My second guess is that the index 0 is always with the “local” player for clients; not sure what it would be for the server. I’m edging towards guess 1 at the moment.
Player 0 is the first player created on the machine that is running the code.
So if you have 2 players on the Server and 2 players on a client and 2 players on another client for a total of 6 players in the game over 3 computers,
then the server’s 2 players will be at index 0 and 1,
and client A’s 2 players will be at index 0 and 1 on their machine. they will also be on the server’s machine but who knows what index they’ll be at there, and they’ll have IDs of -1 because they dont belong to the server machine.
client B, exact same situation.
So server will see 6 player controllers but only its own will have IDs >= 0 the others have IDs == -1
Each client will only see its own playercontrollers.
6 Likes
Thank you for the quick, straightforward answer! This is exactly what I was looking for.
1 Like
If the Player State exists at index 0 on the client and index X on the Server, what happens when I use a BP_Interface to send a message to the Player State?
In this situation, the Player State has the same BP_Interface implemented in its classes, and I plug fGetPlayerState into the target using index 0.
Do I send a BP_Interface message to both my Player State and the Player State of a client at index 0 on the server? (causing either a double activation on my side or a seemingly random activation for another player)
Does the Server take up a player 0 slot? (it’s not a player, but systems be arbitrary)
Do I only send the message to my own Player State because I’m player index 0 to myself?
Is there a better way to implement this?
Sorry I’m just now coming back onto the forum scene.
I hope you found the answer to your question.
I’ts been a long time since I played with Unreal Engine much, but the answer to your questions seems to be “it depends on what you’re trying to do” over the network.
If I remember right, Player States DO replicate their variables (though you might have to tell them to for each variable).
So if you only need the data to replicate then just set the variable on Player State class to replicate, and set the variable normally.
If you need the execution of functions, actions, animations, etc. to replicate, then you’ll still have to do remote procedure calls. Keep in mind whether only that player needs to notice, or if all players need to notice.
C++ delegates set to replicate over network come in real handy hear (or so I’m told), for getting event-driven responses over network, but you can get kind of the same effect using those blue blueprint nodes that send from server to client, or the red multicast nodes. Sorry, been a long time
I don’t remember what everything is called.
Get Player XXXXX (Index) is typically for Single Player or split screen Coop.
The index is the array index corresponding to their join order… added to array order.
On clients, Index 0 points to themselves.
On Server (Dedicated/Listen), Index 0 points to the first client to join.
Server code (RPC) using Get Player Controller 0
will always point to the first player that joined.
The above nodes can never be used in multiplayer.
From Pawn you use Get Controller
then either cast to your controller class, or preferably never cast and use a BP interface function to get what you need.
From controller you use Get Controlled Pawn
then either cast to your character class, or preferably never cast and use a BP interface function to get what you need.
Character component needs character data…
Get Owner
→ BP Interface
Hit/Overlap needs character or controller data
Hit/Other Actor → BP Interface (pawn) → Pawn: Get controller → BP Interface
BP Interfaces are the better approach vs casting.
1 Like
Thank you - these are all things I wish someone had told me 9 years ago. I have come to follow these ways by painful trial and error, including the Interface calls (one of my favorite ways now besides event delegates)
I still hadn’t realized Get Player State was a bad idea. Somehow I got away with that one or resorted to looping through them to find a match to a unique identified I set up when the player joins. Not quite the right way to go about it but I was just trying to get the thing to work and there was no knowledge to be had anywhere I knew to look, to tell me otherwise how to get it working.
Class to Class Event Delegates/Dispatcher require a Reference to the bound class, so I avoid them. Only time I ever use them is when I need a One to Many event, like a switch turning on/off many lights.
I’m currently working on a Multiplayer 101 project where I lean into interface for all class to class interaction. I haven’t run into a single scenario where I’ve needed to cast or use an event dispatcher.
Player State is fully replicated and natively referenced in the player controller. You can get a generic reference to it via Get Player State
under the Controller section in context menu.
From there it’s just about having functions setup in the Player State BPI.