Search in
Sort by:

Question Status:

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
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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 :)


Product Version: Not Selected
more ▼

asked Dec 09 '14 at 07:43 PM in C++ Programming

avatar image

311 37 39 44

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Dec 10 '14 at 06:37 AM

avatar image

9.6k 320 205 450

avatar image ScottSpadea Dec 10 '14 at 07:09 PM

you should look into abstract classes and polymorphism: http://msdn.microsoft.com/en-us/library/k535acbf(v=vs.71).aspx http://msdn.microsoft.com/en-us/library/9w2ctx1s(v=vs.71).aspx

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.

avatar image ScottSpadea Dec 10 '14 at 07:37 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question