Download

Get pointer to actor position

Hi I there way to get pointer to actor position? I need to add a FVector pointer to std::vector, but I don’t know how. I Can’t do this by AActor::GetActorLocation(), It doesn’t return pointer.
It is my code:



void AAspectsTransporter::OnOverlap(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    AAspectsTransporter *itemParent = dynamic_cast<AAspectsTransporter*>(OtherActor);
    ARubick *otherRubick = dynamic_cast<ARubick*>(itemParent);

    if ((OtherActor != nullptr) && (OtherActor != this) && (itemParent != nullptr) && (otherRubick == 0))
    {
        nearToRubick.push_back(OtherActor->GetActorLocation()); //here I try to add actor location to pointer vector
    }
}



I am obviously VERY late to offer a solution, but maybe you’ll appreciate advice.
I don’t understand exactly what you are asking for but I’ll offer knowledge that I think will help. I might include things that you already know. Disregard as necessary.


  1. Getting a pointer to anything
    To get a pointer to something, declare a variable for a pointer to that objects type. You can then use the address operator & to store the address of the object you want in the pointer variable you just declared. Heres a simple code example.
AActor* myActor = GetWorld()-><...>SpawnActor(...).     // Just so I have an actor for this example
FVector myLocation = myActor->GetActorLocation();    // Declaring, Instantiating, and Initializing

// To get a refence to this vector, I just use the "&" operator

FVector* myLocationPtr = &myLocation; 


  1. Adding a std::vector and an FVector
    Unless std:vector or FVector have functions that do this for you (I don’t think they do), you will need to do this manually. You can do this as follows

This is pretty straightforward. Many container objects (like a vector) can use the subscript operator [ ] to access elements directly.

std::vector<float> stdVector= {2.0, 3.2, 1.0};
FVector uVector(1.0, 2.0 1.3);

for (int i = 0; i  < 3; i++){
    uVector[i] += stdVector[i];     // add the elements of stdVector to that of uVector.
}

  1. Changing the location of the actor
    Your choice of words “I need to add a FVector pointer to std::vector…”, suggests that you might not be understanding what a pointer is. A pointer isn’t an object type, it’s the memory address that an object is stored at. If you want to add two things, be them std::vector or FVector, you NEVER add their pointers. You need to use the *dereferencing operating * * to get the object a pointer is pointing to.

I hope that this helps. My apologies if you know all of this already. If you see this at some point, good luck with future projects.

Just so you know, you’re not supposed to use any standard containers in UnrealEngine. Use TArray for lists, TSet for sets, and TMap for maps.

Also, you’re missing a comma here:

This thread is four years old folks… probably not worth bumping.

But hey since we’re here, don’t use std containers and don’t use dynamic_cast either.

Is there any real harm in using std containers? I realize that UE provides alternatives, but if an std container provides a benefit is there any real reason not to use it? If so, what is the harm?

This is correct. Thank you for catching this.

My guess is that is messes with UE4’s garbage collection. A standard container would probably normally need the programmer to do their own memory management.

I bet you that TArray and TSet are actually abstractions using the std containers. Just a guess though.