Problem with self.accept

I cant get self.accept to work. In this example it never seems to reach the setKey function.

  self.keys = {"turnLeft" : 0, "turnRight": 0,"accel": 0, "fire": 0}
        
        self.accept("escape", sys.exit)            #Escape quits
        #Other keys events set the appropriate value in our key dictionary
        self.accept("arrow_left",     self.setKey, ["turnLeft", 1])

Any ideas why?

Thanks

Paul

Make sure your class inherits from DirectObject.

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

class World( DirectObject.DirectObject ):
  def __init__( self ):
    self.accept( 'escape', sys.exit )

w = World()
run()

Yup I am inheriting from DirectObject.

Stange thing is that self.accept( “escape”, sys.exit ) works

but self(“arrow_left” setkey([“turnLeft”],1)) it just never seems to reach the setkey function

Just a little more meat on this problem because this has me going cross eyed now.

This code should close the program when space is press but it doesn’t any idea why?


import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *
from math import sin, cos, pi
from random import randint, choice, random

class World(DirectObject):
    
    def __init__(self):
        #set up the environ mint
        base.setBackgroundColor(0, 0, 0)
        base.disableMouse()
        self.keys = {"turnLeft" : 0, "turnRight": 0, "accel": 0, "fire": 0}
        
        self.accept("escape", sys.exit)            #Escape quits
        #Other keys events set the appropriate value in our key dictionary
        self.accept("space",          self.setKey, ["fire", 1])  

        self.gameTask = taskMgr.add(self.gameLoop, "gameLoop")  
        
    def setKey(self, key, val): self.keys[key] = val  
    
    def gameLoop(self,task):
        if self.keys["fire"]==0: sys.exit   
        return Task.cont
        
w = World()
run()

Good question. It does “work” but sys.exit is not closing panda3d, Im thinking it has something to do with the taskMgr stopping the closure as its waiting for a return value from the main loop.

However, one small problem with your code.

Here you set “fire” to 0


self.keys = {"turnLeft" : 0, "turnRight": 0, "accel": 0, "fire": 0} 

Then you say if space is pressed, “fire” becomes 1


self.accept("space", self.setKey, ["fire", 1]) 

And here in your main loop you test to see if it is 0, which it already is at the start.


if self.keys["fire"]==0:

You should be testing for the 1 value


if self.keys["fire"] == 1:

This works the same as testing for the 1 value


if self.keys["fire"]:

Try this…

import direct.directbase.DirectStart
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
from direct.gui.DirectGui import *
from math import sin, cos, pi
from random import randint, choice, random

class World(DirectObject):
   
  def __init__(self):
    base.setBackgroundColor(0, 0, 0)
    base.disableMouse()
    self.keys = {"turnLeft" : 0, "turnRight": 0, "accel": 0, "fire": 0}
       
    self.accept("escape", sys.exit)
    self.accept("space",  self.setKey, ["fire", 1]) 

    self.gameTask = taskMgr.add(self.gameLoop, "gameLoop") 
       
  def setKey(self, key, val):
    self.keys[key] = val
   
  def gameLoop(self, task):
    if self.keys["fire"]:
      print 'you pressed fire!'
      self.keys["fire"] = 0

    return Task.cont
       
w = World()
run()

P.S. This should really be in the scripting forum :wink:

Have fun!

That dodgy condition was me try to make the app close straight away I was just trying to get some kind of response out of it.

What you have given works and is pretty much the same as I was originally using I guess there is a problem with one of my methods then.