Retrieving custom class-types: Best practise?
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 :(
Thanks in advance :)
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.
Follow this question
Once you sign in you will be able to subscribe for any updates here