I’ve run into a little dilemma.
In my game, I have two kinds of actors in play: active and passive. Active kids belong to clubs. Passive kids do not. Passive kids keep a list that represents their level of loyalty to all the clubs in play. If a passive kid is loyal enough to a club, he/she will follow members of that club. This level of loyalty could exist for multiple clubs at once.
To determine who the passive kid will follow I need to make a list of all the kids they might pick and sort that list from lowest loyalty to highest (or highest to lowest). There are conditions where a passive kid won’t be able to follow an active kid, so I need to be able to walk through the list and make second or third best picks if the best pick isn’t available.
The problem is that if I make a list containing loyalties and sort it, I don’t know what loyalty belongs to what club anymore.
I made this code to solve the problem:
# Passive kids will only follow an active kid who put an up arrow on them.
uAOwners = []
# creates a list to contain the owners of all up arrows on this passive kid.
uAOwners.append(self.iconManager.upArrow[0].getPythonTag("owner"))
# Adds the first up arrow owner to the list.
for uA in self.iconManager.upArrow:
# walks through all of the up arrows on this passive kid.
for O in uAOwners:
# walks through the owners currently in the list.
if(self.loyalties[uA.getPythonTag("owner").clubID] >= self.loyalties[O.clubID]):
# Checks if the loyalty to the current up arrow's owner is higher than or equal to
# the loyalty to the current owner in the uAOwners list.
uAOwners.insert(uAOwners.index(O), uA.getPythonTag("owner"))
# If so, the owner of the current arrow is inserted into the list before
# the owner that he was just compared against.
break
# We don't want to add the current up arrow owner to the list more than once, so
# after adding him we break the loop.
elif(uAOwners.index(O) == len(uAOwners) - 1):
# Checks to see if we are at the end of the owner list.
uAOwners.append(uA.getPythonTag("owner")
# If the current up arrow owner's loyalty isn't higher than or equal to any other's
# we just put him at the end of the list.
This code creates a sorted list to begin with, filled with the active kids that the passive kid could follow, sorted in descending order of loyalty. I’m confident this will work, but it isn’t a very elegant solution. I’d like to find a simpler way to solve this problem, if anyone has any ideas.