Refactoring TMap<int*,bool> setting into a Function

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.

The function won’t work because in the end you aren’t pointing to the “value” anymore; you’re operating on pointers.

Pass value as a const int32 instead, copying around an int or bool will cost you nothing, no need for pointer to primitives.

Btw, never use “int” keyword, always specify the size.

Firstly, thanks for the reply. I figured it would be something of that sort. To clarify, why doesn’t *bool = *anotherbool; work.

“Edit: I did try passing the values as const int32, but as the distance was changing in a seperate area, it wasn’t being reflected in the function”

I’ll try to remain concise in my thought process:
I need high value to update, preferably without constantly setting it. So I made it a pointer.
I made the comparison values pointers so that I can use their address, so if they are the high value and they change, it’s instantly reflected.
In my mind, this makes it all as simple as adding the comparison value once to an array, and then looping through it every X amount of time to check for changes

I wouldn’t be surprised if I’m completely over-complicating it

I’ll also note, I’m not sure if it’s relevant to why the values aren’t being reflected from a seperate function, but the npc->Behavior is an instance of UBehaviors, where the comparison map is. The function the code above is in is within a static function. In case that makes a difference.