How to modify integer inside of a lambda function?

Still working on my C++ and having a bit of a hard time with this one. Basically I’m looping through an array of FHitResult using RemoveAll and removing hits that are not visible using a line trace, but I want to stop doing that once I reach a predefined limit. Below is what I have so far, but doesn’t work.

UWorld* World = WorldContextObject->GetWorld();
int Found = 0;

Hits.RemoveAll([ World, TraceChannel, Location, Found, Limit ]( const FHitResult& Hit ) mutable {
	FHitResult TraceHitResult;

	if ( Found >= Limit ) {
		return false;
	}

	if ( World->LineTraceSingleByChannel( TraceHitResult, ( ! Location.IsZero() ? Location : Hit.TraceStart ), Hit.ImpactPoint, TraceChannel ) ) {
		return true;
	}

	Found++;

	return false;
});

This throws the below error.

\Engine\Source\Runtime\Core\Public\Containers\Array.h(2595): error C2672: 'Invoke': no matching overloaded function found
\Engine\Source\Runtime\Core\Public\Templates\Invoke.h(63): note: could be 'unknown-type Invoke(PtrMemFunType,TargetType &&,ArgTypes &&...)'
\Engine\Source\Runtime\Core\Public\Containers\Array.h(2595): note: 'unknown-type Invoke(PtrMemFunType,TargetType &&,ArgTypes &&...)': could not deduce template argument for 'ObjType'
\Engine\Source\Runtime\Core\Public\Templates\Invoke.h(51): note: or       'unknown-type Invoke(ReturnType ObjType::* ,TargetType &&)'
\Engine\Source\Runtime\Core\Public\Containers\Array.h(2595): note: 'unknown-type Invoke(ReturnType ObjType::* ,TargetType &&)': could not deduce template argument for 'ReturnType ObjType::* ' from 'const PREDICATE_CLASS'
        with
        [
            PREDICATE_CLASS=UTraceAttackHelpers::GetLimitedVisibleAttacksByRef::<lambda_1>
        ]

Can’t quite wrap my head around what it means. Anyone have any suggestions? To give some context lets say the Limit is 10. I want it to stop once doing traces it reaches 10 successful visible hits.

Perhaps you should add the return type to your lambda in this case.

Seems there is no overload for a mutable lambda,
capturing Found by reference instead should do the trick.


Hits.RemoveAll([ World, TraceChannel, Location, &Found, Limit ]( const FHitResult& Hit ) {
	FHitResult TraceHitResult;

	if ( Found >= Limit ) {
		return false;
	}

	if ( World->LineTraceSingleByChannel( TraceHitResult, ( ! Location.IsZero() ? Location : Hit.TraceStart ), Hit.ImpactPoint, TraceChannel ) ) {
		return true;
	}

	Found++;

	return false;
});
1 Like

That worked. Needed to remove mutable entirely and just pass by reference. Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.