0

Thanks in advance. Given these example of classes...



    # registers all that happens during a combat
    class CombatLog:
        def __init__(self):
            self.actions = []

    # it handles a combat
    class Combat:
        def __init__(self):
            self.fighters = []   # list of fighter instances
            self.combat_log = CombatLog()
        def run_round(self):
            # do whatever
            self.combat_log.log(...)

    # are the fighters in a combat
    class Fighter:
        def __init__(self):
            self.property1 = 1
            self.property2 = 2
            self.attacks = []  # list of attack instances
        def attack(self):
            # do whatever
            self.combat_log.log(...)  # clean way to access the log?

    class Attack:
        def __init__(self):
            self.damage = 10
        def cast(self):
            # do whatever
            self.combat_log.log(...)  # clean way to access the log?


    # from somewhere else in the code
    combat1 = Combat()
    combat2 = Combat()

I am wondering what would be the best pattern to handle the next requirements:

  • There is one unique instance of the CombatLog per Combat. So if there are 2 Combat instances, the combat would have 2 CombatLog instances.
  • I want to use the combat log everywhere is needed. For example, if a Fighter, the Combat or Attack, needs to access to the log. The CombatLog is like some "global" object ONLY within that Combat and all the instances within that Combat.
  • When the Combat is done and removed from the memory, the CombatLog gets removed as well.

The approaches I am thinking of, but I am not proud/sure of them:

  • Pass the log instance through all the children instances (the fighters, and the attacks)
  • Make some kind of log manager (like Singleton), and then request the log for that combat from where I want. But i have a similar problem, which is that I have to pass the id of that combat to all chindren in order to request the proper log to the log manager... Which is more o less the same problem than before.
  • I was thinking to make some kind of dependency injector, like using decorators or so, but I always crash against the same problem (the one before).

Maybe I need another approach, but I can't think of... The code is in Python.

Thanks a lot for your time.

timmyn
  • 3
  • 1

2 Answers2

1

If a fighter exists only in a combat, let it know in which combat it is:

class Fighter:
    def __init__(self, combat, name):
        self.name = name
        self.combat = combat
    def attack(self):
        self.combat.log.append('{name} attacking'.format(name=self.name))

If a fighter can switch combats, add a combat setter:

class Figthter:
    # init(self, name) ...

    def set_combat(combat):
        self.combat = combat
Andrew_Lvov
  • 4,621
  • 2
  • 25
  • 31
  • thanks a lot for your time. Yes this is the first approach I did. What I don't like, is that all that is created within that combat (fighters, attacks, teams, etc...) I have to give the combat object in all constructors... This is what I was not sure if would be a more "automated way" to do this. – timmyn Jan 11 '15 at 11:58
  • At the end I decided to approach this way. Thanks! – timmyn Jan 11 '15 at 14:20
0

There is another simple approach, anyway I think that the best option would be to have a pointer inside the other objects refering to the object Combat, but this doesn't seem possible:
Pointers in Python?
You could maintain a dictionary of Combats and pass their key to themselves and to the other objects:

combats = {uuid0: Combat(), uuid1: Combat()}

And access them like

class Attack:
    def __init__(self, combatUUID = None):
        self.damage = 10
        if combatUUID: self.combatUUID = combatUUID 
    def cast(self):
        # do whatever
        if self.combatUUID in combats: combats[self.combatUUID].log(...)
Community
  • 1
  • 1
TuTTe
  • 164
  • 4
  • You can certainly maintain a reference to `Combat` instances within other objects. No problem there. – jme Jan 10 '15 at 23:02
  • thanks a lot!. Yes this is the second approach I though, but like I said to @Andrew_Lvov, all that is created within that combat (fighters, attacks, teams, etc...) I have to give the combat uuid in all constructors... So I don't know if can be more automatic way to do this – timmyn Jan 11 '15 at 12:01