Is it possible to make a DirectObject class to handle some event handling. Import the class into a ShowBase class to handle those events and and handle other events directly in the show base class?
ShowBase inherits from DirectObject, you know. You can use ‘base’ as if it was a usual DirectObject instance.
A good thing about grouping events with DOs, though, is that you have functions that affect only selected events; like deactivating all keybodard keys with one method.
Say you have a steering DO for a vehicle and want to switch to a menu, which takes different input events, of course. In this case you could simply disable the steering DO, switch the menu DO on and vice versa.
I think I’m just confused. Here is my code
from direct.showbase import DirectObject Dist=.5 class Event(DirectObject.DirectObject): def __init__(self): self.accept('w',self.MovForward) self.accept('a',self.MovLeft) self.accept('s',self.MovBack) self.accept('d',self.MovRight) def MoveNP(NP): movNP=NP def MovForward(): NP.setPos(0,Dist,0) def MovLeft(): NP.setPos(Dist,0,0) def MovBack(): NP.setPos(0,-Dist,0) NP.setPos(0,-Dist,0) def MovRight(): NP.setPos(-Dist,0,0)
and the main app
from event import Event from direct.showbase.ShowBase import ShowBase class MyApp(ShowBase): def __init__(self): ShowBase.__init__(self) #insert mouse controls here. # Load the environment model. self.environ = self.loader.loadModel("models/environment") # Reparent the model to render. self.environ.reparentTo(self.render) # Apply scale and position transforms on the model. self.environ.setScale(0.25, 0.25, 0.25) self.environ.setPos(-8, 42, 0) self.disableMouse() EventH=Event.MoveNP(self.camera) app = MyApp() app.run()
You’re using the Event class wrong.
I suggest you make all its functions to methods (this is done by adding ‘self’ as first argument) and in your main file you use an instance of that classs then.