I am trying to develop a multiplayer game using LAN. In my setup I have three cube objects in a map. When I travel to this map from lobby, using server travel, what I am doing is I find all cubes in the map using Get All Actors of Class method. And when I try to input a key, say left-right, they move accordingly.
Now what I want to do is when a player moves his/her cubes, those cubes will be displayed on others’ screen as ghosts, not at all interacting with anything. To do so I wrote two events just after the input event. The first one runs on server - which gets the cubes modified position, the second one runs as Multicast and updates all clients with new positions.
From server-client this works, from client-server, it doesn’t. (I have checked ‘Replicates’ and Replicate Movement’ on.)
Using same concept, if I write a simple print screen, it works both ways.
My question is: Do I have to possess the cubes (as pawns) in an individual player controller to make this happen? If it’s not needed, what am I missing here? Kindly help.
Replication works server to client to send client to server use a custom node with replicates to server and reliable then add any pins for variables. I would also look at spawning the controlled pawn in the player controller and possessing this needs to be done on the server side so again I would trigger this with a custom event set to server and reliable.
Think of the client as just a screen and controller you can do calculations client side but should use any results server side.
The client server model is what underlys this logic
Hope this helps
Thing with the cubes is, I do not spawn them at all like any normal pawn/character. They are just present in the map when game starts. Then I find all of them using ‘Get All Actors of Class’ which gives me three cubes in an array and this array I pass to the left/right keyboard input event. This is a non-replicated event and moves all the cubes accordingly on server/clients screen.
Now from the clients, when its cubes are moved, I want that movement to be replicated in all clients and server. For that I wrote an event which runs on server, it has another event which is a multicast, which ‘broadcasts’ the movements to all clients.
This happens from server->client, but not from client->server.
Because the server is replicating movement when the client passes the info the server overrides the multicast. Solution don’t do multicast but do it on the server. Try that
Does each player get to move 3 cubes? I would suggest spawning 3 cubes for each player when they log in. Here’s what I would do:
Get the 3 cubes in the map and record their locations + rotations, then destroy the cubes.
Spawn 3 cubes at the saved locations. When spawning the cubes, pass a reference to the player pawn that owns it.
You can also make the player the “Net Owner” of the cubes, which will let you call Server RPCs from the player.
Check if the owning pawn reference is the “locally controlled” pawn. If False: turn the cube into a “ghost” with materials etc.
You can now move the cubes by using a server RPC to move the cubes on the server. If the cubes have “Replicate Movement” enabled they should move on all clients.
Final Note: If you disable “Replicates” and “Net Load On Client” for the cubes in the Level, they won’t get loaded by players (which is fine since the server is the only one that needs to know where they are)
Sorry for the long answer, hope this helps!
Hmm I thought so. That would mean I cannot directly use cubes which are already lying around in my map. I will have to spawn and possess them in each PC. Got it. Thanks GrumbleBunny.
@GrumbleBunny: sorry but few things seem confusing. When you say PlayerPawn:BeginPlay, this is Player Controller, right? What I am doing is: I spawn the cubes from my game mode, pass one of the player controller as reference which can be used to possess after spawn is complete.
Since each player controls multiple cubes, I would make a “manager” pawn separate from the cubes.
@GrumbleBunny: Finally managed to get it right! Thanks!