Twine 2.0.11, Sugarcube 1.0.34
As a total code noob, I'm pretty sure I'm in out of my depth with this one, but I thought it wouldn't hurt to ask. Hopefully this makes sense.
In my game, NPCs are associated w/ objects that contain their stats. For instance, I use this for one NPC:
<<set $miko= {
Name: "Miko",
HPMax: 80,
HPNow: 80,
Wins: 0,
Losses: 0,
}>>
As the game progresses, these stats change depending on the actions of the player.
Later in the game, I'd like to have story branches that respond to which NPC character has the most wins. Does anyone have any thoughts on the easiest way to do this?
For what its worth, I was looking into putting each relevant NPC's stat into an array and using some kind of sort or compare function. Something like this:
<<set $leaders = [["Miko",$miko.Wins], ["Elle",$elle.Wins], ["You",$player.Wins]]
<<print $leaders.sort(function(a,b){return a[1] - b[1];})>>
Anyone know if I'm barking up the right tree here? Any thoughts?
Comments
That said, you already have the objects, which include both the name and wins for each character. Adding tuples to your sorting array solely to have the name and wins is somewhat superfluous.
Also, there's no reason I can think of to use a story variable here—doing so is wasteful as you're bloating the story history.
I'd suggest using your objects and the setup object. For example: That will sort the array ascending (lowest to highest) wins.
If you wanted the sort to be descending (highest to lowest), simply reverse the order of the variables in the subtraction. For example:
Once that's done, you can do whatever you want to the array—map it all to a particular format, access only one member, etc. For example: (assume a descending sort)