Changing a variable value on self.accept?

Is there a way to change a variable value when using self.accept?

Right now I’ve got a model on the screen and can move it forward/backward, but it seems to be an excessive amount of code.

I tried to put this in, but Python blew a fit at me saying it wasn’t allowed:

self.accept('w-up',self.direction='Stopped')

So I’ve resorted to using functions just to change the variable of the self.direction variable, which seems a little silly.

Here’s my code:

    def __init__(self):
        self.loadTerrain() # just loads a basic surface
        self.debugConsole() # loads a custom framerate meter
        self.loadModel()   # loads a simple Actor called 'model'
        taskMgr.add( self.movingTask, 'movingTask')
        self.direction = "Stopped"
        
        self.accept('w',self.moveForward)
        self.accept('w-up',self.stopMoving)
        self.accept('s',self.moveBackward)
        self.accept('s-up',self.stopMoving)        
                
    def moveForward(self):
        self.direction = "Forward"
    
    def stopMoving(self):
        self.direction = "Stopped"

    def moveBackward(self):
        self.direction = "Backward"

    def movingTask(self, task):
        dX = 0
        dY = 0
        dZ = 0
        if self.direction == "Forward":
            dX = 0.1
        if self.direction == "Backward":
            dX = -0.1
        self.model.setPos( Vec3( self.model.getX( ) + dX, self.model.getY( ) + dY, self.model.getY( ) + dZ ) )        
        return task.cont

Is there some way to change the value of a variable within the self.accept statement? Or am I stuck using functions to change it?

The example ‘Roaming Ralph’ contains what you are searching.

Awesome, found it. Thanks :slight_smile:

Just for the record, if it is a new-style Python class (make it derive from ‘object’), you can do something like this:

self.accept('w-up', self.__setattr__, extraArgs=['direction', 'Stopped'])

Yeah, but why would you want to? :stuck_out_tongue: