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.
@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++;
}
}