x

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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Replacing components in child classes

Hi there guys, I've had a query running around my head for a while, I have the need to edit both the CharacterMovementComponent and PathFollowingComponent. In the project I'm working on I've been simply editing the source code, which I feel is bad practice. I'd much rather be simply inheriting from them, the issue comes up with inherting, then, from character/AIController which both build in these components and make them private access.

If I were to not make them private, would it even work that in the class constructor I simply "replace" the component with a childed version?

Example:

 ACharacter.h{
 ...
 /*(was private)*/protected :
 UCharacterMovementComponent* CharacterMovement;
 ...
 }
 
 ACharacter.cpp{
     ACharacter::ACharacter(const FObjectInitializer& objectInitializer) : Super(objectInitializer)
     {
            ....
            CharacterMovement = CreateDefaultSubobject<UCharacterMovementComponent>(..
     }
 }
 
 AMyCharacterOverrideClass.cpp{
     AMyCharacterOverrideClass::AMyCharacterOverrideClass(const FObjectInitializer& objectInitializer) : 
         Super(objectInitializer)
     {
            CharacterMovement = CreateDefaultSubobject<USomeChildOfCharacterMovementComponent>
                (..
     }
 }

I don't like the idea of having to hack into the base classes at all to set values to protected, but it seems better than making large changes frequently to the path following component and character movement, and having to recompile the whole engine (and everyone on my team's) every time I make a change. Is this something that's even possible? Will it cause horrible unforseen issues like the original character movement component hanging about in memory for reasons beyond my understanding?

Product Version: UE 4.14
Tags:
more ▼

asked Apr 26 '17 at 06:02 PM in C++ Programming

avatar image

Ungalyant
102 11 15 23

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

1 answer: sort voted first

From my understanding you don't need to declare it protected but can leave it private. The idea is something like this:

 // ACharacter.h
 static FName CharacterMovementComponentName; // set to some arbitrary name
 private:
 UCharacterMovementComponent* CharacterMovement;
 
 // ACharacter.cpp
 ACharacter::ACharacter(const FObjectInitializer& ObjectInitializer)
   : super(ObjectInitializer)
 {
   CharacterMovement = CreateDefaultSubobject<UCharacterMovementComponent>(CharacterMovementComponentName);
   // ...
 }
 
 // AMyCharacter.cpp
 AMyCharacter::AMyCharacter(const FObjectInitializer& ObjectInitializer)
   : super(ObjectInitializer.SetDefaultSubobjectClass<UMyCharacterMovementComponent>(CharacterMovementComponentName))
 {
   // ...
 }

This way the constructor of ACharacter does the assignment and the variable can remain private.

more ▼

answered Apr 26 '17 at 08:02 PM

avatar image

Benergy
831 21 4 29

avatar image Ungalyant Apr 27 '17 at 04:10 PM

That seems to have worked perfect, thanks so much for that!

(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