Search in

Sort by:

Everything C++ Programming

Current Space

Question Status:

All Questions With Answer With Accepted Answer

Quick results

Search help

Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both.
Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.

You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with
each other. Examples

[Closed] Control multiple pawns

0

Hi Guys,

I'm trying to figure out how to control multiple pawns within the game.

So what I want to do is, I want to start playing with a character in 3rd person view. While running through the level I want to be able to "enter" another pawn, things like a vehicle or something. So spawning a new pawn woundn't be an option, since the pawn already exists within the level.

I just could control the input with one playerController, which doesn't seem as "clean" to me. Since I would have to do alot of "switch / case" staements to figure out which pawn I'm actually controlling and what to do next.

My idea is to implement a playercontroller for every pawn. What I would have to do then, is to "posses" the pawn with the respective controller, which is quite easy, and I would have to hook up the new playercontroller. Exactly this is my problem. How would I declare that new playercontroller as the active one?

So I came a little further. UPlayer has a Method called "switchController" which I could use. However, I am failing at initializing my second playercontroller. Since a playercontroller does not have a default constructor, I do not know how to hand over the playercontroller.

My first one gets "magically" initialized by settings its class as default controller in the gamemode class.

The question has been closed Jul 11 '15 at 08:21 AM by Edregol for the following reason:

Hey, I think this is a logic problem. There's no need to switch controllers, that would make things much more complicated!? Why not make an interaction sphere component for the character, and identify/store whats in range of the character?

Thats exactly what I am doing. But I have to switch controls to the new pawn wich is within the interaction sphere. First option would be: Put IF/THEN/ELSE into your controllers movment methods to differentiate the pawn, which is, in my oppinion, very ugly. Depending on how many diffent pawns you plan to control those statements tend to get very long and the code gets difficult to read.

But this is exactly what they are doing in the Unreal Turnament code, which saddens me a bit. To be fair, they are only differentiating between UTCharacter and a SpectatorPawn. There is a small piece of code which handels driving within the UTCharacter class, but this doesn't do much. So not much to get within that code regarding my problem.

Second option would be: Implement a playercontroller per pawn and swap controllers as needed. Thats what I want to do. But I cant figure out how.

I've made an actor class thats the base class for things that I can interact with. Admittedly it's different, because I'm not taking possession of those objects in game, but the way the character interacts with different objects is very different. Each object is being reached through the base class. could you not do something like that? you'd have to build on top of the basic pawn...

It's not that hard to get the pawn I want to control. I already managed to accomplish that. The problem is to pass on the input commands in the right way. And by right I mean, in a way which is flexible enough to build upon it.

Later on I might want to control a stationary turret as well a a flying/hovering vehicle. Each of those have diffent input handlings. I can't imagine that putting another "else if" into the corresponding movement method of the controller is the answer to that.

The thing is, since the pawns are handled diffent, lets think of a normal character and a vehicle, I just can't put the inputs through, at least not all of them. I have to know which pawn I am controlling and handle the inputs accordingly. Which means, the move forward method of the controller has to react diffrent depending on the pawn I am currently controlling.

To possess the pawn isn't the problem. I would be able to know which pawn I am controlling within the controller itself. But currently I just can have only one controller, thats the thing. I think it would be better to use seperate controllers for each pawn.

Would that not be in the movement component of the object you're possessing? If I possess a default pawn i can fly around, if it's a character I walk around. I haven't changed the input settings of my game at all.

but the class you're controlling should have a SetUpPlayerUnputComponent function. That includes things like:
InputComponent->BindAxis("MoveForward", this, &AClass::MoveForward);
or
InputComponent->BindAction("ZoomIn", IE_Pressed, this, &AClass::ZoomIn);

You could bind any number of input commands in the Edit - Project Settings - Input section of your project...

So, not only can you bind inputs to different functions, per class, each classes implementation of "moveforward" could be completely different, because when your controller possesses the class, it's using that classes implementation...

Why don't you just put all of the input parameters inside each pawn class, then you have one player controller per player, and the player controller posses each pawn, which will have all of the inputs for that pawn stored inside the pawn class itself. That is how I did it, I had player controllers switch pawns over the network too.

I'm not sure why you are putting the pawn specific controls inside the playercontroller

Thanks for answering slayeruk. My problem was thinking way too complex as well as forgetting to map the inputs within the editor. The approach you've describled is the way to go, at least in my opinion. I did it the same way after realizing what I did wrong ;)