EQS: Scoring by distance multiple generated points against a context with multiple points

I have generated an EQS query that makes a semi-circle. I have created a context which returns multiple locations in a similar semi-circle.

For each EQS generated point I would like to find the closest point from the context and then score based on distance from that closest point. The built-in Distance test uses some sort of average to all context points so it does not work. Can anyone point me in the right direction?

Update: I think I have it figured out.

Here is the code:

ManyToManyDistance.h

#include "CoreMinimal.h"
#include "EnvironmentQuery/EnvQueryTest.h"
#include "ManyToManyDistance.generated.h"

UCLASS()
class POWERSHOT2_API UManyToManyDistance : public UEnvQueryTest
{
	GENERATED_BODY()

		UManyToManyDistance(const FObjectInitializer& ObjectInitializer);

		virtual void RunTest(FEnvQueryInstance& QueryInstance) const override;

		UPROPERTY(EditDefaultsOnly, Category = Trace)
			TSubclassOf<UEnvQueryContext> Context;
};

ManyToManyDistance.cpp

#include "ManyToManyDistance.h"
#include "EnvironmentQuery/Contexts/EnvQueryContext_Querier.h"
#include "EnvironmentQuery/Contexts/EnvQueryContext_Item.h"
#include "EnvironmentQuery/Contexts/EnvQueryContext_BlueprintBase.h"
#include "EnvironmentQuery/Items/EnvQueryItemType_VectorBase.h"
#include "Kismet/KismetMathLibrary.h"

UManyToManyDistance::UManyToManyDistance(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	Cost = EEnvTestCost::Low;
	ValidItemType = UEnvQueryItemType_VectorBase::StaticClass();
	Context = UEnvQueryContext_Querier::StaticClass();
}

void UManyToManyDistance::RunTest(FEnvQueryInstance& QueryInstance) const {
	//UE_LOG(LogTemp, Log, TEXT("F:RunTest"));

	UObject* QueryOwner = QueryInstance.Owner.Get();
	BoolValue.BindData(QueryOwner, QueryInstance.QueryID);

	bool bWantsHit = BoolValue.GetValue();

	if (QueryOwner == nullptr)
	{
		return;
	}

	FloatValueMin.BindData(QueryOwner, QueryInstance.QueryID);
	float MinThresholdValue = FloatValueMin.GetValue();

	FloatValueMax.BindData(QueryOwner, QueryInstance.QueryID);
	float MaxThresholdValue = FloatValueMax.GetValue();

	TArray<FVector> ContextLocations;
	if (!QueryInstance.PrepareContext(Context, ContextLocations))
	{
		return;
	}

	UWorld* World = GEngine->GetWorldFromContextObject(QueryOwner);
	
	for (FEnvQueryInstance::ItemIterator It(this, QueryInstance); It; ++It)
	{
		const FVector ItemLocation = GetItemLocation(QueryInstance, It);
		auto Distance = 20000.f;

		if (ContextLocations.Num() == 0)
		{
			return;
		}

		auto It2 = 0;
		for (It2; It2 < ContextLocations.Num(); ++It2) {
			auto ContextItemLoc = ContextLocations[It2];
			auto DistanceDelta = (ItemLocation - ContextItemLoc).Size2D();
			if (DistanceDelta < Distance) {
				Distance = DistanceDelta;
			}
		}

		It.SetScore(TestPurpose, FilterType, Distance, MinThresholdValue, MaxThresholdValue);
	}
}

For me, the trick was figuring out how to get context items and then iterating through them for each EQS generated item. Here’s the test in the Query in the UE4 editor:

I changed the reference value to 0.01 to give the results above. Notice how EQS generated points that are on the context points have scores of 1 while points that are not have lower scores.

Hope this helps the next person that has this issue.