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)?
Iterate over the array you have. (TriggersAttached)
When you find the instance of ‘ATriggerPlatfrom’ you’re looking for - it’ll have an index of say, 2.
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
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 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.
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;
}
}
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.