Count Touch on Object? (Touch1, Touch2)
|
|
Lysila Denja
Registered User
Join date: 14 Nov 2007
Posts: 20
|
02-11-2008 10:09
I have looked many places, but can't seem to find this. To be fair, I am very new to scripting. I do programming in SQL, VB, HTML, amd XML for work (HTML more for fun). I am creating something for a class that I am taking, and hope to teach this to others. The script that I want to create will be to open a drawer. I figured out how to get it to open, but if I want it to close when the avatar touches the drawer a second time. I have seen the timer method, and this could work, but would prefer to have the avatar choose to close it. Is this hard to do? Can anyone assist me in not just what the syntax is, but how it works. If you don't have time, I understand. I just want to make sure it is understood. I am not just looking to take an answer and use it. I am working to learn the language so that I can become proficient and share back with the group. Thanks!
|
|
Haplo Voss
Registered User
Join date: 18 Nov 2006
Posts: 137
|
02-11-2008 10:43
Just use a switch variable like this: integer switch = FALSE; default { touch_start (integer blah) { if (switch == FALSE) { //insert open drawer code switch = TRUE; } else if (switch == TRUE) { //insert close drawer code here switch = FALSE; } } } Pretty simple. If that's not what you meant... lemme know. - Hap edit: Sorry, as far as how it works... you are simply performing a "switch" code. This way, when you touch the object, and the state of the variable 'switch' is FALSE, the drawer opens, and then you change the variable to TRUE. Thus, the next time you touch the object, the first "if" statement fails - it goes to the next, and closes it, then changes the variable "switch" back to FALSE. A never ending 0-1 loop. That's way too wordy, hope it made sense.
|
|
Very Keynes
LSL is a Virus
Join date: 6 May 2006
Posts: 484
|
02-11-2008 11:16
And a more cyptic way to achive the same logic, just for comparrison: integer flag; default { touch_start(integer total_number) { if(flag=!flag)llOwnerSay("Open drawer"); else llOwnerSay("Close drawer"); } }
In this case LSL will evaluate” flag=!flag” and thereby toggle the state by assigning it to the opposite of what it currently is. "!" (Pronounced NOT) inverts it then "=" assignees it. The “if” will then branch according to the result. As LSL sees 0 as false, the initial state on declaring the flag is false. On the first touch it becomes true and opens the draw, on the next touch it becomes false and the “else” statement is executed. It is more compact, but less legible, unless like me you tend to prefer Assembler and other low-level languages. And you may often come across such tricks when reading other peoples scripts, so it helps to know a few of the short cuts. If you have not already done so, I suggest you download LSLEditor, it is great for testing ideas without having to go in to SL, especially useful when you are learning the language. And the built in WIKI means you can work and play with no Internet connection at all. Edit - Spelling
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
02-11-2008 11:33
assuming you are moving the postion of the drawer as a seperate object, something like the following is also possible vector vPosOffset = <-0.1, 0.0, 0.0>;
default{ touch_start( integer vIntTouched ){ llSetPos( llGetPos() + vPosOffset * llGetRot() ); vPosOffset *=-1; } }
in this example the drawer moves .1m local south (this can be changed dependant on how the object was put together) and inverts the offset constant. the next touch will use the inverted value, placing it back where it was, and then reverting the offset to it's original value.
_____________________
| | . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - 
|
|
Lysila Denja
Registered User
Join date: 14 Nov 2007
Posts: 20
|
02-11-2008 17:14
Thank you all for your help! Void, I ended up using your script, and to get it to come out towards the avatar, I changed the coordinates accordingly.
One question for anyone up to it - This has 4 separately connected components: 1) Main dresser - center, top and bottom linked 2,3 & 4) each a dresser with center, base and knob. Each dresser has the script. How do I link these? Every time I try the whole thing moves, or nothing moves. lol There's a newbie for you! Thanks for any help!
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
02-12-2008 05:35
if the drawers are NOT linked to the main dresser they just need the above script in the root prim of each drawer. if you want to link the whole thing together, each prim of each drawer will have to have the script, and need to send a link message on touch, and handle the moving part in the link message handler, instead of the touch handler. you'll also want a seperate message for each drawer set. something like vector vPosOffset = <0.0, -0.1, 0.0>; string vStrDrawer = "top";
default{ touch_start( integer vIntTouched ){ llMessageLinked( LINK_SET, 0, "top", ""); }
link_message( integer vIntSender, integer vIntNumber, string vStrCommand, key vKeyNull ){ llSetPos( llGetPos() + vPosOffset * llGetRot ); vPosOffset *= -1; } }
note linking it all together can sometimes show incorrectly due to lag effects, but it's easier to position the whole object. the unlinked version will use less scripts and show correctly, but it harder to set into position or initial placement (usually overcome by using a boxing solution like rez-foo, rez-faux, jack-in-the-box, or builders buddy)
_____________________
| | . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - 
|
|
Lysila Denja
Registered User
Join date: 14 Nov 2007
Posts: 20
|
02-13-2008 17:19
Thank you for your responses Void, and sorry I took a while to get back. I am getting this dresser ready for my first mock class, and got to the point where I decided to use your script on each drawer, and not link the drawers to the whole thing. I am also not going to worry about putting stuff in the drawers. Maybe someday after the class I will try working through all of your knowledge lol.
Anyway, I would like to ask if you mind me using the script that you posted for me, in my class. It would like to pass it out with the supplies as well. Please let me know if this is ok, and if you would like me to do any of the following: -Include a note card with any place you would like to advertise, giving you credit for the script - a landmark to a place you would like to promote - deny copy, modify or transfer permissions or all of them If you prefer to post what you would like here, that is fine. If you can send me a notecard online, that would be excellent. Thanks!
|
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
02-13-2008 20:06
if I post it here, it's free and clear to use anyway you see fit, with any permissions...
I think most here operate on this principle, unless it's otherwise commented in the script (like some library scripts, which request credit or special handling)
But if you want something to promote, there is a new rental island, started up by a friend of mine called New Toulouse. it's an old New Orleans themed sim, with 6 planned expansions including a swamp. I'f you'd like, there is a Demo of the style, (and a rough layout for the new sim) loctated in the 'Endless Destruction' Sim (northern half).
Seriously though, not necessary, but thanks for offering.
_____________________
| | . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - 
|