Network game and replication
I am doing a network game and I need information about replication. (I have already read unreal documentation about this subject but it is very theoric)
How can I do if client1 do something, to make it replicated on client2 ? For example, if the client1 press a button in a UI that change is skin, he will be the only one that see his player with a new skin. So, how can I do to make the client2 see the client1 with is new skin ?
Sorry for my mistakes but I am not english, I do my best to be understandable. Thank you very much in advance for your answers ! Kind regards
asked Sep 16 '18 at 04:22 PM in Using UE4
When a client does something, he is the only one who knows about it (except for CHARACTERmovement).
A client can tell the server to do it but cannot tell another client.
So for client A to tell client B something, Client A has to tell Server to tell Client B.
Only the Server can tell other clients to do something.
The most common two ways to do this: Client A tells Server to set SomeVariable. SomeVariable is Replicated, so the server updates that variable on any Clients that are Relevant, as fast as the Server thinks it needs to tell them. OR, Client A tells Server to run an event (Server RPC). The server runs this function which calls another RPC (Multicast) which runs on the Server and all clients. this event happens on all machines Almost immediately. If you have several these happening every tick it will take too much network bandwidth and slow down the game making it laggy. But this is how you should do it for occasiional (not constant) things that all machines need to do at the same time immediately.
RepNotify happens when the server changes a variable and a client comes into relevant range (which could be much later than the first two options) Use this to guarantee that a client who joined the session late or entered a room after an effect happened still sees the effect turned on or off.
answered Sep 16 '18 at 08:18 PM
I had this same problem until i realized that PlayerControllers are not replicated. So each machine had its own Player 0, Player 1, etc. Which are not the same objects as te Player 0, Player1, etc. on the other machines.
Pawns replicate and Playerstates replicate, but Playercontrollers do not, even though they own the connections that communicate with the server.
So Player 0 on machine A is not the same object as Player 0 on machine B, even if one of them is the server.
So what to do? In your Playercontroller you can send a Server RPC containing the number of which skin you want, and it multicasts to all machines saying this guy chooses that skin but it gets it all wrong.
I dont know what is the right way but what I did that works is the following: - In Playercontroller I send Server RPC with a PlayerID parameter which is from the PlayerController --> Playerstate --> PlayerId variable. - Also in Playercontroller that Server RPC Loops over all the Playercontrollers (because server has all playercontrollers but clients only have playercontrollers of their machine) until it finds a match between the parameter playerid and one of the playercontroller--> playerstate-->playerids. When it finds the match, it sets the skin on that playercontroller's controlled pawn (you must pass in a parametef to tell it which skin also). - Do a multicast on the controlled Pawn passing which skin it should have. then on each majine the multicast received should run the set material function.
answered Sep 19 '18 at 06:24 PM
So what I did in my game is that when the player joins the server, then that will fire an event on their playerclass or their default pawn, like beginplay for example, because when you Open Level or Join Session it opens a level and destroys the pawn and controller they had (but keeps the underlying player connection object I think), then creates new ones once the level is loaded, and this fires their constructors and beginplay events.
So you go to where the player is just beginning to function, and have it send a Server RPC with its desired skin (which it told the GameInstance to remember because GameInstance is not destroyed on the client machine between levels like player controllers and pawns are) and its Player ID from the playerstate it has on the client machine which should be the same as the one on the Server's version of the same player's player state.
From there the server receives the Server RPC and just relays the Skin and PlayerID into a MultiCast. Then that MultiCast is received by the Server and all the Clients, and they all get the skin and Player ID to match to, and they look for the Pawn which has the Player State which has the matching ID, and when they find it, then they apply the skin that was passed to them in the Multicast. This will happen on all the machines.
So then the only problem is that if somebody else joins the session later, they missed all those multicasts from before, so really every player who joins will need to not only tell the server to multicast their chosen skin to everyone, but ALSO repeat everyone else's skin choices back to him so they get set on his computer too.
answered Sep 25 '18 at 07:21 PM
Ok, I have sent you an email, I hope I can trust you. Thank you !
answered Oct 15 '18 at 02:29 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here