Download

Array access and filling to BP from CPP class

I have my code which looks like this:

and my BP which looks like this:

My array is coming back with a length of zero so obviously there’s a problem here. I’m guessing this is not the correct way to have a CPP class fill a BP created array of integers.

Basically, my goal is this: My BP class needs to get an array of tiles from itself to its designated ending spot. I’ll be iterating through this array to provide the AI movement for this class (As using the Unreal AI for this situation is extremely LOUSY… jerky movement and directional movement that keeps jumping around all over the place.)

Instead of this method, should I have an array defined in CPP and copy the array to one in BP after I generate the path? Would rather be able to do this the way I’m trying, though, as my functions are extremely fast and efficient and would be completely unobtrusive to normal gameplay.

Thanks! :slight_smile:

In the function signature for FindPath, take a reference to the array instead and have it declared in your blueprint. (void ALabyrinth::GeneratePath(int32 StartingIndex, int32 EndingIndex, TArray<int32>&OutPathTiles) {} ) <- Like this.
Your current solution is pass by value, meaning you create a copy of your array and pass it to the function. While the .cpp function may be correct, it only writes to the copied value of the array you pass to it. It has to either return this copy (switch from void to TArray<int32> and return PathTiles). This isn’t very good for performance, however. As I assume the size of PathTiles can scale up pretty quickly?
Instead you want to pass by reference. This allows your .cpp function to work on the same array you declare in Blueprint, just by going to the same memory address you supply it with in the function call.
This way the size of the array doesn’t affect performance, your function call only sends and receives a fixed size memory address.

You will also automatically get a return value which is the populated array in the blueprint node if you do this.

Another solution is have your blueprint inherit from your .cpp class and the PathTiles be simply be a member variable.

Good luck!

Yep. All I needed to do was add the & and I was golden (In the good way.) Thanks! Never tried to use templates as a parameter before, so wasn’t sure if I needed to do the normal pointer stuff or not. It has been a long time since I’ve been able to do any real coding. Pretty much when C++ was just getting popular. Lol!