How do i Get tags from a component in code? c++

I know that to add and remove it is used:

pActorComponent->ComponentTags.Add(FName(“NewTag”));

pActorComponent->ComponentTags.Remove(FName(“OldTag”));

How do i Get tags from a component in code? c++

ComponentTags is just a TArray so you can use all this: TArray | Unreal Engine Documentation

I believe pActorComponent->ComponentTags is your tags array, you can just use ComponentTags[i] to get the tag you need.

@Tuerer

I have .h

public:

UPROPERTY(EditAnywhere)
class UInstancedStaticMeshComponent* box;

AMyActor::AMyActor()
{
box = CreateDefaultSubobject(TEXT(“box”));
}

void AMyActor::BeginPlay()
{
int RamdonNumber;

for (size_t i = 0; i < 3; i++)
{
	RamdonNumber = FMath::RandRange(1, 11);
	
	FString NewString = FString::FromInt(RamdonNumber);

	FName ConvertedFString = FName(*NewString);

	box->ComponentTags.Add(ConvertedFString);

	box->AddInstance(FTransform(FVector(0, i * 200.0f, 0.0f)));
    }

}

result:

how do I get the labels using a UE_LOG? using ComponentTags[i]??

I’m new. I do not understand the syntax. I want to place the UE_LOG inside the bucle for

I’m not so good at with C++ myself, I’m more familiar with C#, but the syntax should be about the same.


What I meant is if you can Add or Remove something to/from the Array of Tags, it means you can already access it. So if you write something like WriteLine(pActorComponent->ComponentTags[1]) you will get your “10” (well, maybe you have to write WriteLine(pActorComponent->ComponentTags[1].ToString()), because in the blueprints tags are not Strings but Names, IDK).


Since pActorComponent->ComponentTags is already your Tags Array, you don’t have to “Get” it like you would do in a blueprint. The very statement of pActorComponent->ComponentTags is the same as Getting.


I might be wrong though, I’m not really aware of all the particularities. If I am wrong, I beg your pardon in advance for misleading you. And maybe someone can correct me in this case.

@Tuerer

UE_LOG(LogTemp, Warning, TEXT("%d"), box->ComponentTags[1].ToString());

when compiling it gives me error: non-portable use of the class ‘FString’

and also

UE_LOG(LogTemp, Warning, TEXT("%d"), box->ComponentTags[1]);

error C2338: Invalid argument(s) passed to FMsg::Logf_Internal
see the reference to the creation of function template instances 'void FMsg::Logf_Internal

I must be wrong then, excuse me =)

%d formats a number. What you want is %s. FStrings need to be deferenced to supply as string argument.

So what you get is:

UE_LOG(LogTemp, Warning, TEXT(“%s”), *box->ComponentTags[1].ToString());

1 Like

@erikdubbelboer

thank you very much!!