Jump to content
Robby Falworth

Looking help for modify the Open/close script Please

Recommended Posts

 

Hello everyone,

I have 2 questions

My first :

Here is the script for opening and closing a door. However, the sound of the opening starts when you click and the closing sound once the door is closed. I would like the closing sound to start during the closing movement and not once closed.

My second :

Do you think I can add a script in a second prim linked so that the two objects follow the same movement?

Can you help me please ?

/*
 * Smooth Rotating Linked Door With Hinge
 *
 * By: Lyn Mimistrobell
 * Version: 1.1
 * License: Do whatever you like with it, just don't blame me if you break it :)
 */
 
/*
 * Define the rotation in degrees, using the door prim's local coordinate
 * system
 */
vector      ROTATION            = <0.0, 0.0, 180.0>;
 
/*
 * Define the position of the virtual hinge; usually this is half the door
 * prim's width and thickness
 */
vector      HINGE_POSITION      = <-0.4, 1.3, 0.0>;
 
/*
 * Define how fast the door opens, in seconds
 */
float       SECONDS_TO_ROTATE   = 1.0;
 
/*
 * Define after how much time the door should close automatically, in seconds;
 * set to 0.0 to disable autolmatic closing
 */
float       AUTO_CLOSE_TIME     = 10.0;
 
/*
 * Define a sound that plays when the door starts to open; set to NULL_KEY
 * for no sound.
 */
key         SOUND_ON_OPEN       = "b814a289-a786-ef81-e5df-e0e9b9aeebab";
 
/*
 * Define a sound that plays when the door has closed; set to NULL_KEY
 * for no sound.
 */
key         SOUND_ON_CLOSE      = "00429879-654d-7925-9e66-5c1695324e58";
 
/*
 * Define the volume of the opening and closing sounds
 */
float       SOUND_VOLUME        = 1.0;
 
/*
 * NORMALLY, THERE IS NO NEED TO CHANGE ANYTHING BELOW THIS COMMENT. IF YOU DO
 * YOU RISK BREAKING IT.
 */
 
integer     gClosed;            // Door state: TRUE = closed, FALSE = opened
rotation    gRotationClosed;    // Initial rotation of the door (closed)
vector      gPositionClosed;    // Initial position of the door (closed)
vector      gRotationPerSecond; // The amount to rotate each second
 
doOpenOrClose() {
    /*
     * Only perform the rotation if the door isn't root or unlinked
     */
    integer linkNumber = llGetLinkNumber();
    if (linkNumber < 2)
        return;
 
    if (gClosed) {
        /*
         * Store the initial rotation and position so we can return to it.
         *
         * Rotating back purely by calculations can in the longer term cause the door
         * to be positioned incorrectly because of precision errors
         *
         * We determine this everytime before the door is being opened in case it was
         * moved, assuming the door was closed whilst being manipulated.
         */
        gPositionClosed = llGetLocalPos();
        gRotationClosed = llGetLocalRot();
 
        /*
         * Play the opening sound and preload the closing sound
         */
        if (SOUND_ON_OPEN)
            llPlaySound(SOUND_ON_OPEN, SOUND_VOLUME);
    }
 
    vector hingePosition = gPositionClosed + HINGE_POSITION * gRotationClosed;
 
    /*
     * Reset the timer and start moving
     */
    llResetTime();
    while (llGetTime() < SECONDS_TO_ROTATE) {
        float time = llGetTime();
        if (! gClosed)
            /*
             * Invert the timer for closing direction
             */
            time = SECONDS_TO_ROTATE - time;
 
        rotation rotationThisStep = llEuler2Rot(gRotationPerSecond * time) * gRotationClosed;
        vector positionThisStep = hingePosition - HINGE_POSITION * rotationThisStep;
        llSetLinkPrimitiveParamsFast(linkNumber, [PRIM_ROT_LOCAL, rotationThisStep, PRIM_POS_LOCAL, positionThisStep]);
    }
 
    /*
     * Set the new state
     */
    gClosed = !gClosed;
 
    if (gClosed) {
        /*
         * Finalize the closing movement
         */
        llSetLinkPrimitiveParamsFast(linkNumber, [PRIM_ROT_LOCAL, gRotationClosed, PRIM_POS_LOCAL, gPositionClosed]);
 
        /*
         * Play the closing sound and preload the opening sound
         */
       if (SOUND_ON_CLOSE)
            llPlaySound(SOUND_ON_CLOSE, SOUND_VOLUME);
        if (SOUND_ON_OPEN)
            llPreloadSound(SOUND_ON_OPEN);
    } else {
        /*
         * Finalize the opening movement
         */
        rotation rotationOpened = llEuler2Rot(ROTATION * DEG_TO_RAD) * gRotationClosed;
        vector positionOpened = hingePosition - HINGE_POSITION * rotationOpened;
        llSetLinkPrimitiveParamsFast(linkNumber, [PRIM_ROT_LOCAL, rotationOpened, PRIM_POS_LOCAL, positionOpened]);
 
        /*
         * Preload the closing sound
         */
        if (SOUND_ON_CLOSE)
            llPreloadSound(SOUND_ON_CLOSE);
 
        /*
         * Set a timer to automatically close
         */
        llSetTimerEvent(AUTO_CLOSE_TIME);
    }
}
 
default {
    state_entry() {
        /*
         * Assume the door is closed when the script is reset
         */
        gClosed = TRUE;
 
        /*
         * These doesn't change unless the script is changed, calculate them once
         */
        gRotationPerSecond = (ROTATION * DEG_TO_RAD / SECONDS_TO_ROTATE);
 
        /*
         * Preload the opening sound
         */
        if (SOUND_ON_OPEN)
            llPreloadSound(SOUND_ON_OPEN);
        
    }
    touch_start(integer agentCount) {
        doOpenOrClose();
    }
    timer() {
        llSetTimerEvent(0.0);
 
        /*
         * Close the door if it isn't already closed
         */
        if (! gClosed)
            doOpenOrClose();
    }
}

 

Share this post


Link to post
Share on other sites

Trigger the sound when clicked, but delay the opening for a half second or more, so that the door is moving at the right time.   Also, adjust the timing of the sound in Audacity before you upload it, so that the duration of the sound matches the time it takes to open/close the door.

Share this post


Link to post
Share on other sites
1 hour ago, Rolig Loon said:

Trigger the sound when clicked, but delay the opening for a half second or more, so that the door is moving at the right time.   Also, adjust the timing of the sound in Audacity before you upload it, so that the duration of the sound matches the time it takes to open/close the door.

Increase the sound level as well. When the sound is played in SL it is half the level in Audacity. Which i assume is due to compression.

Share this post


Link to post
Share on other sites
13 hours ago, Robby Falworth said:

My second :


Do you think I can add a script in a second prim linked so that the two objects follow the same movement?

Yes fairly simple. Just modify the script so that in the state_entry it listens to a particular channel., an d on the touch whisper so that the paired door starts as well:

//Comms channel .. set to unique value for each pair set
integer PAIR_CH = -12345;

default
{
    state_entry()
    {
        //filtering for single message
        llListen(PAIR_CH, "", NULL_KEY, "touched");
    }
    touch_start(integer total_number)
    {
        llWhisper(PAIR_CH, "touched");
        doOpenOrClose();
    }
    listen(integer channel, string name, key id, string message)
    {
        doOpenOrClose();
    }
}

 

Share this post


Link to post
Share on other sites
7 hours ago, Wandering Soulstar said:
20 hours ago, Robby Falworth said:


Do you think I can add a script in a second prim linked so that the two objects follow the same movement?

Yes fairly simple. Just modify the script so that in the state_entry it listens to a particular channel., an d on the touch whisper so that the paired door starts as well:

If the two doors are linked in the house structure, you'll want to use link message (  llMessageLinked and link_message events ) instead of chatting and using listen events, but the idea is the same.  Or, just write a single script that activates both doors when either one is touched.  Put that script in the root prim of the house.

Share this post


Link to post
Share on other sites
2 minutes ago, Rolig Loon said:

If the two doors are linked in the house structure, you'll want to use link message

Good catch @Rolig Loon I missed the 'linked' reference in the OPs statement (and did not look into the routine for moving the door where it would have become very clear)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...