Could you describe which problems you get with this implementation?
Also I don't know if this implementation could work for your use case, but you could store one array per team, this way each character to look for a target would just have to look in the arrays which do not correspond to their own team,
and emit a signal when a character dies which is then picked up by your arrays holder to delete them, this way you can also avoid cycling through the array multiple times.
Example:
var teams = []
var numberOfTeams = 4 #or whatever
func initTeams( numberOfTeams):
for i in range(numberOfTeams):
teams.append([])
func lookForTarget(allyTeamIndex):
var targets = []
for i in range teams.size():
if not i == allyTeamIndex:
targets.append(teams[i]) #I believe there is a function to unpack this array if you need it, but I dont remember its name
return targets
func onCharacterDeath(teamIndex, characterIndex):
teams[teamIndex].delete(characterIndex)
The only problem of this implementation should be on the characterDeath function,
since if we delete a character from the team list all of the other index get shifted and need to be reevaluated,
we can solve it by just setting that index to null and if we are memory limited we can remember our null positions so the next time a character is spawned in we can put it in that index position.
or just simply do as you do and cycle through the array to delete it if death without knowing the object index,
hope this help at least a bit : )