Player joins the game and can be in two modes, possibly switching between them:
RTS type top-down camera mode, being able to select Characters and give them commands to move around (so that AI handles that) (movement input to camera)
Directly possessing a character (movement input to character)
Currently, the first case can be achieved by having a TopdownPawn which is effectively just an invisible camera moving in the world, listening to mouse clicks and giving AI commands to selected characters.
The second case can be handled by just deriving from a default Character that handles the input as UE does by default.
BUT, is it possible to make the first TopdownPawn also support (possibly temporarily) taking over an otherwise AI controlled character (which also wouldn’t have camera attached to it by default anymore)?
I have made test code (mostly in C++) trying to achieve that but still haven’t managed to get it to work fully yet. Basically, I added my own replicated PossessedPawn field to the TopDownPawn and redirect MoveForward, MoveUp etc. to it when one exists. Also, if there is a possessed pawn, camera position and rotation are modified to look at that. But currently, the setup does not work in a multiplayer game since GetLocalRole of the possessed pawn keeps returning ROLE_SimulatedProxy, even after calling SetAutonomousProxy(true).
So, before debugging this further, I wanted to verify from someone with more experience on Unreal that this is indeed something that could in theory be made work, and there aren’t any better ways to achieve the same result.
So, feel free to give opinions or better solutions :).
Your question seems to be: “Can my controller possess more than one pawn?”, and the answer to that is clearly no. Don’t expect Unreal to let your pawn be an autonomous proxy just by calling that function. Your pawn has to be possessed by the controller. Other than that you’re free to do what you feel best.
Why don’t you just possess directly the pawn you want to control?
The AI controller should go away automatically when you request to possess its pawn.
The top-down pawn should be left hanging and invisible where you left it, so you can return to it anytime (via a keybind or however you set it up).
When returning to the top-down pawn, you might have to respawn an AI controller for the previously possesed pawn.
I’m not sure I get your issue right. Why would the AI controlled character not have a camera attached to it? You don’t need different character classes for AI and player-driven characters. In most multiplayer FPS games with bots, the character class is the same whether it’s a player or a bot. Only difference is that one is controlled by a PlayerController while other is controlled by an AIController. Besides, for first-person view you don’t even need a camera.
There were some earlier related questions and answers which implied that it would actually be possible to have two pawns controlled by one controlled but I guess there were for single player only. But yes, I think have to consider other options then.
About AI controlled characters having cameras attached to them. It just feels wrong if there are dozens of characters and they all have an unused camera component attached to them. Doesn’t that mean there is overhead somewhere? Or is that how it is supposed to be done in Unreal and the camera actually does not take any resources when it is not attached to a pawn controlled by player controller? Or did you mean something else like manually attaching camera when player is possessed and removing it when unpossessed?
This might be a newbie question but yes, I am more used to how cameras work e.g. in Unity :).
No, it’s okay to have a camera attached to them even if they aren’t used. That way when you Possess the pawn in question ViewTarget will be automatically assigned to that pawn.
Ok, maybe that is right. It just feels “wrong” to have a camera component for every character if mostly one of them is active at the same time.
An alternative could be for example having a separate local-only actor in the scene whose purpose is to move the local camera around the scene. When possessing a new character, it would then update its target and keep focusing on that. Some of the camera controls (like changing the camera angles and distance) would then need to be redirected from the possessed pawn to the camera I guess. One good side of this is that the same camera could more naturally have completely different camera modes, too, like a cinematic camera or temporarily focusing on certain non-player target. But are there good reasons why this would be a much worse solution than making every character have a camera component?
You can ADD a camera component pre-possession. Not a huge deal. Yet this will have an impact at the time of possession. Overall its better to have the camera there and inactive. When you go to possess , simply activate the cam, then possess.