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"

AddChildActor via function C++

Hello guys,

I've been trying to get versed with UE4 C++ now as I think I have become pretty familiar with blueprints. I made a game using blueprints and now I'm trying to replicate the same using C++. I've hit a roadblock and cannot find a proper solution.

Summarizing what I need is -

I have a Blueprint actor that I want to spawn in a C++ actor

I have set the blueprint actor in header files as this -

 UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Obstacles)
         TSubclassOf<class AActor> MyObstacle;

I have initialized the same in the constructor like this -

 static ConstructorHelpers::FObjectFinder<UBlueprint> My_Obstacle(TEXT("Blueprint'/Game/Blueprints/Obstacle_BPs/SimpleObs/BP_MyObstacle.BP_MyObstacle'"));
     if (My_Obstacle.Object) {
         MyObstacle = (UClass*)My_Obstacle.Object->GeneratedClass;
     }

Now, I also have defined and initialized three arrow components in my C++ Actor class like this.

Header -

 // Definition of arrow components 
 
     UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = SpawnPoints)
         UArrowComponent * LeftSpawn;
     UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = SpawnPoints)
         UArrowComponent * RightSpawn;
     UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = SpawnPoints)
         UArrowComponent * MiddleSpawn;

CPP -

 MiddleSpawn = ObjectInitializer.CreateDefaultSubobject<UArrowComponent>(this, TEXT("MiddleSpawn"));
     LeftSpawn = ObjectInitializer.CreateDefaultSubobject<UArrowComponent>(this, TEXT("LeftSpawn"));
     RightSpawn = ObjectInitializer.CreateDefaultSubobject<UArrowComponent>(this, TEXT("RightSpawn"));    


What I want to know is how to add/spawn MyObstacle as a child actor on one of the above arrow components ON FUNCTION CALL, like in blueprint you have "AddChildActorComponent" Node which takes transform as one of the inputs and class as another one.

Product Version: UE 4.13
Tags:
more ▼

asked Nov 10 '16 at 01:10 PM in C++ Programming

avatar image

SHOBBIT
650 12 13 27

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

2 answers: sort voted first

Ok, nvm I found an alternate to achieve what I wanted to do. Thanks for the answers though. Cheers.

more ▼

answered Nov 10 '16 at 05:38 PM

avatar image

SHOBBIT
650 12 13 27

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

I'm not sure I did understand what you asked for, but maybe you are talking about this:

     // Create the root node
     DefaultSceneRoot = CreateDefaultSubobject<USceneComponent>(
         TEXT("DefaultSceneRoot"));
     RootComponent = DefaultSceneRoot;
 
     // Attach a camera to the parent node (here it's the root node)
     Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
     Camera->SetupAttachment(RootComponent);

In this example, the camera will have the same transform as its parent (the root node).

Documentation is here : https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/USceneComponent/SetupAttachment/index.html

Good luck. Best regards.

more ▼

answered Nov 10 '16 at 01:33 PM

avatar image

NymKappa
692 31 20 54

avatar image SHOBBIT Nov 10 '16 at 02:00 PM

Apologies if I was not clear.

Simply, what I want is on an arrow component (Its transform to be more specific) that I created as above I want to add/spawn a child actor (Which is blueprint) through calling a function.

Basically this, alt text

request.png (93.7 kB)
(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