0

I need to update my Game lists and variables in the Game's init function from my Troop class.

# trying to make a game
# here is a quick summary of what I am trying to accomplish

class Game(object):
    def __init__(self):
        self.var1 = 0
        self.xlist = [1, 2, 3]
        self.ylist = [6, 5, 4]
    def mainloop(self)
        for x in range(0, len(self.xlist))
            t = Troop(self.xlist[x], self.ylist[x])

class Troop(Game):
    def ally(self, x, y):
        x += 1
      # ^--v these formulas are more complex in the game
        y += 1

      # v-- what I need to update from Troop class
      # var1, xlist, and ylist
        # code code code
        # code code code

The Troop class basically makes a copy of these variables and lists and changes only them. They are not updating correctly for my full game.

  • Your problem is one of class design. There is no way for _any_ class to access an instance variable without, well, creating an instance - you could maybe try some ugly hack with `super()`, but your code will simply look silly and hard to read. Instead, change your design: make your `Game` instance variables into class variables instead. Then you can access them from anywhere. – Akshat Mahajan Jun 20 '16 at 04:07
  • but `var1`, `xlist`, `ylist` aren't class objects, they are instance objects i.e. instance variables. – juanpa.arrivillaga Jun 20 '16 at 04:08
  • Thank you Akshat Mahajan, my code works now! – David Schroeder Jun 20 '16 at 18:08

0 Answers0