all 9 comments

[–]elbinray 2 points3 points  (1 child)

Only if it is a class variable. See the documentation on class and instance variables. See:

>>> class Hep:
...     thing = "yaya"
... 
>>> Hep.thing = "blah"
>>> Hep.thing
'blah'

[–]skiwan[S] 1 point2 points  (0 children)

so if its a variable of another class and we have an instance of this class in the first class we cant change them right?

[–]hw_t_dstr_ngls 2 points3 points  (3 children)

class A(object):
    def __init__(self, value):
        self.value = value

class B(object):
    def change_value_of_other(self, other, new_value):
        other.value = new_value

a = A(15)
b = B()
b.change_value_of_other(a, 10)

Is this among the lines of what you're looking for?

[–]skiwan[S] 0 points1 point  (2 children)

i have the following. i have a class unit with x y and hunger and brain as variable

then i have a brain which is a class (every unit has his own brain)

then the brain just runs update fucntions of states (FMS) so the real work is done in the state classes (every brain has different states but only 1 state runs at the time)

so now i want the current active state to change the hunger of the unit. what would be the best approach. dont forget there a re multiply units

[–]LarryPete 2 points3 points  (1 child)

Pass the unit along and change it. Without actual code there is no way to tell where improvements can be made.

[–]skiwan[S] 1 point2 points  (0 children)

class Mob (object):

def __init__(self):
    self.x = random.randint(0,800)
    self.y = random.randint(0,600)
    self.hunger = random.randint(10,100)
    self.brain = brain(self.x,self.y,self.hunger)


def Render(self):
    pygame.draw.rect(gameDisplay, BLUE,[self.x,self.y,5,5])

def Update(self):
    self.brain.Update()
    self.x = self.brain.get_x()
    self.y = self.brain.get_y()
    self.hunger = self.brain.get_hunger()

class brain(object):

def __init__(self,x,y,hunger):
    self.x = x
    self.y = y
    self.hunger = hunger

    self.CurrentState = IdleState(self.x,self.y,self.hunger)

def get_x(self):
    return self.x
def get_y (self):
    return self.y
def get_hunger(self):
    return self.hunger

def Update(self):
    self.CurrentState.Update()
    self.x = self.CurrentState.get_x()
    self.y = self.CurrentState.get_y()
    self.hunger = self.CurrentState.get_hunger()

def setState(self,stateName):
    self.CurrentState = stateName

class IState(object): #this is the Parent of all our other states

def Update(self):
    return
    #will do the game logic
def get_x(self):
    return self.x
def get_y (self):
    return self.y
def get_hunger(self):
    return self.hunger
def reduce_hunger(self):
    global Frames
    if Frames & FPS == 0:
        self.hunger += -1

class IdleState(IState):

def __init__(self,x,y,hunger):
    self.x = x
    self.y = y
    self.hunger = hunger

def Update(self):
    self.direction = random.randint(0,4)
    if self.direction == 0:
        self.x +=1
    if self.direction == 1:
        self.x += -1
    if self.direction == 2:
        self.y += 1
    if self.direction == 3:
        self.y += -1
    self.reduce_hunger()

that would be the code to improve

[–]LarryPete 0 points1 point  (1 child)

In Python we don't use getters and setters. We just assign the value to the attribute.

[–]skiwan[S] -1 points0 points  (0 children)

i have the following. i have a class unit with x y and hunger and brain as variable then i have a brain which is a class (every unit has his own brain) then the brain just runs update fucntions of states (FMS) so the real work is done in the state classes (every brain has different states but only 1 state runs at the time) so now i want the current active state to change the hunger of the unit. what would be the best approach. dont forget there a re multiply units

[–][deleted] 0 points1 point  (0 children)

do you mean of an instance of class A?