Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Tracking Mouse Button Held

Valence Sojourner
Junior Member
Join date: 1 Aug 2004
Posts: 2
08-04-2004 08:41
How do I make a gun call a function repeatedly as long as a mouse button is held? I am trying to create a gun with auto-fire, so I want it to call the 'fire' function repeatedly or peroidically as long as the user is holding down the left mouse button and stop when they let go.
Goshua Lament
Registered User
Join date: 25 Dec 2003
Posts: 703
08-04-2004 08:45
I've seen this done before. I have a public domain script that I'l post a snippet of here. I'll do it when i login.
_____________________
Flickr Second Life Photo Gallery

I no longer regularly login to SecondLife, but please contact me if an issue arises that needs my attention.
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
08-04-2004 12:45
One way I know of of doing something like this is via use of a timer event and a control event.

CODE

// Time in seconds between timer fires if a button is held down.
float FUNCTION_FIRE_INTERVAL = 0.1;

// A bitfield storing various CONTROL_* bits, which represent buttons being held down.
integer buttonsHeld = 0;

default {
attach(key id) {
llRequestPermissions(id, PERMISSION_TAKE_CONTROLS);
}

run_time_permissions(integer perms) {
// If user accepted permissions:
if ((perms & PERMISSION_TAKE_CONTROLS) == PERMISSION_TAKE_CONTROLS) {
state active;
}
}
}

state active {
state_entry() {
llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE);
}

control(key id, integer buttonsPressed, integer buttonsChanged) {
// If a button was pressed:
if ((buttonsPressed & buttonsChanged) != 0) {
// If no buttons *were* being held:
if (buttonsHeld == 0)
llSetTimerEvent(FUNCTION_FIRE_INTERVAL);

// Turn on all bits in buttonsHeld who's buttons were pressed.
buttonsHeld = buttonsHeld | (buttonsPressed & buttonsChanged);
}

// If a button was released:
if ((~buttonsPressed & buttonsChanged) != 0) {
// Turn off all bits in buttonsHeld who's buttons were released:
buttonsHeld = buttonsHeld & ~(buttonsPressed & buttonsChanged);

// If no buttons are now held:
if (buttonsHeld == 0)
// Turn off the timer, this reduces lag.
llSetTimerEvent(0);
}
}

timer() {
if (buttonsHeld & CONTROL_ML_LBUTTON) {
// Fire the function:
}
}

attach(key id) {
// If object was detached:
if (id == NULL_KEY) {
llReleaseControls();
state default;
}
}

run_time_permissions(integer perms) {
// If user revoked permissions:
if ((~perms & PERMISSION_TAKE_CONTROLS) == PERMISSION_TAKE_CONTROLS) {
state default;
}
}
}


I hope my bit math is right.
==Chris
Valence Sojourner
Junior Member
Join date: 1 Aug 2004
Posts: 2
08-04-2004 15:55
Thank you for posting your code. After analyzing it, I realized I could make a gun automatic with only a small change to the pop-gun code:

CODE

control(key name, integer levels, integer edges)
{
if ( (levels & CONTROL_ML_LBUTTON) == CONTROL_ML_LBUTTON )
{
// If left mousebutton is pressed, fire
fire();
}
}


I don't think any extra variables are necessary.

Delay can be added by setting a timer event in fire() that unlocks a boolean that supresses fire().