Ok, first of all we need to make sure that we are talking about the same setup:
You need the following setup (based on my knowledge) to get this to work:
1- Tank needs to be a Pawn and have all the logic, which is needed to control it, in itself
2- Turret needs to be a Pawn, too, and also have all the logic, which is needed to control it, in itself
(With this, you make sure that you split the controls and don’t have to manage everything in one class.)
3- You don’t want a DefaultPawn in your GameMode! Because that one is spawned and possessed, when a Player connects. You need to do this by hand:
4- Use the “OnPostLogin” function of the GameMode to Spawn the Tank for the first Player (simple Int Variable to count or a FlipFlop) and use the passed PlayerController to possess it. GameMode is ServerSide only, so this is replicated and should just work. Same goes for the Turret, but you want to make sure that you save the Tank Pawn in a variable (Inside of the GameMode), so you can attach it to the Tank.
(You could also spawn a combined “pack” of Tank and Turret directly. You can add “ChildActors” as components, which you could do in the Tank and then assign the Turret Class to it.)
5- Now we can start talking about Replicating the Movement
So if this is your setup, (can differ a bit), and each Pawn has its own Input, we can start replicating.
Make sure both Pawns are actually set to Replicate and ReplicateMovement in the Default Class Settings.
Now you need to remember, that “EventTick” is called on Server and Client. If you don’t use a DedicatedServer Setup, the first Player will be the ListenHost, which means he’s the Server AND a Client.
(Which means, if you, by any chance, let the first Player control the Turret, it will be replicated.)
So, we don’t want to use the EventTick like that. Now i need to know:
Is this a 2D TopDown Game?
Because you are getting the 2D Mouse Position on Screen.
Not focusing on the actual input you need, you need to do it somehow like this:
In your InputSettings of the Project, create Inputs for the MouseMovement. The Template Project
mostly have the setup. You need “AxisMapping” for the Mouse.
These events can be found by the same name in the Turret Pawn then. They are constantly called
and will pass a float value that is 0.0f until you move the mouse up/down, or for the second axis, left/right.
These Events are LOCAL. Means they will only be called by the Local Player who possess the Turret.
That means Player1 can’t use them for now. But now you need replication.
So you want to setup the Movement that it works for Player2 locally. Even if you replicate, you want to have the Player2 get the Input directly applied, since he would notice a lag if not.
Now you add so called “RPCs”, which can be used to transfer a call to the Server version of an Actor.
To create then, add a “CustomEvent” and in its settings, set it to “RunOnServer”.
(It’s important to note, that RPCs only run on the Server OR on ClientOwned Actors. PlayerController or a possessed PlayerCharacter(Pawn), are client controlled, so we are good to go.)
The RunOnServer RPC needs the Float Value that the Mouse Event passes. And then, you just do the same stuff on the Server again. So you do it twice. Once on the Client, to prevent Lag and one on the Server to replicate it.
That’s “all”, so try to accomplish this step by step and if you run into problems, please post a screenshot of the setup at this point and ask (:
Cheers!