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"

Replace Root Component on an Inherited Class

Hi everyone!

So my question is: is it possible to replace the root component in a subclass if I've already changed it in the super class?

To explain more, I have a c++ class inheriting from Actor called BaseInteractable. I made the root component of BaseInteractable a SphereComponent. Now in a subclass of BaseInteractable, BaseInteractable_Child, I am trying to change the root component to a BoxComponent. But I am having issues with it. Does anyone know if a) this is possible to do and b) how I might do this?

Any help or advice is greatly appreciated thanks!

Product Version: UE 4.14
Tags:
more ▼

asked Dec 17 '16 at 09:57 PM in C++ Programming

avatar image

poplockentaco
239 21 12 28

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

1 answer: sort voted first

Alright never mind haha. It's always right after you think you can't do something that you find the solution.

So apparently the solution was very simple:

SuperClass.h

 // Declare a sphere component 
 UPROPERTY(VisibleDefaultsOnly, Category = Interact)
 class USphereComponent* InteractArea;

SuperClass.cpp

 // Create subobject in the constructor
 InteractArea = CreateDefaultSubobject<USphereComponent>(TEXT("InteactArea"));
 RootComponent = InteractArea;

SubClass.h

 // Declare a new component to use as the new root component
 UPROPERTY(VisibleDefaultsOnly, Category = Interact)
 class UStaticMeshComponent* SM;

SubClass.cpp

 // Create the subobject make it the new root component
 SM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SM"));
 
 // Note that this makes SM the new root but it then attaches the previous root to SM
 RootComponent = SM;
 
 /* 
  * The above code makes SM the new root as mentioned, but then it effectually does:
  *      InteractArea->SetupAttachment(GetRootComponent());
  * 
  * At least that's what I'm observing. It could be doing something else.
  */

This is the behavior I was going for, but hopefully others will find this helpful too.

more ▼

answered Dec 17 '16 at 10:34 PM

avatar image

poplockentaco
239 21 12 28

avatar image Napoleonite Jan 18 '18 at 05:46 PM

Yes you have to assign a new RootComponent and then call:

 InteractArea->DestroyComponent(); // Unregisters and destroys the old RootComponent

Otherwise the old component will still exist and will still interfere with the gameplay which may not always be intended.

(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