Keyboard Support

Excuse me, i’m a bit confused about self.accept , i dont know how it works…

I have my code here, but cant understand very well this support:

import direct.directbase.DirectStart
from direct.actor import Actor
from direct.task import Task
from pandac.PandaModules import *
from direct.showbase.DirectObject import DirectObject
from direct.interval.IntervalGlobal import *

base.setBackgroundColor(0,0,0)

class World(DirectObject):
	
	def __init__(self):
		
		entorno = loader.loadModel("models/environment")
		entorno.reparentTo(render)
		entorno.setScale(0.25,0.25,0.25)
		
		pandaActor = Actor.Actor("models/panda-model")
		pandaActor.reparentTo(render)
		pandaActor.setScale(.01)
	
	def keyUP(self):
		pandaActor.setPos(pandaActor, 0,1,0)
	
	def SetupEvents(self):
		self.accept("q",	["keyUP",1])
		

w=World()
run()

When user press “q” key, should do keyUP function, what is wrong there?

Upon a glance, it looks like you have a whole function just for your accept line, but you never call that function. Your self.accept line should be in your “init” function.

hm. i tried like you are telling me, i imagine you are talking about this way isn’t ?

import direct.directbase.DirectStart
from direct.actor import Actor
from direct.task import Task
from pandac.PandaModules import *
from direct.showbase.DirectObject import DirectObject
from direct.interval.IntervalGlobal import *

base.setBackgroundColor(0,0,0)

class World(DirectObject):
	
	def __init__(self):
		
		entorno = loader.loadModel("models/environment")
		entorno.reparentTo(render)
		entorno.setScale(0.25,0.25,0.25)
		
		pandaActor = Actor.Actor("models/panda-model")
		pandaActor.reparentTo(render)
		pandaActor.setScale(.01)
	
		def keyUP(self):
			pandaActor.setPos(pandaActor, 0,1,0)
	
		def SetupEvents(self):
			self.accept("q",	["keyUP",1])
		

w=World()
run()

Like this way does not work too, or maybe i did not capture your idea…
Do you thing is really well scripted the keyUP(self): function…

import direct.directbase.DirectStart
from direct.actor import Actor
from direct.task import Task
from pandac.PandaModules import *
from direct.showbase.DirectObject import DirectObject
from direct.interval.IntervalGlobal import *

base.setBackgroundColor(0,0,0)

class World(DirectObject):

   def __init__(self):

      entorno = loader.loadModel("models/environment")
      entorno.reparentTo(render)
      entorno.setScale(0.25,0.25,0.25)

      self.pandaActor = Actor.Actor("models/panda-model")
      self.pandaActor.reparentTo(render)
      self.pandaActor.setScale(.01)
      self.SetupEvents()

   def keyUP(self):
      print "up"
      self.pandaActor.setPos(0,1,0)

   def SetupEvents(self):
      self.accept("q",   self.keyUP)


w=World()
run()

You have made several mistakes in your script:

  1. You need to call SetupEvents to tell panda that “q” key is mapped to self.keyUP
  2. You make a mistake of the self.accept function call
  3. pandaActor need to be stored as a member variable. Otherwise you cannot find it in the keyUp method.

haa, got it…!

Its work, but just for moves for one time, but i think i was read something to repeat…

thanks you al.

it moves only one time because you say to panda:
if q is pressed, move the object to 0,1,0

if you want your object to keep moving forward you have to get the Y coord of the object and add 1.

newY = self.pandaActor.getY()+1
self.pandaActor.setPos(newY)

this way every time you press q it moves 1 up.

assainator

Yeah. The problem is this:

   def keyUP(self):
      print "up"
      self.pandaActor.setPos(0,1,0)

This always moves it to the position (0,1,0). You could do it like assainator said, or like this:

   def keyUP(self):
      print "up"
      self.pandaActor.setPos(self.pandaActor, 0,1,0)

This way you’ll move it relative to itself - meaning if the object is rotated, it still moves forward.

okey, all clear… thankS understood…

Is possible to define any function within self.accept???

for example…

if I press “q” key, that will do the SpinCameraTask function, but that function is a task, … so, i tried but not work…

could be that task functions have another way?

It didn’t work because the task required a ‘task’ as extra parameter. Either you need to pass a valid task object in the extraArgs parameter of the accept() call, or you need to remove the ‘task’ parameter from the spinCameraTask function (and remove the return value, of course.)

Keep in mind that the ‘q’ event still only gets invoked once. Use ‘q-repeat’ to get that event thrown every frame instead of only once.

very well… thank you, understood again.