I have a pretty simple array like this:
players = []
I want to check if username is exists in the array, if so, then the user shouldn't be added. I don't think iterating through the array would be the smartest approach, because it might be to big to run this everytime.
I also thought it might be an idea to use a dict, but never did that before so I don't know if that would solve my problem.
My Player-Class looks like this:
class Player:
def __eq__(self, other):
return self._username == other._username
def __init__(self, x, y, name, sprite):
# more stuff
The main problem is, that I need to access this array from two different function, which is why I probably can't check with if character in players
Have a look at the full code:
This is where I add the character to my array:
@commands.command(name='join')
async def join(self, ctx: commands.Context):
character = Player(random.randint(100, 400), 210, ctx.author.display_name, random.choice(["blue", "red"]))
if character not in players:
await ctx.send(f'You are in, {ctx.author.name}!')
players.append(character)
else:
await ctx.send(f'You are already in, {ctx.author.name}!')
Here where I want to check if the name already exists in the array, so it will either print "can quest" or "can't quest, not ingame yet"
@commands.command(name='quest')
async def quest(self, ctx: commands.Context):
#check if player joined the game
print(players)
await ctx.send(f'{ctx.author.name} joined the quest!')
or similar?