Loop Array in c++?

Hi,

Would anyone happend to know how i can make a c++ class that loops through an array in blueprint and find the heighest z value from that loop?

Z as the height of an actor?
Something like this:

float ZValue = MyActorsArray[0]->GetActorLocation().Z;
for (AActor* actor : MyActorsArray)
{
    float NewZ = actor->GetActorLocation().Z;
    if (NewZ > ZValue)
    {
        ZValue = NewZ;
    }
}
1 Like

Would you happend to know how i can expose that to blueprint, so the blueprint c++ node would take in a blueprint array of actor & then loop that array? :slight_smile:

.h:

UFUNCTION(BlueprintPure)
float GetHighestZ(TArray<AActor*> InActorArray);

.cpp:

float AMyClass::GetHighestZ(TArray<AActor*> InActorArray)
{
    if (InActorArray.Num() == 0) return 0.f;
    float ZValue = InActorArray[0]->GetActorLocation().Z;
    for (AActor* actor : InActorArray)
    {
        float NewZ = actor->GetActorLocation().Z;
        if (NewZ > ZValue)
        {
            ZValue = NewZ;
        }
    }
    return ZValue;
}

image

2 Likes

Thanks <3
I can’t seem to get the c++ class exposed to blueprint? I can only see the casting to function

How would you expose a class, it needs to be an actor or a component to use it in blueprint.

Sorry that was what i meant and trying, but still cant seem to find the node in blueprint

Where did you add the function ? let’s say you added it to ComponentX, in that case you need a reference to componentX and from it call the function, if you want to access it anywhere you might need to create a blueprint library class and create a static function.

You don’t need a blueprint library just mark the function as static

UFUNCTION(BlueprintPure)
	static float GetHighestZ(const TArray<AActor*>& InActorArray);

Still not able to get any reference…

Could your show your header file ? make sure the function is public, try adding to your character class for example.

In my example I added to the character class and I’m able to call it anywhere, I called here in the level blueprint

image

BlueprintPure wouldn’t compile if it weren’t public. And It doesn’t have to be static, idk, for me it works without being static.

Good point. The static is for calling the function in any blueprint you want, in your case you called it in the same class right ?

1 Like

Yes, I didn’t check it in other classes. Didn’t know it needs to be static for that. Thanks for the info.

1 Like

//your function specifiers, expose this function to bp
void GetZValue ( UPARAM(ref) TArray<MyActorsArray> & ZValueReturn);

check specifiers here: Link
hope it helps
cheers!

1 Like

Im not sure what i did, but after taking a look at it in the morning and updateing my compiler it worked :smiley:

Thanks alot, amazing community <3