Be gentle, I’m only little and still very amateur. Thought I had my head wrapped around pointers and references, but perhaps not.
Here is the original code not inside a separate function, working as intended.
UBehaviors.cpp
///UE_LOG(LogTemp, Warning, TEXT("ThreatLevel : %d"), npc->Behavior->ThreatLevel);
///UE_LOG(LogTemp, Warning, TEXT("Distance : %d"), npc->Behavior->Distance);
if (!npc->Behavior->HighValue)
{
npc->Behavior->HighValue = &npc->Behavior->Distance;
npc->Behavior->Comparisons&npc->Behavior->Distance] = true;
}
for (auto& item : npc->Behavior->Comparisons)
{
///UE_LOG(LogTemp, Warning, TEXT("%d is the high value"), *npc->Behavior->HighValue);
if (*item.Key > *npc->Behavior->HighValue)
{
npc->Behavior->HighValue = item.Key;
///UE_LOG(LogTemp, Warning, TEXT("The address of HighValue is now %d"), npc->Behavior->HighValue);
item.Value = true;
///UE_LOG(LogTemp, Warning, TEXT("The value of %d is now %s"), item.Key, item.Value ? TEXT("True") : TEXT("False"));
} else if( item.Key != npc->Behavior->HighValue)
{
item.Value = false;
///UE_LOG(LogTemp, Warning, TEXT("The value of %d is %s"), item.Key, item.Value ? TEXT("True") : TEXT("False"));
}
}
I put that directly into a function, but the item.Values were not updating. So I tried to turn bool into a pointer as well.
UBehaviors.h
bool* True = new bool(true);
bool* False = new bool(false);
UBehaviors.cpp
void UBehaviors::CompareValues(ANPC* npc, TMap<int*, bool*> comparisonMap)
{
///UE_LOG(LogTemp, Warning, TEXT("ThreatLevel : %d"), npc->Behavior->ThreatLevel);
///UE_LOG(LogTemp, Warning, TEXT("Distance : %d"), npc->Behavior->Distance);
if (!npc->Behavior->HighValue)
{
npc->Behavior->HighValue = &npc->Behavior->Distance;
*npc->Behavior->Comparisons&npc->Behavior->Distance] = *npc->Behavior->True;
}
for (auto& item : comparisonMap)
{
UE_LOG(LogTemp, Warning, TEXT("%d is the high value at address %d"), *npc->Behavior->HighValue, npc->Behavior->HighValue);
if (*item.Key > *npc->Behavior->HighValue)
{
npc->Behavior->HighValue = item.Key;
UE_LOG(LogTemp, Warning, TEXT("The address of HighValue is now %d"), npc->Behavior->HighValue);
*item.Value = *npc->Behavior->True;
UE_LOG(LogTemp, Warning, TEXT("The value of %d is now %s"), item.Key, item.Value ? TEXT("True") : TEXT("False"));
} else if (item.Key != npc->Behavior->HighValue)
{
*item.Value = *npc->Behavior->False;
UE_LOG(LogTemp, Warning, TEXT("The value of %d is %s"), item.Key, item.Value ? TEXT("True") : TEXT("False"));
}
}
}
I created True and False because I needed a way to add a bool into the map
UBehaviors.cpp
npc->Behavior->Distance = npc->Scorer->GetDistanceScore(npc, npc->currentTarget);
if (!npc->Behavior->Comparisons.Contains(&npc->Behavior->Distance))
{
npc->Behavior->Comparisons.Add(&npc->Behavior->Distance, npc->Behavior->False);
}
But trying to read the value of the bools when set through the function, they all return true.
UBehaviors.cpp
//Movement determination
if (*npc->Behavior->Comparisons&npc->Behavior->Distance] ) // && npc->GetVelocity().Size() < 1
{
UE_LOG(LogTemp, Warning, TEXT("%s has moved to target"), *npc->GetName());
npc->movementComponent->MoveTo(npc, npc->currentTarget);
}
Currently, this code only runs on one npc, the only one in the level, if that’s important or not.
The first thing I need to wrap my head around is why the original code doesn’t work when refactored to a function. I did the everything while experimenting without understanding fully.