This might be a very stupid question, but how can I do a pointer to a pointer? That is for example pointer x points to something, then pointer y points to pointer x, so when pointer x points to something else, pointer y will see the difference. How can this be done?
Sometimes you can see ** used for arrays that are filled with pointers. Like char** is actually a pointer to a pointer that points to a character like you can see it is done in the standard C main Function to handle multiple char arrays.
A pointer is a storage for a memory adress( meaning just a number), that additionally tells the compiler what kind of datatype he can expect at that adress. If there is another pointer to a datatype at that address and the original pointer tells this by indicating **, why not.
Your example above is really what you would do with a pointer most times, give it to some other class or use it as function parameter to access the original object. But take care, deleting the original object and then accessing it by a pointer that still has its address will crash.
But back to your original question, if you really need a reference to a pointer and it is no array of pointers, you might consider:
AActor* pointer1 = *whatever*;
AActor*& pointer2 = pointer1; // reference to the pointer
Without the UPROPERTY it worked perfectly, with it the engine just crashed when I started it. I guess what I want to do can be done with some kind of struct that has a pointer in it, right?
then initialize it in the constructor if it is a component. Otherwise in BeginPlay. Or this here sounds like it is bound to what you look at. That would most likely be updated in the Tick function.
If the pointer does not get a value in the constructor right away it is usefull to initialize it as nullptr in the constructors initialization list, like this:
ATestActor::ATestActor(const FObjectInitializer& ObjectInitializer) :
Super(ObjectInitializer),
ActorPointerReferenceTest(nullptr)
{
}
void AGazeGuiElement::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
FCollisionQueryParams TraceParams(FName(TEXT("CurrentLookAtActorTrace")), false, this);
const FVector Start = Camera->GetComponentLocation();
const FVector End = Start + Camera->GetComponentRotation().Vector() * 100000;
TArray<AActor*> ActorsToIgnore;
ActorsToIgnore.Add(this);
FHitResult HitData(ForceInit);
UKismetSystemLibrary::LineTraceSingle_NEW(GetWorld(), Start, End, ETraceTypeQuery::TraceTypeQuery2, false, ActorsToIgnore, EDrawDebugTrace::None, HitData, true); // This will trace using the Camera Channel - ideal for VR visibility look at traces
ActorPointerReferenceTest = Cast<APickupableItem>(HitData.Actor);
// If your collision and stuff was set up correctly, and the actor you looked at was a APickupableItem, then the pointer should be something else as nullptr here...
}
Something like that.
** or *& are really only fore very special use cases that you should avoid and won’t need as a starter.