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 direct.actor.Actor 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
        #&&&ERROR
        self.accept('w', self.moveCharlesForward()) #move Charles forward
        return
    def moveCharlesForward(self):
        global charlesY
        charlesY += .5
        return
    def __init__(self):
        #Create a function to move charles forward
        ShowBase.__init__(self)
        #load the small room (orientation = spawn camera)
            #textures
        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")
            #ground
        self.groundOrig = self.loader.loadModel("myWork/myModels/ground")
        self.groundOrig.reparentTo(self.render)
        self.groundOrig.setScale(0.25, 0.25, 0.25)
        self.groundOrig.setPos(0, 5, -1)
        self.groundOrig.setTexture(self.groundTexture)
            #left wall
        self.wallL1 = self.loader.loadModel("myWork/myModels/wall")
        self.wallL1.reparentTo(self.render)
        self.wallL1.setScale(0.25, 0.25, 0.25)
        self.wallL1.setPos(-2.2, 5.25, 0.01)
        self.wallL1.setTexture(self.wallTexture)
            #right wall
        self.wallR1 = self.loader.loadModel("myWork/myModels/wall")
        self.wallR1.reparentTo(self.render)
        self.wallR1.setScale(0.25, 0.25, 0.25)
        self.wallR1.setPos(2.2, 5.25, 0.01)
        self.wallR1.setTexture(self.wallTexture)
            #back wall
        self.wallB = self.loader.loadModel("myWork/myModels/wall2")
        self.wallB.reparentTo(self.render)
        self.wallB.setScale(0.25, 0.25, 0.25)
        self.wallB.setPos(0, 7, 0)
        self.wallB.setTexture(self.wallTexture2)
        #load the player
            #load Charles, the robot
        self.charles = self.loader.loadModel("myWork/myModels/charles")
        self.charles.reparentTo(self.render)
        self.charles.setScale(0.25, 0.25, 0.25)
        self.charles.setPos(0, charlesY, -.4)
        self.charles.setTexture(self.charlesTexture)
        self.KeyInput()


app = MyApp()
app.run()

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.