How to choose the highest variable?

Started by newwaveburritos, Wed 17/11/2021 03:43:33

Previous topic - Next topic

newwaveburritos

Every character in my game has a different "Friendship Points" variable associated with them.  If I wanted to have something happened based on who your best friend is it would obviously be the character with whom you've accumulated the highest score.  How would you do that though in practice?  How would you select the character with the highest associated Friendship Points variable?

Crimson Wizard

#1
The method to find the max/min of object's values is:
1) have 2 temp variables to store the max/min value found and another to store the object's index (or object's pointer, if this is more convenient);
2) iterate over all objects in a loop, checking their values;
3) on each check, if the object's value is greater/less than the one stored in the temp var, then save this value in a temp var; and also save the object's index or pointer;
4) after finishing iterating you will have max/min value and the corresponding object stored.

Example (assumes that you have Friendship Points in the character's custom property):
Code: ags

int max_fp = -1;
Character *best_friend;

for (int i = 0; i < Game.CharacterCount; i++) {
    if (character[i] == player) continue; // skip player character
    int fp = character[i].GetProperty("Friendship");
    if (fp > max_fp) {
        max_fp = fp;
        best_friend = character[i];
    }
}

// at this point you should have best_friend selected

newwaveburritos

Oh, thanks, this explains it very well but I'm a little embarrassed to say that now that I have

Code: ags

best_friend


I'm not exactly sure what I can do with it.  Ideally I would be able to call best_friend like I call player as in

Code: ags

player.ChangeRoom(26);



Crimson Wizard

Quote from: newwaveburritos on Wed 17/11/2021 06:08:28
I'm not exactly sure what I can do with it.  Ideally I would be able to call best_friend like I call player as in

Code: ags

player.ChangeRoom(26);


Just like "player", "best_friend" is a pointer to Character, so you may use it like you use "player" or any explicit character's script name.

"character" in script is a global array of characters, each element of which may also be used same way, e.g. you may do "character[10].ChangeRoom" to move character #10 into another room.

newwaveburritos

I'm not sure what I was doing wrong last night where it wasn't working.  I guess I'll just chalk it up to the late night loopiness since I got it working just fine this morning.  Thanks again for your help.

SMF spam blocked by CleanTalk