Stimulating the message/event pump with arguments

I’m writing a set of classes to integrate the PyGame joystick module with P3D and have come upon the following problem. I want to be able to create an event and have the messenger pass it along in the form DirectObject subclasses are wired to handle. I want to pass an event with arguments and I cannot seem to figure out how to handle them. For example in the following code from my joystick class, I trap a joystick event from PyGame in terms of a button ID and a button state.

	# Internal button event callback
	def __buttonCB(self,buttonID,buttonState):
		evt = "JoystickButton"
		args = "%d,%d,%d" % (self.mDeviceID,buttonID,buttonState)
		messenger.send(evt,args)

This works wonderfully as I see traces from the messenger (with messenger.toggleVerbose()) such as the following where the message contains the control type (Axis, Button, or Hat) and a variable length argument list showing things such as the button ID, hat x and y, axis value:

:10-12-2006 15:46:19 Messenger(debug): sent event: JoystickAxis sentArgs: '0,2,-1.000031'
:10-12-2006 15:46:19 Messenger(debug): sent event: JoystickButton sentArgs: '0,9,1'
:10-12-2006 15:46:19 Messenger(debug): sent event: JoystickButton sentArgs: '0,9,0'
:10-12-2006 15:46:20 Messenger(debug): sent event: JoystickButton sentArgs: '0,8,1'
:10-12-2006 15:46:20 Messenger(debug): sent event: JoystickButton sentArgs: '0,8,0'
:10-12-2006 15:46:21 Messenger(debug): sent event: JoystickAxis sentArgs: '0,2,-0.897671'
:10-12-2006 15:46:21 Messenger(debug): sent event: JoystickAxis sentArgs: '0,2,-0.874050'
:10-12-2006 15:46:21 Messenger(debug): sent event: JoystickAxis sentArgs: '0,2,-0.834681'
:10-12-2006 15:46:21 Messenger(debug): sent event: JoystickAxis sentArgs: '0,2,-0.763817'
:10-12-2006 15:46:21 Messenger(debug): sent event: JoystickAxis sentArgs: '0,2,-0.740196'
:10-12-2006 15:46:21 Messenger(debug): sent event: JoystickAxis sentArgs: '0,2,-0.724448'
:10-12-2006 15:46:21 Messenger(debug): sent event: JoystickAxis sentArgs: '0,2,-0.708701'
:10-12-2006 15:46:22 Messenger(debug): sent event: JoystickHat sentArgs: '0,0,1,0,0.000000'
:10-12-2006 15:46:22 Messenger(debug): sent event: JoystickHat sentArgs: '0,0,1,1,45.000000'
:10-12-2006 15:46:22 Messenger(debug): sent event: JoystickHat sentArgs: '0,0,0,1,90.000000'
:10-12-2006 15:46:23 Messenger(debug): sent event: JoystickHat sentArgs: '0,0,-1,1,135.000000'
:10-12-2006 15:46:23 Messenger(debug): sent event: JoystickHat sentArgs: '0,0,-1,0,180.000000'
:10-12-2006 15:46:23 Messenger(debug): sent event: JoystickHat sentArgs: '0,0,0,0,-1.000000'

Now the problem is that I have been unable to figure out how to subscribe to such events with arguments and to get a hold of the arguments. I see in the documentation that I can associate an event with a callback function and send arguments to that function using accept(event,args). However, I have no ‘args’ to specify since they are already embedded in the event. If I use the ‘accept(event)’ syntax when I embed the arguments in the event, I get a TypeError. The ‘accept(event)’ syntax is fine when I don’t encode the arguments into the event. Can you lend me some guidance here?

Specifically:

  1. How do I use accept to register an object to receive the events with embedded arguments.
  2. Once I have registered an object for one of these events, how do I get a handle to the arguments that are embedded in it?

Here is the structure of what I am trying to do from the end user’s perspective. Here a class named SceneNavigator creates an instance of JoystickDriver that traps the PyGame events and translates them into P3D events as in the above code. The ‘self.accept(…)’ statements won’t work with the embedded arguments so I want to figure out how to replace these three statements with the appropriate calls to accept(…). I also want to replace the ‘pass’ statements with code to decode the arguments from the events.

class SceneNavigator(DirectObject) :

	def __init__(self,jDeviceID):
		self.mJoystickDriver = JoystickDriver(jDeviceID)

		self.accept("JoystickButton",self.joyButtonEvent)
		self.accept("JoystickHat",self.joyHatEvent)
		self.accept("JoystickAxis",self.joyAxisEvent)
	
	def joyButtonEvent(self):
		pass

	def joyHatEvent(self):
		pass

	def joyAxisEvent(self):
		pass

Thanks,
Paul

I believe you’re overthinking this one. The arguments are just passed as ordinary Python arguments, like this:

def joyButtonEvent(self, device, buttonID, buttonState):
      ... do stuff with device, buttonID, and buttonState

evt = "JoystickButton"
messenger.send(evt, (self.mDeviceID,buttonID,buttonState))

David

Ok that works great except I have to pass a tuple, not a list :

messenger.send(evt, (self.mDeviceID,buttonID,buttonState))

becomes

messenger.send(evt, [self.mDeviceID,buttonID,buttonState])

Thanks so much!
Paul