Assign pawns to controllers from multiple classes

Hi, I have a problem with making a succesful relationship between controllers and three pawn classes. There are AI and player controllers, that should be assigned to three different pawn classes: assault, heavy, sniper, each with it’s unique values on things such as health, speed and damage and then passing those to aiming and movement components. I managed it to work with one class by using getPawn method that returns cast to assault class, and that pawn is then used almost everywhere down the diagram. But how can I get pawns from those 2 other classes as well? I tried to solve it with separate controllers for each pawn class, but then they only noticed their own kind, because everything is based on that getPawn method, that retrieves pawns only from one class. I’m new to this and I ran out of ideas. Thank you for your time.

Hi,

What you need here is a base class for your pawn (and/or controllers) which will take care of your general functionalities such as input handling. In technical terms, it’s called inheritance. Inheritance, along with polymorphism, are very powerful features of any object-oriented programming language such as C++. Blueprints support these features as well, putting a powerful toolkit at your disposal. Please refer to my other answers (link1, link2, link3) where questioners had similar issues (You can think of these questions as examples to better understand these concepts).

In your case, what you would need to do is to create a base class based on character. Let’s call it MyBaseCharacter. Next, implement all your base input handling, movement functionalities, etc. that are common between your 3 types of pawns. You can also make common variables in this parent class as well and then change them in your pawn classes.

Now using this parent MyBaseCharacter class, right click on it and create new child blueprint based on it. This will be a unique child that will have access to its parent methods and properties. You can create as many children as you want and make an inheritance chain as deep as you want.

Having a parent that hold common functions and interfaces, you can take advantage of polymorphism here and pass your child classes around using their parent base class MyBaseCharacter as the argument type. You wouldn’t need to cast to a specific child each time, instead, you would use this parent and call its method which will link to the child and call the child method automatically for you (polymorphism again!). Definitely check those 3 other answers, especially the one about AI and behavior tree where I show how polymorphism works for that question.

Hope this helps!

Thank you! This is exactly what I needed. I just want to ask, can the class you named as MyBasedCharacter be based on Pawn class? Because the pawns in my project are just cartoonish boxes, that don’t need to swim or jump. And if I understood the polymorphism correctly, Can I have a blueprint class, attached to a C++ class, that is attached to the base class? Because I like to do things mainly in Visual Studio and in Blueprints I only like to do things like making door rotate, barrel elevate etc.

Absolutely! The 3 different classes you named sounded like you’re using Character and that’s why I said to create it based on the Character! Polymorphism and inheritance are object-oriented concepts and are not restricted to a specific class! You can easily do the same thing with a Pawn class :slight_smile:

The C++ approach you have in mind is a good one! See this answer for a good practice using C++ and blueprints. In this case, you can let your C++ class work as the base class. You would then create blueprints based on this class. Simply right-click anywhere in your Content Browser > Blueprint class, and search for the name of your c++ class (Alternatively, you can find your C++ class in the Content Browser, right-click on it and create a child blueprint based on it).

Did you know you can even check who the parent of your blueprint is? Simply open the blueprint, and on the top right corner you should see Parent Class: [Name of Parent Class]. You can easily change its parent from inside the blueprint by going into Class Settings.

Finally it works just like it’s supposed to. Thank you again for saving me lots of time and nerves :slight_smile:

You’re very welcome :slight_smile: