Game Development Community

pushbutton issue

by Howard Dortch · in Torque 3D Professional · 07/04/2012 (7:37 am) · 5 replies

I have need for the user to press and hold a button. It calls a command that checks the value of the button and if getValue() == 1 continue the operation otherwise exit.
I am moving a satellite dish so L,R,U,D buttons are presented to the user. Problem is after the call to move the satellite the button.getValue() always returns a 0 and the function exits.
How can I press a button and have the getValue() = 1 until the button is released?

#1
07/04/2012 (9:35 pm)
Define "button." Are you using a gui button or are we talking keyboard/mouse/controller buttons?

With either type I suppose you could catch the onMouseDown()/onKeyDown() and set a "holding" flag to true, then set it to false onMouseUp()/onKeyUp() event.

An example for keyboard is found in the default.bind.cs file (search for bindcmd)

moveMap.bindCmd(keyboard, "ctrl k", "commandToServer('suicide');", "");

First param is input source, second is the input to catch, third is the code to call on "make" (literally a short clip of script) and fourth is the code to call on "break." Then set up a function to toggle your flag.

The triggers are handled differently, though it should be possible to bind a trigger like:

function doCrouch(%val)
{
   $mvTriggerCount3++;
}

which is how firing, crouching, sprinting, etc. are handled, but I haven't looked into it.
#2
07/05/2012 (5:35 am)
I'm sorry pushbutton = guiButton with type field set to pushbutton rather than radio or toggle.
#3
07/05/2012 (11:52 am)
Ah, then in ToggleButton::onMouseDown() set your toggle flag and in ToggleButton::onMouseUp() clear it.

You'll need to check in an update() loop for the flag and do your thing if it's toggled, so not really pretty. But it should do the job if the update() isn't too crowded.
#4
07/05/2012 (2:25 pm)
I didn't want to use the toggle was the point here. I can do it with radio buttons since they keep the value button.getValue() = 1 when down. But I have to add another button for stop and didn't really want to do that either.
I want the user to just press a button and hold it down to move the dish and when they let up it stops moving. Thanks for the input.
#5
07/05/2012 (3:40 pm)
If you toggle it on in onMouseDown() and toggle it off in onMouseUp() it will move when the button is pressed and stop when it is released....

The onMouseDown() callback is triggered by a mouse click on the button and the onMouseUp() callback is triggered when that click is released. I can't remember, but I think that unless you catch onMouseLeave() and onMouseEnter() callbacks the onMouseUp() event will be lost if you click on the button and then move the mouse out of the button's area before releasing it, but other than that this should do exactly what you describe.

getValue() won't work, but something like this:

function MyToggleButton::onMouseDown(%this)
{
   %this.myValue = 1;
}

function MyToggleButton::onMouseUp(%this)
{
   %this.myValue = 0;
}

Now, instead of button.getValue(), just check button.myValue.