DirectButton Questions

Hello Everyone!

This is my first post so if it is in the wrong forum I apologize. I have also searched Google and these forums for an answer to my question but couldn’t find anything hence this post.

Ok, so, we are using Panda3D in my video game design class and we are working on a simple FPS. In the level we are designing we have to have a couple “hotspots” that trigger a quiz or a puzzle the player must solve in order to progress through the level. One of my ideas is to implement a chess puzzle an example of which can be found here:

chesswizards.com/?page_id=15

The basic idea is to display a chess board to the user and have them choose the spot where the player should move (the player doesn’t have to choose the actual chess piece to move, just make a guess as to which space should be used)

I want to implement it using the DirectGUI specifically have a chessboard made out of 64 DirectButtons with pictures on them to represent the pieces and the colored squares on the board. I know how to set this all up but here is where I am having trouble:

Is there a way to assign a value to each of the buttons so I can tell which button was pressed? Basically I want to be able to tell which button was pressed and compare it to the solution I have for the puzzle. I know you can assign a function to the button that runs after the button press, but I really need keep track of which button the player chose. If anyone could point me in the right direction I would be very grateful.

# I assume you create your chessboard as 8 rows and 8 collums of DirectButtons
for i in range(8):
	for j in range(8):
		button = DirectButton(...) # Setup the button as usual but don't assign a command to it
		button.bind(DGG.RELEASE, self.buttonPressCommand, extraArgs = [i, j])

def buttonPressCommand(self, i, j, arg):
	# arg must be there because that's how bind() works
	print "Button coords:", i, j

In fact, IIRC, you can also use the “command” attribute with an additional “extraArgs” attribute, but bind() gives you a lot more possibilities (most notably different button clicks (left, middle, right) and mouse hovering) so I prefer it. And mixing the two is not advisable because it makes your code incoherent and thus messy.

Hope that helps.

Nice! Thank you! This will help me a lot. I was really hung up on this.