Retrieving custom class-types: Best practise?

Hello,

recently I’ve been pretty confused if I was doing something wrong my whole (Unreal-)lifetime:

When implementing custom classes (let’s say: ACustomPlayerController and ACustomPawn) the PlayerController-Object in ACustomPawn is still of type APlayerController so for every single custom class I write in every single object I basically create a “correct” getter-method that casts the APlayerController to ACustomPlayerController and returns it. Is that the way to go or is there any way to modify the type of APlayerController to ACustomPlayerController (same for other things, GameMode etc.) ?

If I did not do that, I couldn’t access any of my custom methods of ACustomPlayerController, so I pretty much have to do some sort of conversion, it just doesn’t feel right somehow :frowning:

Thanks in advance :slight_smile:

Regards

casting every time in your getter method is slower than casting once during an initialization method and storing a reference pointer.

on the other hand, you need to be careful with how specific your objects are when they communicate. if you have a playerController that tells your GoKart to steer, then you swap out the GoKart with a DirtBike, it will break your functionality. instead, it is better to tell their parent class Vehicle to steer, so the code is more generic.

the playerController should not know exactly what kind of pawn you have, but it should know the common parent class that all of your custom pawns derive from.

you should look into abstract classes and polymorphism:

the general idea is you have an abstract base class that defines the functions for interacting with your pawns, then you have child classes that override those functions and that define the behaviors that the functions control. anything you want outside objects to know about your child classes should be kept in the base class. that way, you can swap between child classes without needing a separate reference variable for each type.

i recommend you make a customPawnBase class based on Pawn, then make a customCharacterBase class based on your customPawnBase class, but in your customCharacterBase, you can either copy the code from character, or you can use multiple inheritance to inherit from character and yourCustomPawnBase.

then your player controller can have a reference to CustomCharacterBase and a reference to CustomPawnBase.

now, you can make classes that inherit from CustomPawnBase, like GoKart, Elephant, and Submarine. and you can make a few classes that inherit from customCharacterBase, like Police, Medic, and Pedestrian.

but these specific classes should not be referenced by your customPlayerController, they should only react to their base class functions with new behavior. if these child classes need new functions that can be called from other objects, you may want to move those functions into the base class, it should just change the behavior of its parent, without adding any new functionality to the interface.

eventually, your game might require a variety of police types, so you might turn Police into a base class called PoliceBase and make a few classes that inherit from it called Sniper, Swat, Pilot, and Guard. now your player controller should add a reference to PoliceBase, as well as PawnBase and CharacterBase.

so in summary, you don’t need to reference every single new class you create. you can instead reference base classes to interface with a variety of related objects. your player controller shouldn’t know how to use a shotgun, potion, or knife, it should just know how to use a weapon.