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"

Create and Attach Custom Actor Component

Can someone explain me how to create the component and attach to the root component of my actor?

alt text alt text alt text

Product Version: UE 4.10
Tags:
more ▼

asked Dec 10 '15 at 01:23 PM in C++ Programming

avatar image

ErayT
235 36 37 169

avatar image ErayT Dec 10 '15 at 07:09 PM

I have found that only USceneComponents are supposed to be attached with "AttachTo" method. However, I still cannot find a way to add ActorComponents to Actors.

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

I believe I have solved the problem. There are things to mention before the solution:

"AttachTo" method is belong to "USceneComponent"s. Therefore, you are not supposed to attach an "ActorComponent" to them. The idea of attaching a component which is not a "USceneComponent" is also not true. If your component does not have any attribute in 3d world, you are not supposed to attach it to the components having attributes in 3d world.

Solution:

1) It is correct to create a component by using "CreateDefaultSubobject(TEXT("YourComponentName")).

2) If you dive into the API document for AActor, you find a method `AddOwnedComponent( UActorComponent * Component`). Yes, this is the method you should call if you want to add an actor component into your actor.

Here is my SpellCaster component inside a blueprint of my "Hero" class:

alt text

more ▼

answered Dec 11 '15 at 10:19 AM

avatar image

ErayT
235 36 37 169

avatar image GhostRavenstorm Feb 24 '17 at 04:49 AM

How would this work if you wanted to parameterize the template for which actor component is created? I intend to have a class defined in a variable in blueprint that gets passed to this function.

  UActorComponent* UApolloLibrary::ConstructActorComponent(UClass *actorComponent, class AActor *owner, FText name)
  {
      UActorComponent *newComp = owner->CreateDefaultSubobject<actorComponent>(name);
  
     owner->AddOwnedComponent(newComp);
  
      return newComp;
  }

 

This currently doesn't compile as I'm still trying to figure it out. https://answers.unrealengine.com/questions/565592/how-to-dynamically-instantiate-actor-components.html

(comments are locked)
10|2000 characters needed characters left

Try :

 RootComponent = SpellCaster;
more ▼

answered Dec 10 '15 at 05:09 PM

avatar image

Begounet
918 32 27 49

avatar image ErayT Dec 10 '15 at 07:11 PM

This does not help me in two things. First I still do not know how to initialize a component in c++. Second, my RootComponent holds USceneComponents.

avatar image Begounet Dec 10 '15 at 07:21 PM

I thought you knew, according to your screenshot, since it is the correct way to create your component. Then, to initialize, you just have to set any property from here :

 SpellCaster->SpellArray.Add(whatEver));

Or set the values directly in the constructor of your UActorComponent.

About the RootComponent, I did not see that it was an UActorComponent and not an USceneComponent. And as you said in your previous comment, only USceneComponents can be attached. So, you should inherit from USceneComponent instead of UActorComponent.

(comments are locked)
10|2000 characters needed characters left
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