UE5 C++ how to use TArrays?

I have little idea on how to access a TArray in UE5.
This is my TArray ment to hold object references of class ATriggerPlatform.

UPROPERTY(EditAnywhere)
		TArray<class ATriggerPlatform*> TriggersAttached;

I want to get a varable(not change it) in one of those objects listed in the array. The varable I want to read is a boolean value.
How do I get a public varable from an object with a reference? Do I need to cast it to a pointer(if so how do I do this)?

There may be other ways to do this but:

  1. Iterate over the array you have. (TriggersAttached)
  2. When you find the instance of ‘ATriggerPlatfrom’ you’re looking for - it’ll have an index of say, 2.
  3. I’m pretty sure you can then access the member variables of that particular instance in your case by:

TriggersAttached[2]->bMyBoolean


The pointers in your Array are already expecting to point to instances of ‘ATriggerPlatfrom’ so unless your boolean is in a child class of ‘AtriggerPlatform’ I dont think youj’ll need to do any casting.

Just a fyi; GameDevTv has a discord server setup and its super helpful to jump in there sometimes and get direct help if you’re struggling with something.

Sorry I said an array full of references and showed an array full of pointers. I mean if I have an object reference to an object of class ATriggerPlatform what can I do with that reference to read public values from? Do I have to cast to it?
How do I cast to it? I’ve tried this: i is a object reference of class ATriggerPlatforrm and IsTriggered is a bool varable.(This doen’t work)

for (ATriggerPlatform& i : TriggersAttached)
	{

		bool value = Cast<ATriggerPlatform>(i)->IsTriggered;
	}

I use references pretty rarely, so i use them in function parameters and maybe if i’m mucking around wtih pointers - so i wasn’t sure what an array of them would actually be. I’m not sure if you can do that. I thought a reference was just a location in memory, in order to put that into an array you’d need to hold it in some kind of variable or pointer. as pointers seem to have names and also a spot to store a location in memory that they ‘point’ to.

I created a c++ class callled ‘TriggerPlatfrom’ in my code so i could try to test it out. I wasn’t able to get it working with the reference. So instead i implemented it using pointers and it works;

#include "TriggerPlatform.h"  //NEED THIS SO THAT THIS FILE KNOWS WHAT THE HECK AN ATRIGGERPLATFORM IS
...blah code...
...blah code...

void UTriggerComponent::mytest()
{
	
	TArray<ATriggerPlatform*>TriggersAttached;
	for (ATriggerPlatform* i : TriggersAttached)
	{
		bool value = i->IsTriggered;
	}
	
}

and i had this in my TriggerPlatform.h in the public section

	UPROPERTY(EditAnywhere,Category="Timsstuff")
		bool IsTriggered = false;

You can’t have an array of references anyway. What is TriggersAttached? Presumably it’s TArray<ATriggerPlatform*>, if it is then

for (ATriggerPlatform& i : TriggersAttached)

this isn’t going to work because you can’t initialise a T& from a T* which is the presumed element type of the array (because as mentioned you can’t have an array of references).

I don’t think you can have TArray<AActor&> Actors;

However, you can have TArray<AActor*>& Actors; You just have to Initialize it when declaring it. A Reference cannot be null.

TArray<AActor*> A;
TArray<AActor*>& B = A;

Essentially, what this does is store A within B, NOT A COPY of A. so if you remove an element from B. it will also be removed from A. I hope that makes sense. I’m trying my best to explain it. References have to be initialized when it is declared. I can think of one time they don’t have to be but you will never deal with that use case within unreal anyway.

You would access a reference like you would any other none pointer variable with the Dot(.); now if it is a reference to a pointer then you use the Arrow( ->).

A reference to a pointer looks like this AActor*& Owner = GetOwner();
you use reference’s cause you don’t want to copy values all the time. It takes up cpu power and memory allocation. Using reference’s is the best way to pass data around. I can think of a few times where you would want to “pass by value”(make a copy) of data, but its rare.

Think of it like this, lets say an AActor* is 8 bytes long. I’m not sure how big it is that’s just a number I’m throwing out there. Now lets say you have an array of 100 AActor*s. so your array is over 800 bytes long at this point. now if you assign your array to a new variable like:

TArray<AActor*> Actors2 = Actors1;

Then you just copied all them bytes when you didn’t have to. You could have just as easily
TArray<AActor*>& Actors2 = Actors1;

Think of a Reference as a Variable that holds another Variable.

your solution:

the range loop is not coping the pointer into the platform var on each iteration. It is “passing by reference”.
for (ATriggerPlatform*& Platform : Platforms)
{
Platform->Activate(true);
}

I have created an array of references like this.

TArray<ATriggerPlatform&>

I thought I could create an object of any varable and since a object reference can be a varable I was able to make an array with it. I understand now I can’t do much with a object reference except cast it and get a pointer out of it.
But how do I cast? I have tryed and it always says cast is overloaded.

can you take a code snippet and post it so I can visualize what your intent is?
shortcut on windows is win key + shift + s.

does the object you’re trying to cast from derive from the class you’re trying to cast to?
Cast syntax in Unreal looks like this

ACharacter* Character = Cast<ACharacter>(Object);

Cast <Class> (The thing that needs to be the class);

References aren’t objects so you can’t have an array of them. From the language’s point of view, they take up no space.

You don’t need to cast. You just need them to be pointers and not references.

But what if I can’t get pointers and can only get references? I still want to know how to cast in this situation with a object reference.

Heres my relevent code:
Cpp file

void AMovingPlatform::IsTriggered()
{
	for (ATriggerPlatform& i : TriggersAttached)
	{

		bool value = Cast<ATriggerPlatform>(i)->IsTriggered;
	}
}

header file

UPROPERTY(EditAnywhere)
		TArray<ATriggerPlatform&> TriggersAttached;

For some reson it allows me to make an array full of references. I haven’t put the references in yet so it might not like it then.

you need to add a * ATriggerPlatform*& in your loop. This (*) means pointer. You dont need to cast.

for(ATriggerPlatform*& i : TriggersAttached) { bool value = i->IsTriggered; }

That’s not possible. If you think it is, could you describe such a situation?

Cast takes an address and returns the same address if the cast was successful otherwise it returns nullptr. The code you have shown doesn’t need a cast at all ATriggerPlatform assumingly already has that member

void AMovingPlatform::IsTriggered()
{
	for (ATriggerPlatform& i : TriggersAttached)
	{

		bool value = i.IsTriggered;
	}
}

Though to answer your question

ATriggerPlatform* Thing = Cast<ATriggerPlatform>(&i);
if (Thing)
{
    bool bIsTriggered = Thing->IsTriggered;
}

UPROPERTY(EditAnywhere)
TArray<ATriggerPlatform&> TriggersAttached;

that will not compile! you cannot have an array of references.

change it to

UPROPERTY(EditAnywhere)
TArray<ATriggerPlatform*> TriggersAttached;

Yes it doesn’t compile but for some reason it doesn’t compile with this either.

TArray<ATriggerPlatform*> TriggersAttached;


My sistuation is I want to click on my object in the editor and then manualy add a reference or pointer of a triggerplatform that is in the editor. I thought I may not be able to select it if it was a pointer.

you need to forward declare ATriggerPlatform. so TArray<class ATriggerPlatform*> TriggersAttached;
don’t forget to include your header in your .cpp file.

You need to forward declare your type in the header and include its definition where you use its members (include the header in the source file).

Thank you it works now. I am able to select what I want to add to the array manualy. Thank you both for your help.

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

Privacy & Terms