Aller au contenu

Photo

Highest Ability Score


  • Veuillez vous connecter pour répondre
9 réponses à ce sujet

#1
The Mad Poet

The Mad Poet
  • Members
  • 425 messages

I'm not sure why I'm blanking on this, but I'm trying to pull the highest of the six ability scores. Is there a way to do it easier than doing a bunch of >. < comparisons between them? 



#2
meaglyn

meaglyn
  • Members
  • 809 messages

Something like this should work (untested for typos etc):

int nHighAbility = ABILITY_STRENGTH
int nHigh = GetAbilityScore(oPC, nHighAbility);
int i; 
for ( i = ABILITY_DEXTERITY; i <= ABILITY_CHARISMA; i ++ ) {
       int nTemp = GetAbilityScore(oPC, i);
       if (nTemp > nHigh) {
             nHigh = nTemp;
            nHighAbility = i;
       }
}

// here nHigh = high ability score, nHighAbility = ABILITY_* which is highets. Favors lower numbered one on ties.

  • The Mad Poet aime ceci

#3
The Mad Poet

The Mad Poet
  • Members
  • 425 messages

That's for comparing two of them. Useful, but is there a way to pull it off for all 6?



#4
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages

He did compare all 6 of them in the loop.


  • The Mad Poet aime ceci

#5
meaglyn

meaglyn
  • Members
  • 809 messages

What LF said...   It will work for all of them. I used the constants so that it would be easier to read. Read through what that for loop is doing again and see if  you can see it.


  • The Mad Poet aime ceci

#6
The Mad Poet

The Mad Poet
  • Members
  • 425 messages

Re-read. 

.

.

.

.

AH!

 

Thanks. Learn something new every day!


  • meaglyn aime ceci

#7
The Mad Poet

The Mad Poet
  • Members
  • 425 messages

Maybe I am reading this wrong, but here we go.

 

Since this works between the constants of Dex and Cha, wouldn't that be 1-5? Would not ABILITY_STRENGTH, which is 0, be ignored this way?

 

God I'm terrible at loops. I have no idea how I've avoided medication from using these.

 

Alright, the for works by cycling through the first expression, in this case 1 (for dexterity), until it reaches the second expression, which would be 5 (for charisma). The i++ means it goes up by one, which means that it would cycle 1, 2, 3, 4, right? If that's the case wouldn't it skip 0 and 6, Strength and Charisma? Unless that <= ABILITY_CHARISMA means that it also includes 5? Not sure there.

 

I see that it goes and checks it against the strength score and picks the highest though....

 

Should it be:

int nHighAbility = ABILITY_STRENGTH
int nHigh = GetAbilityScore(oPC, nHighAbility);
int i; 
for ( i = ABILITY_STRENGTH; i <= ABILITY_CHARISMA; i ++ ) {
       int nTemp = GetAbilityScore(oPC, i);
       if (nTemp > nHigh) {
             nHigh = nTemp;
            nHighAbility = i;
       }
}

I ask because I looked up the lexicon for what numbers the constant pulls, and double checked the for loop, because I plan to convert the pulled int to determine a string that names the ability score. Like:

string sAbility;

switch (i)
{
case 0: sAbility = "Strength"; break;
case 1: sAbility = "Dexterity"; break;
.
.
.
.
 


#8
The Mad Poet

The Mad Poet
  • Members
  • 425 messages

Ah heck... think I just answered my own question. Guessing starting at 0, then checking between 1-5 is right to find highest. I think that the <= ABILITY_CHARISMA was what was throwing me off.

 

Final code, haven't tested it yet.

//TMP - Returns the name of the highest ability score; help from meaglyn
string GetHighestAbility(object oTarget)

{
int nHighAbility = ABILITY_STRENGTH;
int nHigh = GetAbilityScore(oTarget, nHighAbility);
int i;
string sAbility;

for ( i = ABILITY_DEXTERITY; i <= ABILITY_CHARISMA; i ++ )
    {
       int nTemp = GetAbilityScore(oTarget, i);
       if (nTemp > nHigh)
       {
            nHigh = nTemp;
            nHighAbility = i;
       }
    }

switch (nHighAbility)
    {
    case 0: sAbility = "Strength"; break;
    case 1: sAbility = "Dexterity"; break;
    case 2: sAbility = "Constitution"; break;
    case 3: sAbility = "Intelligence"; break;
    case 4: sAbility = "Wisdom"; break;
    case 5: sAbility = "Charisma"; break;
    }

return sAbility;

}


#9
Proleric

Proleric
  • Members
  • 2 354 messages

You could initialise nHigh to zero then loop from ABILITY_STRENGTH to ABILITY_CHARISMA.

 

Only reduces the number of constants quoted by one, though.

 

Arguably, looping from 0 to 5 might be clearer.



#10
meaglyn

meaglyn
  • Members
  • 809 messages

Yes,  you could start with invalid values and loop through them all. The amount of code executed is about the same. I went with the approach that since I had to have an initial value for those variables I might as well use the first value we need to check and start the loop at the second.

 

As an aside,  from a software engineering point of view, I'd make that into two functions. One returns the int ABILITY_* that's the highest and a different one that gets the string, call it AbilityToString() or something. You may find other places where something like that will be useful by itself.

 

Also, why not just use the constant names in the case statements? That makes it easier to read as well. For the loop logic I did rely on knowing the actual values of the constants, but you would not even need to have looked them up to make the switch().

e.g.

 case ABILITY_STRENGTH: sAbility = "Strength"; break; 
 etc