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"

Instantiate a UActorComponent subclass from a parametrized class in C++?

I'm building a character in C++, and I want to have a weapon component which is instantiated from a class which can be set in blueprint. In my .h file I have this to parametrize the class:

 /** Weapon class to spawn */
 UPROPERTY(EditDefaultsOnly, Category="Weapon")
 TSubclassOf<class UWeaponBaseComponent> InitialWeaponClass;

Then in the .cpp I am trying to instantiate like my other components, but it's throwing an error:

 MyCharacter::MyCharacter(const FObjectInitializer& ObjectInitializer):Super(const FObjectInitializer& ObjectInitializer)
     currentWeapon = CreateDefaultSubobject<InitialWeaponClass>(TEXT("InitialWeaponComponent"));

The error I get is No matching function to CreateDefaultSubobject.

So I guess InitialWeaponClass is not a class which can be passed to the template function CreateDefaultSubobject. What's the right way to do this?

Product Version: UE 4.11
more ▼

asked May 14 '16 at 04:43 PM in C++ Programming

avatar image

27 5 6 11

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

1 answer: sort voted first

InitialWeaponClass is a member variable not a type name for a function template.

Try non-template version of CreateDefaultSubobject, here is an example:

     UPrimitiveComponent* CurrentWeapon = nullptr;
     TSubclassOf<UPrimitiveComponent> WeaponClass = UStaticMeshComponent::StaticClass();
     CurrentWeapon = Cast<UPrimitiveComponent>(CreateDefaultSubobject(TEXT("InitialWeaponComponent"), *WeaponClass, *WeaponClass, true, false, false));

more ▼

answered May 15 '16 at 02:09 AM

avatar image

578 23 15 48

avatar image spencerkohan May 15 '16 at 06:26 AM

Thanks - doing it this way I'm no longer getting the compiler error. I am getting a runtime error though: it looks like my WeaponClass is still null in the constructor even though I've set a default for it in the blueprint.

So I guess the defaults set in blueprint are not available in the constructor? When do they become available, and when is the right time to use them?

(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