Keyboard Input Problem

So I’m trying to piece together keyboard input. My goal is to move a robot I’ve built forwards, along the Y axis. However, my current code returns an error when I try to run it;

Here’s my code so far:

from math import pi, sin, cos

from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from import Actor
from direct.interval.IntervalGlobal import Sequence
from pandac.PandaModules import Point3

import sys #used for Esc = Quit command

#create Charles's location in variables
charlesY = 5.0

class MyApp(ShowBase):
    def KeyInput(self):
        #Handle keyboard input
        self.accept('escape', sys.exit) #Exit the program when the Esc key is pressed
        self.accept('w', self.moveCharlesForward()) #move Charles forward
    def moveCharlesForward(self):
        global charlesY
        charlesY += .5
    def __init__(self):
        #Create a function to move charles forward
        #load the small room (orientation = spawn camera)
        self.groundTexture = self.loader.loadTexture("myWork/myTextures/groundTexture.bmp")
        self.wallTexture = self.loader.loadTexture("myWork/myTextures/wallTexture.bmp")
        self.wallTexture2 = self.loader.loadTexture("myWork/myTextures/wallTexture2.bmp")
        self.charlesTexture = self.loader.loadTexture("myWork/myTextures/charlesTexture.jpg")
        self.groundOrig = self.loader.loadModel("myWork/myModels/ground")
        self.groundOrig.setScale(0.25, 0.25, 0.25)
        self.groundOrig.setPos(0, 5, -1)
            #left wall
        self.wallL1 = self.loader.loadModel("myWork/myModels/wall")
        self.wallL1.setScale(0.25, 0.25, 0.25)
        self.wallL1.setPos(-2.2, 5.25, 0.01)
            #right wall
        self.wallR1 = self.loader.loadModel("myWork/myModels/wall")
        self.wallR1.setScale(0.25, 0.25, 0.25)
        self.wallR1.setPos(2.2, 5.25, 0.01)
            #back wall
        self.wallB = self.loader.loadModel("myWork/myModels/wall2")
        self.wallB.setScale(0.25, 0.25, 0.25)
        self.wallB.setPos(0, 7, 0)
        #load the player
            #load Charles, the robot
        self.charles = self.loader.loadModel("myWork/myModels/charles")
        self.charles.setScale(0.25, 0.25, 0.25)
        self.charles.setPos(0, charlesY, -.4)

app = MyApp()

Any help would be greatly appreciated.

The problem is here:

        self.accept('w', self.moveCharlesForward()) #move Charles forward 

You need to pass to accept a method itself (that is, without braces). You have been calling the method and passed the return value of the method to accept. This should work better:

        self.accept('w', self.moveCharlesForward) #move Charles forward 

Wow… I guess I’m not used to Python yet- I’m a Java / C++ programmer.

Thanks for fixing all of my problems, enn0x :smiley:

No problem. DirectObject.accept() needs to know which method to call when the event (a keyboard stroke in this case) happens. This is why you pass your own function here.

A C++ equivalent would be to pass a function pointer.

Java does not have function pointers. A workaround would be declare a callback interface (within the API) and pass an object which implements this interface.