Charging up things by holding a button.
|
|
Hanumi Takakura
Registered User
Join date: 24 May 2006
Posts: 57
|
03-17-2007 16:20
Hello. I'm trying to script something that whould charge up when holding the button. I tried going simple at first. if (levels & ~edges & CONTROL_ML_LBUTTON) { llSetTimerEvent() } Of course it didnt work. When I slapped in a llOwnersay("held"  call to it, instead of saying held once, it spammed my char screen with the message, so its not holding, its like pressing as fast as it can actually. so I went to step two. float charge; if (levels & ~edges & CONTROL_ML_LBUTTON) { charge +1; } if (charge == 100); { code here } Again. didnt work. I need the charge to start when starting to hold a button, then stopping and resetting to zero when released. How it's done then?
|
|
Kenn Nilsson
AeonVox
Join date: 24 May 2005
Posts: 897
|
03-17-2007 16:24
touch_start(integer num) { //stuff }
then...
touch(integer num) { ++charge; //stuff }
then...
touch_end(integer num) { //stuff }
_____________________
--AeonVox--Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms chasing ghosts, eating magic pills, and listening to repetitive, addictive, electronic music.
|
|
Hanumi Takakura
Registered User
Join date: 24 May 2006
Posts: 57
|
03-17-2007 17:58
Will try. Thanks.
|
|
RobbyRacoon Olmstead
Red warrior is hungry!
Join date: 20 Sep 2006
Posts: 1,821
|
03-17-2007 18:19
From: Kenn Nilsson touch_start(integer num) { //stuff } then... touch(integer num) { ++charge; //stuff } then... touch_end(integer num) { //stuff } I dont think that is going to work... For one thing, the OP's code shows that he is using control events to detect the button, and secondly he is using CONTROL_ML_LBUTTON which indicates to me that he wants it to work in MouseLook. I am not in-world at the moment to build a sample script and ensure that it works, but consider using something like the following code snippet. Keep in mind that this is basically off the top of my head, so don't shoot me if it doesn't work. default {
state_entry() { llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS); // get permission to take controls }
run_time_permissions(integer perm) { // permissions dialog answered if (perm & PERMISSION_TAKE_CONTROLS) { // we got a yes llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE); } }
control(key id, integer held, integer change) { integer pressed = held & change; integer down = held & ~change; integer released = ~held & change; integer inactive = ~held & ~change; if (pressed & CONTROL_ML_LBUTTON ) { // As long as they don't release the mouse, // then whatever it is will charge in 10 seconds llSetTimerEvent( 10.0 ); } else if (released & CONTROL_ML_LBUTTON ) { // They released the mouse, stop our "it's charged" // timer event from firing llSetTimerEvent( 0.0 ); } }
timer() { llOwnerSay( "It is charged" ); }
}
|
|
Hanumi Takakura
Registered User
Join date: 24 May 2006
Posts: 57
|
03-17-2007 18:37
Yea. it is for a gun Im making. I want it to charge energy to then fire the shot either by releasing the button after charging, or just firing it when the charge is completely.
EDIT: Your code seems to work. Somehow I tried basically the same call and it instead of holding once, just kept repeating the call, thus repeating the llSetTimerEvent part. Sould I always name the level and edge integers first so the script knows beforehand what is pressed, held and released? That's the biggest difference between this script and mine.
|
|
Gearsawe Stonecutter
Over there
Join date: 14 Sep 2005
Posts: 614
|
03-17-2007 18:59
For a less laggy version! float time; float charge; float charge_per_second = 1000;
default { touch_start(integer total_number) { time = llGetTime(); } touch_end(integer total_number) { time = llGetTime() - time; charge = charge_per_second * time; llOwnerSay((string)charge); } }
|
|
Hanumi Takakura
Registered User
Join date: 24 May 2006
Posts: 57
|
03-17-2007 19:11
Since the other code worked, I must ask. Will this less laggy version work in a controls and mouselook enviroment? I see it also uses touch starts and touch ends. I ask because I really don't know if using llGetTime would be faster than just setting a llSetTimerEvent. Of course the touch start and end part wouldnt work for what I need. Just that time part.
|
|
Kenn Nilsson
AeonVox
Join date: 24 May 2005
Posts: 897
|
03-17-2007 19:33
llGetTime() is faster than a llSetTimer(), but the touch_start and touch_end events would not really work in mouse-look.
Your best bet is to go with RobbyRaccoon.
_____________________
--AeonVox--Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms chasing ghosts, eating magic pills, and listening to repetitive, addictive, electronic music.
|
|
Gearsawe Stonecutter
Over there
Join date: 14 Sep 2005
Posts: 614
|
03-17-2007 19:42
sorry this is what you want this way you don't have to use timers. and maybe use llMinEventDelay(0.1); to slow down the repeat of the control event to reduce script time. float time; float charge; float charge_per_second = 1000;
default {
state_entry() { llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS); // get permission to take controls }
run_time_permissions(integer perm) { // permissions dialog answered if (perm & PERMISSION_TAKE_CONTROLS) { // we got a yes llTakeControls(CONTROL_ML_LBUTTON, TRUE, FALSE); } }
control(key id, integer held, integer change) { integer pressed = held & change; integer down = held & ~change; integer released = ~held & change; integer inactive = ~held & ~change; if (pressed & CONTROL_ML_LBUTTON ) { // As long as they don't release the mouse, // get the start time time = llGetTime(); } else if (released & CONTROL_ML_LBUTTON ) { // They released the mouse, stop our "it's charged" time = llGetTime() - time; charge = charge_per_second * time; llOwnerSay((string)charge); //or do something here like fire } } }
|
|
Hanumi Takakura
Registered User
Join date: 24 May 2006
Posts: 57
|
03-17-2007 20:01
Okies. I will try this one as well. If it's aimed at doing the same as the other code, it would then be a matter of seeing which makes less lag, if they make an important amount of it really, which I doubt. Thanks all for the help.
|