Touch Slide Lever

From Second Life Wiki
Jump to navigation Jump to search

Description

This sample script creates a slide lever which will move through a series of positions between two points, every time a user touches it. This script is a part of Haravikk Mistral's Lever Library.

When touched this lever will move in a given direction until it reaches FORWARD_POINT, at which point any subsequent touch will cause it to change direction, moving instead towards BACKWARD_POINT where the same behaviour occurs but in reverse. This will result in a lever which can be moved between these two limits simply by touching it.

You should be wary when setting points when it comes to local and global co-ordinates. It is strongly recommended that you use only levers that are child-prims of something else, so that they may use local co-ordinates which are a lot easier to use. You can get the co-ordinates you require using the llGetLocalPos() function.

This script also contains other features, including the ability to have it reset after a period of time, and also have it contact other objects regarding changes, either using link-messages or by sending messages using whisper/say/shout/region-say. See the comments in the script below for COMMS_TYPE on how to enable these.

Script

<lsl>// ===================================================================== // Simple Touch Slide Lever by Haravikk Mistral // ===================================================================== // This script allows you to create a simple lever that when touched // will slide through a set of positions. It is best used when the lever // prim is a child of a root prim, as the slide is performed between two // points. // // This script can be used and modified freely, though I ask that you // give credit to myself where possible, and provide a link to my // wiki page at https://wiki.secondlife.com/wiki/User:Haravikk_Mistral // =====================================================================

// ===================================================================== // CONSTANTS // ===================================================================== // The following values can be used to quickly set-up a lever without // having to modify the script. Read the comments above each item to // get an idea of what to do.

// The number of lever positions from FORWARD_POINT to BACKWARD_POINT // (inclusive). Minimum POSITIONS is 2. integer POSITIONS = 3; // The starting position, positions are numbered from 1 to POSITIONS integer START_POSITION = 1; // The starting direction, TRUE will slide the lever forward from it's // starting position, FALSE will slide it backward first. When it // reaches it's limit it will change direction. integer START_DIRECTION = TRUE;

// The forward/end-point of the slide lever's movement vector FORWARD_POINT = <-0.15, 0.0, 0.0>; // The back/start-point of the slide lever's movement vector BACKWARD_POINT = <0.15, 0.0, 0.0>;

// A sound to play when moving the lever key SOUND = NULL_KEY; // The volume of the sound float SOUND_VOLUME = 1.0;

// This value determines how long the lever will wait before resetting // it's position back to START_POSITION and START_DIRECTION. Set this // to 0.0 if you never want to reset the lever. float RESET_TIME = 30.0;

// Determines what type of message this lever sends when its position // changes. The available types are: // -1 lever sends no communications (lever is a prop only) // 0 link-message (see LINK_* constants below to set-up) // 1 whisper (see CHANNEL constant) // 2 say (see CHANNEL constant) // 3 shout (see CHANNEL constant) // 4 region-say (see CHANNEL constant) // // Link messages will have the position number in their string parameter // and the key of the user who touched the lever in the key parameter. // All other message types will contain a message in the form: // <position>,<user> // That is; the position, and user-key are comma-separated. // NOTE: Any time the lever positions itself (reset) the key will be // NULL_KEY. integer COMMS_TYPE = -1;

// Determines what channel the lever whispers/says/shouts/region-says on integer CHANNEL = -1234567890;

// The link in a linked-set to send messages to upon changing position integer LINK_TARGET = LINK_ROOT; // The value to place in the integer parameter of a link-message for // easy filtering. integer LINK_FILTER = 8192;

// ===================================================================== // VARIABLES // ===================================================================== // Below here are variables, these are used by the script. integer position = 0; integer direction = START_DIRECTION;

float positionsF = 0.0;

moveToPosition(integer newPosition, key id) {

   if (newPosition > POSITIONS) newPosition = POSITIONS;
   else if (newPosition < 1) newPosition = 1;

   integer steps = newPosition - position;
   if (!steps) return; // Already there

   integer change = 1;
   if (steps < 0) {
       direction = FALSE;
       change = -1;
       steps = -steps;
   } else direction = TRUE;

   integer i = 0;
   vector diff = FORWARD_POINT - BACKWARD_POINT;

   do {
       position += change;
       llSetPos(BACKWARD_POINT + (
           diff * ((float)(position - 1) / positionsF))
       );
       
       if (SOUND) llPlaySound(SOUND, SOUND_VOLUME);
   } while ((++i) < steps);

   if (COMMS_TYPE >= 0) {
       if (COMMS_TYPE == 0)
           llMessageLinked(LINK_TARGET, LINK_FILTER, (string)position, id);
       else {
           string str = (string)position + "," + (string)id;

           if (COMMS_TYPE == 1) llWhisper(CHANNEL, str);
           else if (COMMS_TYPE == 2) llSay(CHANNEL, str);
           else if (COMMS_TYPE == 3) llShout(CHANNEL, str);
           else if (COMMS_TYPE == 4) llRegionSay(CHANNEL, str);
       }
   }

   if (RESET_TIME > 0.0) llSetTimerEvent(RESET_TIME);

}

default {

   state_entry() {
       positionsF = (float)(POSITIONS - 1);
       moveToPosition(START_POSITION, NULL_KEY);
   }

   touch_start(integer x) {
       x = position;
       if (direction) {
           if ((++x) > POSITIONS) 
               x = POSITIONS - 1;
       } else if ((--x) < 1) x = 2;

       moveToPosition(x, llDetectedKey(0));
   }

   timer() {
       llSetTimerEvent(0.0);
       moveToPosition(START_POSITION, NULL_KEY);
       direction = START_DIRECTION;
   }

}</lsl>