How to manage player pawn selection for a racing game?

Hi. I have a racing game and I am using a pawn for my race car. I want to add more cars with different properties. Right now I have all my blueprints casting to that specific race car pawn Actor. How would one make a pawn selection from menu and how to properly set up communication from other blueprints to the current pawn in usage? Thanks

Hi,

You can do that by using a Base Class for your vehicle. Your base class can be implemented in either C++ or blueprints (if it’s a big game with lots of functionalities then you should consider implementing in c++ for obvious reasons).

Even your current class might work as a base class for your other pawns. Your base class will inherit from the Unreal WheeledVehicle class and will take care of all the general shared functionalities for you (such as input handling which is the same regardless of what vehicle pawn you select to play with). For all your pawns you can simply use this base class and pass it around as function argument. You can easily cast it to a specific pawn or just use the base and call its functions (This is known as polymorphism and is a powerful and useful technique in an object-oriented language.)

Hope this helps.

Hi. Thanks for the answer. I cannot figure out how to use Base Class and where to find it. Is there a tutorial or documentation on that somewhere? Thanks

Unfortunately, I can’t recall a tutorial right now that would directly point you to this sort of stuff but pretty much all of them at some point use this technique. In short, a blueprint approach to using base classes would be as follows:

  1. Start by creating a base BP class based on WheeledVehicle. Let’s Call this MyBaseVehicle.

  2. Go to this MyBaseVehicle and implement all the shared logics, such as input handling and movement.

  3. Create a new BP class for your vehicle but this time based it on MyBaseVehicle. That is, when you right-click in content browser > Blueprint class, search for MyBaseVehicle and you will be able to select it as parent. Let’s name it MyChildVehicle.

  4. Open it, add a mesh, set up the animation and tune that specific vehicle’s movement parameters such as speed, torque, etc. (If all your vehicles will have the same rigging structure, you might even want to do the Vehicle setup > Wheels Setup in the BaseClass. Overall, if you see that you’re adding and repeating a same thing for every single instance, then you should consider doing that in your Base class.

  5. Now drag your MyChildVehicle blueprint into the level and set the Player 0 to take control of it. You’ll see that you’ll be able to control this even though you implemented all the input handling in MyBaseVehicle blueprint.

  6. Create as many vehicles as you want and set them all to inherit from MyBaseVehicle. You can easily control each one of them without having to reimplement movement handling for them over and over again.

You can also refer to my other answer in here where I explain the proper usage of inherits and polymorphism to handle AI attack based on a single parent base class.

Hope this clarifies it :wink:

Thanks so much. I can’t believe that worked so well, exactly what I wanted, thank you so much for taking time in answering that.

You’re very welcome :slight_smile: