Camera movement on keystate

I’m trying to make my camera move on keystates.

Considder the following code, and then especially the set movement and update camera parts

from direct.showbase.DirectObject import DirectObject
import direct.directbase.DirectStart

class World(DirectObject):
    def __init__(self):

        model = loader.loadModel("Models/television.egg")

        nodepath = model.find("**/television")
        mytexture = loader.loadTexture("Models/Textures/shuttle.avi")
        model.setTexture(mytexture, 1)
        # Set up movement keys
        self.keystate = {'back':0} # Just a member variable to store the state of each key
        self.accept('s', self.set_keystate, ['back',1])  # Handler for 's' press
        self.accept('s-up', self.set_keystate, ['back',0]) # Handler for 's' release
        self.keystate = {'forward':0}
        self.accept('w', self.set_keystate, ['forward',1])
        self.accept('w-up', self.set_keystate, ['forward',0])
        self.keystate = {'right':0}
        self.accept('d', self.set_keystate, ['right',1])
        self.accept('d-up', self.set_keystate, ['right',0])
        self.keystate = {'left':0}
        self.accept('a', self.set_keystate, ['left',1])
        self.accept('a-up', self.set_keystate, ['left',0])
        self.lasttime = 0
        taskMgr.add(self.update_camera, "camerathread") # Start a new thread which then will loop
                                                        # forever to update the camera position
    def set_keystate(self, key, state):
        """ Set a stored state (either 0 or 1) for a specified key ('back',...)"""
        self.keystate[key] = state # Updates the member variable holding the states of the keys
    def update_camera(self, task):
        Update camera position and/or rotation for all active key states.
        elapsed = task.time - self.lasttime
        if self.keystate['back'] is 1:   # Checks the member variable for the current state of a given key
  ,0,-0.7*elapsed,0)  # "setPos" with 4 parameters makes the translation 
                                                         # relative to the first parameter                                               
        if self.keystate['forward'] is 1:
        if self.keystate['right'] is 1:
  *elapsed, 0, 0)
        if self.keystate['right'] is 1:
  *elapsed, 0, 0)

        self.lasttime = task.time
        return task.cont
base.disableMouse(), -3.5, 0.5), 0, 0)
World1 = World()

When I run this code I get the if self.keystate[‘forward’] is: 1

I’m kinda new to python and am wondering why it can’t read the forward member variable. Is it outside of the function’s namespace?

nvm solved it =)

self.keystate = {'back':0, 'forward':0, 'right':0, 'left':0}

Was overwriting the member variable 3 times with new values as I should have placed all the keystates within it at the same time.
Blame it on being noob at python :smiley:

Well in the constructor you overwrite the contents of self.keystate a couple times. The following by definition creates a variable named self.keystate with only one key (the last one, “left”). Then when you ask for something like self.keystate[‘forward’], that key doesn’t exist anymore since it was overwritten.

self.keystate = {'back':0}
self.keystate = {'forward':0}
self.keystate = {'right':0}
self.keystate = {'left':0}

One quick improvement would be to replace those lines with simply

self.keystate = {'back':0, 'forward':0, 'right':0, 'left':0}