Issue getting index of TArray

Unreal does not think you added anything to that array. You can verify this by printing out the value of locations.Num(). So really I’ll bet your question is how do you add to a TArray from a blueprint. You’ll have to show us how you’re currently trying to do it.

// In Header Class
UPROPERTY(EditAnywhere, Category = “Movement”)
TArray locations;

// In cpp Class
FVector variable = locations[0]; // Error is thrown here

I don’t do anything else other than fill the TArray, “locations”, from within UE4 and I receive the error “Array Index out of bounds” when attempting to obtain data from the TArray.

Thanks for any help you can provide.

Thank you for the insight. I do believe that is the issue. I am not currently adding to the TArray via a blueprint. Is there a way to do this via c++ or is this one of those moments blueprints will shine?

Thanks for the quick answer

Sure, in a Blueprint you would first add a node to get the locations array, and drag a pin off of it with an “Add” node.

In C++ you would just do

locations.Add(FVector(0,0,0));

(or whatever vector you want added to the array.)

Thanks. However, I was hoping I could follow something along the lines by using the UPROPERTY functionality.

Here’s a photo to better explain what I’m attempting.

232661-ue4-uproperty.png

Yup, you can certainly do that. I just recreated that in a project and it works fine. Where in your .cpp file are you accessing locations[0]? It’s probably before the scene has been created, like in a constructor or something. My project has an object with a Tick, I just accessed it from there and the data shows up just fine.

Thanks for taking the time to recreate what I have.

locations is defined in the header as a public variable.
Additionally, I have a private member function declared in the header.
In the .cpp, I also have a tick method which calls the above function which then attempts to access locations.

When I go to watch my variables while debugging, locations shows that it has 2 elements, but as soon as I type locations[0] in the watch, I receive the message “no operator [] matches these operands”

I attempted the watch during the tick and not in the created function to see if it would show something different, but I receive the same message.

Admittedly, I don’t know the specific details of Unreal’s garbage collector, but it has to be allocating its own memory from a pool so it can manage the reference counting. So I wouldn’t be surprised if it confuses the debugger’s ability to track a watch variable. It’s more than likely that “locations” just points to a single variable the garbage collector knows how to manage. So as far as the debugger’s concerned, “locations[0]” is invalid.

Try putting this into the tick routine just to verify that your data is what you think it is:

if(locations.Num() > 0)
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("locations[0] is ") + FString::SanitizeFloat(locations[0].X) + TEXT(", ") + FString::SanitizeFloat(locations[0].Y) + TEXT(", ") + FString::SanitizeFloat(locations[0].Z));
else
    GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("locations is empty"));

(Sorry, that code got mangled because the lines are so long, but just copy and paste it into Visual Studio and it should look right!)

Thanks for all the help.

It looks like I only have the issue when running it directly from visual studio. But have gotten it to work now.