DirectGui DirectButton

Currently, I am implementing DirectButton into my game for the start screen. I know that there is a command parameter for the button, but I have multiple functions that I want to do. I wanted to know if I could check which state the button is in. In the docs, it says the states are numbered 0 to 3, so I want to know if I can check which one it is so I can use it as an event. For example:

if DirectButton.state == clicked:
return task.done

Thank you

Edit: I also found that my program was running whatever function I put in the command parameter at the beginning when the Directbutton was being initialized. Is this supposed to happen?

Hmm… I’m not clear on how this should work. Surely the button will always be in the “pressed” state when clicked?

It’s not supposed to happen, indeed. At a guess, do you have brackets after your function-name in the parameter-list that you give to DirectButton? That is, do you do something like the following?

def myFunc(self):
    # Do something here

self.btn = DirectButton(command = self.myFunc(),
                        # other parameters here

If so, then your function is being called, and the result passed in to DirectButton, rather than the function itself.

1 Like

Yes, I was referring to the function like that, I’ll make the changes so it doesn’t do that. As for checking the state, if there is no way to get the state that the button is in, that is ok, I should be able to work around it. Thank you.

Good good. :slight_smile:

(To be clear, all that’s called for is to leave off the brackets after the function, I believe. That should then pass in the function itself, rather than the result of calling the function.)

It’s not that there’s no way to do it–I’m pretty sure that there is–it’s just that surely its state will always be the same when clicked, and so testing in a command-function doesn’t seem to make much sense?

I meant in a game loop. For example every frame it tests if the button is being clicked.

Hmm… Okay, fair enough.

I see two ways of doing this, but your solution is perhaps the better of them. (You could also bind various events–“enter”, “exit”, etc.–and use those to set some variable, but that’s a bit over-indirect here, I feel.)

So, it’s possible that there’s an easier way than the following, but it seems that it can be done something like this:

# In your initialisation:
    self.btn = DirectButton(<parameters here>)

    self.updateTask = taskMgr.add(self.update, "update")

# Elsewhere:
def update(self, task):
    print (self.btn.guiItem.getState())

The “getState()” method will return an integer representing the button’s current state.

1 Like

Try the example in DirectButton — Panda3D Manual and tell us if it works.

1 Like

That was what I was looking for, thank you

1 Like

I’ve got the buttons to work, I just ended up finding a way to use one function instead of using getState(), thanks.