Getting data from actor components.

Hi All, I’m trying to get an int variable and TArray from an actor component.

In my blueprint function library I’m getting an actor by tag then getting reference to the Actor component on it.


 void UFunctions_LIBBPLibrary::MatSelector_ChangeMat(FName Tag, int MatID, TSubclassOf<UActorComponent> Component)
{
    UWorld* World = GEngine->GetWorldContexts()[0].World();

    TArray<AActor*> FoundActors;
    UGameplayStatics::GetAllActorsWithTag(World, Tag, FoundActors);

    //works to get component 
    UActorComponent* FoundComponent = FoundActors[0]->GetComponentByClass(Component);

    FString nname = FoundComponent->GetName();

    UE_LOG(LogTemp, Warning, TEXT("%s"), *nname);

}

I want to change the material from the Tarray on the child component but I cant get the array from my reference to run the following.


auto* newmesh = FoundComponent->GetOwner()->FindComponentByClass<UStaticMeshComponent>();
newmesh->SetMaterial(0, ArrayFromComponent[MatID]);

The code seems fine (assuming you left all necessary validation for demonsation purpose). So what exactly does not run? Or does it not compile?

I can’t get reference to the tarray in the actor component.

I have the object reference but can’t find the public variables on it. How do I get them when I have the object.

I assume I have to cast. I but note sure how to from a function library.

If you have



UCLASS()
class UMyComponent : public UActorComponent
{
    public:
    TArray<type> mArray;
} 

then you need to cast to UMyComponent* as UActorComponent* does not now the variables created in UMyComponent.

If you know how to do casting with reflected types, why you think it is not working in a function library?

just



UActorComponent* needsCast;
UMyComponent* comp = Cast<UMyComponent>(needsCast);