I have the following code:
class Player: def __init__(self, username, trip, model): self.username = username self.trip = trip self.hp = 100 #### For player moving location/room #### def Move(self, dest): if dest == self.loc: return True # Check destination room is accessible from current room for room in aGame['rooms']: if room['ref'] == self.loc: for acsroom in room['acs']: if acsroom == dest: self.loc = dest return True return False
aGame is an array that is defined outside of this class, so this code does not work. Since there are likely to be many other functions in this class that might use the aGame array, I have to do this:
class Player: def __init__(self, username, trip, model, aGame): self.username = username self.trip = trip self.hp = 100 self.aGame = aGame #### For player moving location/room #### def Move(self, dest): if dest == self.loc: return True # Check destination room is accessible from current room for room in self.aGame['rooms']: if room['ref'] == self.loc: for acsroom in room['acs']: if acsroom == dest: self.loc = dest return True return False
Or it would be better to do this:
class Player: def __init__(self, username, trip, model): self.username = username self.trip = trip self.hp = 100 #### For player moving location/room #### def Move(self, dest, aGame): if dest == self.loc: return True # Check destination room is accessible from current room for room in aGame['rooms']: if room['ref'] == self.loc: for acsroom in room['acs']: if acsroom == dest: self.loc = dest return True return False
Or do I need to make aGame a global variable (if so, how, note that this class is in another file)?
Since aGame is an array that is used everywhere, it does not seem right to make copies of it inside each class. Maybe I'm wrong, I'm slowly learning OOP, so thanks for any help.