How do I add ArrowComponent to array objects?

Hello!

I am following a tutorial on Youtube on how to implement a PushableObject mechanic into my game. The issue is that it is in Blueprints and I am trying to convert that into c++. I am stuck on a part where the instructor is (in Blueprints) adding an ArrowComponent (AddArrowComponent) to each object(FTransform) in an array(Push Transforms) within the blueprint constructor. These arrows are supposed to point to the direction each transform object is facing.

The tutorial Blueprint:

This is my code that I tried to convert:

PushableObject.h

private:

	UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Meta = (MakeEditWidget = true, ExposeOnSpawn = "true", AllowPrivateAccess = "true"))
	TArray<FTransform> PushTransforms;

PushableObject.cpp

APushableObject::APushableObject()
{
	PrimaryActorTick.bCanEverTick = true;

	MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
	SetRootComponent(MeshComponent);

	for (FTransform Transforms : PushTransforms)
	{
		UArrowComponent* ArrowComp = CreateDefaultSubobject<UArrowComponent>(TEXT("ArrowComp"));
		ArrowComp->SetupAttachment(RootComponent);
		ArrowComp->SetRelativeTransform(Transforms.GetRelativeTransform(Transforms));
		ArrowComp->SetArrowColor(FColor::Red);
	}
}

What it’s supposed to look like:

What mine looks like:

I am not sure what else to do, I’ve been at this for hours and I’m not sure what I’m missing. Any ideas?

Thank you in advance!

.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PushableObject.generated.h"

UCLASS()
class YOUR_API APushableObject : public AActor
{
	GENERATED_BODY()
	
public:	

	APushableObject(const FObjectInitializer &objectInitializer);

protected:

	virtual void BeginPlay() override;

public:	
	
	
	UPROPERTY(EditInstanceOnly, BlueprintReadWrite)
	UStaticMeshComponent* MeshComponent;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Meta = (MakeEditWidget = true, ExposeOnSpawn = "true", AllowPrivateAccess = "true"))
		TArray<FTransform> PushTransforms = {};

	UPROPERTY(EditInstanceOnly, BlueprintReadWrite)
	float arrowSize = 5;
};

cpp


#include "PushableObject.h"
#include "Components/arrowComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Kismet/KismetMathLibrary.h" 


APushableObject::APushableObject(const FObjectInitializer& objectInitializer) : Super(objectInitializer)
{

	PrimaryActorTick.bCanEverTick = false;

	MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
	SetRootComponent(MeshComponent);

}





void APushableObject::BeginPlay()
{
	Super::BeginPlay();

	int i = 1; 
	for (FTransform const Transforms : PushTransforms)
	{
		UArrowComponent* ArrowComp = NewObject<UArrowComponent>(this, UArrowComponent::StaticClass(), FName("Arrow" + FString::FromInt(i)));
		check(ArrowComp);
		ArrowComp->RegisterComponent();
		ArrowComp->SetHiddenInGame(false);
		ArrowComp->SetArrowSize(arrowSize);				
		ArrowComp->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::SnapToTargetNotIncludingScale);
		ArrowComp->SetRelativeTransform(Transforms);		
		FVector dir = (GetActorLocation() + Transforms.GetLocation()) - MeshComponent->GetComponentLocation();
		FRotator rot = UKismetMathLibrary::MakeRotFromX(dir);
		ArrowComp->SetWorldRotation(rot);
		ArrowComp->SetArrowColor(FColor::Red);		
		i++;
	}
}

@3dRaven Thank you for your help! This did work and the ArrowComponent did show up on each element of my FTransform array! Due to my own lack of attention, I did not mention that I need the ArrowComponent to be visible in the editor window, not during runtime.
Thanks to your help, I was able to do some research and modify the code you suggested.
I learned that I needed to input this code in a function called OnConstruction(const FTransform& Transform). This is the outcome:

.h

public:	
	UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Meta = (MakeEditWidget = true, ExposeOnSpawn = "true", AllowPrivateAccess = "true"))
	TArray<FTransform> PushTransforms = {};
	UPROPERTY()
	UArrowComponent* ArrowComp; // ArrowComponent attached to each of the transforms within PushTransforms array.

	UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true"))
	float ArrowSize = 1.f;
	UPROPERTY(EditInstanceOnly, BlueprintReadWrite, Meta = (AllowPrivateAccess = "true"))
	float ArrowLength = 40.f;

.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "Environment/Pushable/PushableObject.h"

#include "Components/ArrowComponent.h"
#include "Components/StaticMeshComponent.h"
#include "Kismet/KismetMathLibrary.h" 

// Sets default values
APushableObject::APushableObject()
{
	PrimaryActorTick.bCanEverTick = false;

	MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
	SetRootComponent(MeshComponent);
}

void APushableObject::OnConstruction(const FTransform& Transform)
{
	int ArrowIndex = 0;
	for (FTransform Transforms : PushTransforms)
	{
		Super::OnConstruction(Transform);

		ArrowComp = NewObject<UArrowComponent>(this, UArrowComponent::StaticClass(), FName("Arrow" + FString::FromInt(ArrowIndex)));
		check(ArrowComp);
		ArrowComp->RegisterComponent();
		ArrowComp->SetHiddenInGame(false);
		ArrowComp->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::SnapToTargetIncludingScale);
		ArrowComp->SetRelativeTransform(Transforms);
		ArrowComp->ArrowLength = ArrowLength;
		ArrowComp->ArrowSize = ArrowSize;
		ArrowComp->SetArrowColor(FColor::Red);

		ArrowIndex++;
	}
}

You have helped me a ton and I am immensely grateful! Thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.