Pass value by reference

I don’t understand why this line gives error C4239, something about non standard conversion from FVector to FVector& for RandomPt. I’m learning c++ I have no idea what is wrong please help

offending line

FVector RandomPt;
bool bFound = GetRandomPointInRadius(Bot->GetActorLocation(), SearchRadius, RandomPt);

and this is the funcion

bool AAIEnemy::GetRandomPointInRadius(FVector& Origin, float Radius, FVector& OutResult)
{	
	UNavigationSystemV1* NavSys = FNavigationSystem::GetCurrent<UNavigationSystemV1>(this);

	if (!NavSys)
	{
		return false;
	}
	
	FVector Result;
	bool bSuccess = NavSys->K2_GetRandomReachablePointInRadius(GetWorld(), Origin, Result, Radius);

	//Out
	OutResult = Result;
	return bSuccess;
}

Can you find what’s wrong?

You probably just want to use your OutResult as the out Result in your call to K2_GetRandom…

bool AAIEnemy::GetRandomPointInRadius(FVector& Origin, float Radius, FVector& OutResult)
{	
	UNavigationSystemV1* NavSys = FNavigationSystem::GetCurrent<UNavigationSystemV1>(this);

	if (!NavSys)
	{
		return false;
	}

        return 	NavSys->K2_GetRandomReachablePointInRadius(GetWorld(), Origin, OutResult, Radius);
}

… if you wanted to assign like you did above, i think you’d need to assign as a pointer, ie “OutResult = *Result;” … or is it “OutResult = &Result;” … one of those. my pointer operations never make sense when i’m running on no sleep.

Ty. I’ll check the function, but I get the error only when I call it

FVector RandomPt;
bool bFound = GetRandomPointInRadius(Bot->GetActorLocation(), SearchRadius, RandomPt);

AIEnemy.cpp(76) : error C4239: nonstandard extension used: 'argument': conversion from 'FVector' to 'FVector &'

checking on the web I find that when this error happens, the argument must be passed as constant

bool AAIEnemy::GetRandomPointInRadius(FVector& Origin, float Radius, const FVector& OutResult)
{
	...
}

but I don’t get it. What’s the point in passing a constant variable by reference if I’ll not be able to modify it, which is exactly what I’m trying to do? This doesn’t make any sense lol sorry for my ignorance.

The problem here is that GetActorLocation() returns an FVector by value, which you cannot directly pass to a mutable reference. Update your function to use const FVector& Origin.

1 Like

That was the problem, I would never have found it by myself. Thanks a lot.