Jump to content

return object to original position


SweetFire Macpherson
 Share

You are about to reply to a thread that has been inactive for 3285 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

script is for a reciprocating saw blade.  Press the button and the saw blade moves along the Z axis back and forth. The scripts are placed in two separate objects that are unlinked. Having them unlinked may not be elegant but so far the result of linking them has been problematic. What I am trying to add is when the button is pressed to turn it off I want the saw blade to return to the original position.
     
    This is the script I'm using for the button

    integer COG_CHAN01 = -50001;
    integer ON_OFF;
    string MSG_OPTS;
    float SOUND_VOL = 1;
    default
    {
     
        touch_start(integer num_detected)
        {
            if (ON_OFF == 0)
            {
                llSleep(0.5);
             
                MSG_OPTS = "Pump";
                llSay(0, "Power On");
                llSay(COG_CHAN01,MSG_OPTS);
                ON_OFF = 1;
            }
            else
            {
                llSleep(0.5);
             
                MSG_OPTS = "Stop";
                llSay(0, "Power Off");
                llSay(COG_CHAN01,MSG_OPTS);
                ON_OFF = 0;
             
                }
        }
    }
     
     
     
     
     
    This what I have for the saw blade...

    integer LISTEN_CHANNEL = -50001;
    vector pos;
    integer SWITCH;
    integer OFFSET = 1;
    default
    {
         state_entry()
         {
             pos = llGetLocalPos();
             llListen(LISTEN_CHANNEL,"","","");
         }
         listen(integer channel, string  name, key id, string message)
         {
            llSetTimerEvent( 0.3 * ( SWITCH = !SWITCH ) ); // Alternate SWITCH from 0 to 1
         }
         timer()
         {
            pos.x += (0.3 * ( OFFSET *= -1 )); //Alternate OFFSET from 1 to -1
            llSetPos(pos);
     
         }
    }
     
     
I think its as simple as identifying a second object position in the saw script and having some sort of IF statement regarding switch position. Unfortunately I have no idea how to do that given what I already have. Any help appreciated.

Link to comment
Share on other sites

You really can do it all in one script if you link the saw blade and the button and use llSetLinkPrimitiveParamsFast  with lPRIM_POS_LOCAL to reset the saw blade's position.  Whether you do that or not, though, one easy way to solve the reciprocating challenge is to save the blade's original position in state_entry as a global vector, gPos, and then define a local variable Pos in each time step with

vector Pos = <0.3 * (OFFSET = !OFFSET) + gPos.x, gPos.y, gPos.z>;

So you're either adding 0.3 to the original position to create Pos, or you're not adding it. Then set Pos.

Link to comment
Share on other sites

Oh.  I was just making the distincttion between a global variable -- in this case, your starting position -- and a variable that only has a value within the scope of your timer event.  You can call either one of them anything you like.  Many (most?) LSL scripters keep track of global variables by putting a little "g" in front of them as a reminder.  Hence, gPos and Pos.

So, you have a perfectly good, if not overly efficient script already.  Just modify it as I suggested before....

 integer LISTEN_CHANNEL = -50001;    vector gPos;    // Here's your global position vector    integer SWITCH;    integer OFFSET = 1;    default    {         state_entry()         {             gPos = llGetLocalPos();   // So grab the "home" position when the script starts             llListen(LISTEN_CHANNEL,"","","");         }         listen(integer channel, string  name, key id, string message)         {            llSetTimerEvent( 0.3 * ( SWITCH = !SWITCH ) ); // Alternate SWITCH from 0 to 1         }         timer()         {            // OFFSET will alternate from zero to +1            vector Pos = <0.3 * (OFFSET = !OFFSET) + gPos.x, gPos.y, gPos.z>;            llSetPos(Pos);         }    }
Link to comment
Share on other sites

This is a script for in a linkset. 

// Put this script in the root prim of the linkset. // Name the moving prim in the linkset "saw"// Name the start-stop prim in the linkset "button"// Do NOT set the 'saw' as the root prim.integer ON_OFF;integer OFFSET;integer SawLinkNum;vector StartPos;vector pos;default {    state_entry() {        integer i;        while(++i <= llGetNumberOfPrims()) {            if (llToLower(llStringTrim(llGetLinkName( i), STRING_TRIM)) == "saw" ) SawLinkNum = i;        }     }         touch_start(integer num_detected) {        if (llToLower(llStringTrim(llGetLinkName(llDetectedLinkNumber(0)), STRING_TRIM)) == "button") {            if (ON_OFF = !ON_OFF) {                StartPos = llList2Vector(llGetLinkPrimitiveParams(SawLinkNum, [PRIM_POS_LOCAL]),0);                pos = StartPos;                OFFSET = 1;                llSay(0, "Power On");                llSetTimerEvent(0.5);            }            else {                llSay(0, "Power Off");                llSetTimerEvent(0.0);                llSetLinkPrimitiveParamsFast(SawLinkNum, [PRIM_POS_LOCAL, StartPos]);            }        }    }        timer() {        llSetTimerEvent(0.3);        pos.x += (0.3 * ( OFFSET *= -1 ));        llSetLinkPrimitiveParamsFast(SawLinkNum, [PRIM_POS_LOCAL, pos]);    }}
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3285 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...