Problem recreating "Roaming Ralph" movement code (noob here)

Only started learning Panda3D the other day and I’m a bit stumped as to the “right way” to have events happening continuously while a key is pressed, then stop immediately when it is released (i.e. moving the camera around).

I tried re-creating the basic bits of the movement code from the Roaming Ralph sample program, but for some reason my function moveCamera() won’t read the state of “forward” in keyMap, or else the function isn’t being triggered at all.

from direct.showbase.ShowBase import ShowBase
from direct.task.Task import Task
import sys

class MyApp(ShowBase):
	
	def __init__(self):
		ShowBase.__init__(self)

		self.keyMap = {"forward":0}

		self.accept('escape', sys.exit)
		
		self.accept('w', self.setKey, ["forward",1])
		self.accept('w-up', self.setKey, ["forward",0])
		
		self.taskMgr.add(self.moveCamera,'moveCamera')
	
	# Sets the value in the keyMap depending on if the key is pressed or not
	def setKey(self, key, value):
		self.keyMap[key] = value
		print("Hello") # Prints when a value in the keymap changes
	
	# The part that isn't working
	def moveCamera(self, task):
		if (self.keyMap["forward"]!=0):
			print("Things are happening") # Should print repeatedly while a key is being pressed; it doesn't

app = MyApp()
app.run()

Can anybody see why my code isn’t working? As far as I can tell I’ve recreated all the essential bits for this feature from the sample program, but it’s only half-working. Any help is appreciated, complete noob here :frowning:

You forget about “return task.cont” in the moveCamera procedure. Without this your task will be executed only one time and then it will removed from the task manager.

While that was also a problem, the bigger issue was that I forgot to call base.disableMouse() at the start. Thanks for your help anyways :slight_smile: