(SOLVED) Dictionary.Keys.Max() not working

I am trying to work on AI, and I am trying to get the stat that has the highest amount, but my code only does the third thing, needing a mate.

 Dictionary<string, float> needs = new Dictionary<string, float>();
        needs.Add("hunger", stats.Hunger);
        needs.Add("thirst", stats.Thirst);
        needs.Add("urge", stats.Urge);
        string need = needs.Keys.Max();
        float needValue = needs[need];

        if (needValue > 0.3f) {
            if (need == "hunger")
            {
                curNeed = Needs.food;
                return;
            }

            else if (need == "thirst")
            {
                curNeed = Needs.water;
                return;
            }

            else if (need == "urge")
            {
                curNeed = Needs.mate;
                return;
            }
        }
        else
        {
            curNeed = Needs.wander;
            return;
        }

Does anyone know a way to fix this?

I wouldn’t take that approach for the simple reason that it it is needlessly complex.

Why not this:

curNeed = Needs.wander;

highestNeed = 0.3f;        // min before we stop wandering

if (stats.Hunger > highestNeed)
{
 curNeed = Needs.food;
 highestNeed = stats.Hunger;
}

if (stats.Thirst > highestNeed)
{
 curNeed = Needs.water;
 highestNeed = stats.Thirst;
}

if (stats.Urge > highestNeed)
{
 curNeed = Needs.mate;
 highestNeed = stats.Urge;
}

That seems far more understandable to me, and if I did me bits right, would result in the same output. Correct me if I’m wrong… I didn’t run the above code.

Finally I leave you with a quote from the band Devo:

Got an urge, got a surge and it’s outta control
Got an urge I want to purge 'cause I’m losing control
Uncontrollable urge I want to tell you all about it
Got an uncontrollable urge, it makes me scream and shout it

1 Like

The code will more than likely work! Thank you