I’m working on a multiplayer game where a player’s pawn can spawn an actor (a robot character) and control it. The player keeps possession of the pawn but the player also has the ability to control the robot at the same time. When the robot is destroyed, the pawn is able to spawn another robot.
The issue I’m having is with replication and assigning the spawned robot to the correct player to be controlled.
I’ve tried calling a function on the player controller to spawn the robot and while it works for the server, the clients aren’t able to control their spawned robot. Also multiple robots appear from the client’s point of view but not the server.
What would be the correct approach to set this scenario up? How can I keep possession of the pawn while spawning and controlling another character actor and have everything correctly replicated?
Can you show some screenshots of your code?
The top green area is in the pawn possessed by the player.
The middle yellowish area is in the player controller. The event is set to run on server and reliable. It sends the spawned actor’s reference (as ‘controlled unit’ back to the pawn to be controlled.
The bottom code is part of the movement controls in the pawn. It uses the ‘controlled unit’ reference to control the spawned actor. Other controls work in a similar way from the pawn by referencing ‘controlled unit’.
When I run this (this has produced the best result so far), the server works fine but the client has no control over it’s spawned actor. It is replicated now, but clients have no control at all.
I’m not sure whether you already do this (it is not included in your blueprints), but make sure that you do not try to call “possess” on the client. It works only on the server using the “server representation of the client player controller”.
One way to do this would be to directly provide the Controller as parameter for the “RobotSpawn” event and then for example store the data which controller the “Ice Man” is connected to. If you store the controller (in addition to the spawned actor) you can later easily call “Possess” for the correct controller. A better option would be to provide the controller later as parameter for the server event which is supposed to “possess” the “Ice Man”.
However, make sure you use the correct controller and call possess on server side, and things should be fine.
Edit: Also, if you don’t destroy the other character when possessing the new, you might want to “Unpossess” the previous character.
Thanks for your suggestions. In this case, possession never changes. The player is always in possession of “PlayerCamera” and indirectly controlled the spawned actor “IceMan”.
Another issue with the setup is that whenever a client joins a game in progress, the existing players all spawn another “IceMan” actor when they shouldn’t. Can you see any issues with this setup that might be causing this when new players join the game?
Regarding the camera: Camera is controlled separately from the “Possess” call. This can be quite tricky. If you have a fixed camera position, for example top down, you can disable “auto manage active camera” in your player controller:
For other methods, see for example the first 20 or so minutes of this tutorial:
(about “set view target with blend”)
About the issue with spawning additional “IceMen”: If “RobotSpawn” is really only called on keypress “P”, I don’t see why this should happen. So I’m sorry I cannot help there.
I think this solves my problems. I didn’t realize that it’s possible for the camera to be controlled independently from the possessed pawn.
The reason I had this setup is because it will be a VR game where the player controls a character in the world. So the player can physically walk around in their room while controlling their character’s movement. So I needed the camera to be completely detached from the character.
Based on this scenario, do you think it is a better option to take possession of the character and simply set the view target to a non-replicated camera in the world? Do you foresee any issues with all clients using one non-replicated camera in the world or would I need to create a camera for each player?
I’m sorry I don’t know about VR because I do not use it, but I suppose you need a “first person” camera for each client player, due to the nature of how VR works. (You usually see through the eyes of some character).
Anyway, if you have a VR issue, maybe it would be better to create a new question for this.
lotodore’s suggestion of disabling ‘Auto Manage Active Camera’ solved all of my replication issues.
Taking possession of the spawned pawn then setting the camera target fixed everything.
It was also possible for each player to use the same camera actor as long as it wasn’t replicated. This meant VR and non-VR players could all play in the same game.