Jump to content

sliding door script- adding auto close


Annabell Wandsworth
 Share

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

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

Recommended Posts

personally i never ever use states, but...

a graphical look at states would be kinda like ...

DEFAULT*STATE
{
    default*state ....state_entry()
    {   //Operations that will be executed when the script restarts or when execution enters this state
    }

    default*state ....touch_start(integer num)
    {   // Operations that will be triggered if someone left-clicks on yhe object
        state SECONDARY*STATE ;  // go to the next state
    }
}  // end of DEFAULT*STATE

SECONDARY*STATE
{
    secondary*state ....state_entry()
    {   //Operations that will be executed when the script restarts or when execution enters this state
    }

    secondary*state ....touch_start(integer num)
    {   // Operations that will be triggered if someone left-clicks on yhe object
        state DEFAULT*STATE;  // go back to  the first state
    }
}  // end of SECONDARY*STATE

Link to comment
Share on other sites

Each state is a self-contained separate set of events. They all share the same global variables (defined above the states). “Default” state runs when a script starts or resets; you switch to another state using the syntax “state my_state;”. This causes state_entry() to run in the new state. The main reason you would have different states in 1 script is if you want the events to do something different (or more events or fewer events) in the different states.

Link to comment
Share on other sites

No..... state_entry is an event.  Specifically, it is the event that the script executes as it enters the state.  So, if you start the script (or restart it), all of the statements in the state_entry event are executed immediately.  It's your "Start here" set of instructions.  Like any other event, it's only necessary if you actually have things that the script must do as it enters the state, just as you only need a touch_start event if you expect the script to need to respond to being touched.

Link to comment
Share on other sites

3 hours ago, Rolig Loon said:

Yes. Study the structure of LSL.  A script must have at least one state, called default, which is a container for the events in which all the work is done. An event, like a timer or a touch_start,  contains statements that will all be executed in response to a specific trigger in the environment. The timer event is a block of commands triggered by the passage of time ... the time that you set with the llSetTimerEvent statement. So, that event that Love wrote out for you is an independent block within state default, just like the touch_start event.

I tried putting it there and got the error "Name not defined within scope"

Link to comment
Share on other sites

1 minute ago, Rolig Loon said:

No..... state_entry is an event.  Specifically, it is the event that the script executes as it enters the state.  So, if you start the script (or restart it), all of the statements in the state_entry event are executed immediately.  It's your "Start here" set of instructions.  Like any other event, it's only necessary if you actually have things that the script must do as it enters the state, just as you only need a touch_start event if you expect the script to need to respond to being touched.

wow ok so state_entry is not a state :-)

 

head is reeling

Edited by Annabell Wandsworth
Link to comment
Share on other sites

4 hours ago, Love Zhaoying said:

Since timer() is an event, it goes inside the state. Put it somewhere under “state default {“ - those other functions inside the state are events, too. You could put it at the very end right before the last closing “}”.

Sorry used the wrong quote. This is the right one and here is the code:

timer() {

  llSetTimerEvent(0.0);

  OpenClose();

}

Link to comment
Share on other sites

3 minutes ago, Annabell Wandsworth said:

Sorry used the wrong quote. This is the right one and here is the code:

timer() {

  llSetTimerEvent(0.0);

  OpenClose();

}

Aha.  There's nothing wrong with that except that your user-defined function is not called OpenClose.  It's openclose.  Capitalization and punctuation count.  :)

  • Like 1
Link to comment
Share on other sites

init() and openclose() are user-defined functions.  That is, they are globally-defined functions that may be called from ANY event ANYWHERE in the script.  That's why they are outside any state.  The timer event, on the other hand, is an event.  Like all events, it MUST be inside a state.  In this case, that means inside the default state.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

OK, now that you have been playing with this script for a while, let me show you what Love and I have been talking about.  Take a good look at the comments, especially at the one thing that we haven't talked about.  9_9   If you've been following along, you should have created something like this yourself by now....
 

integer isOpen = FALSE;
vector originalPos;
float slideBy = 1.5;
float fTimeToMove = 5.0;    // Here's the time to remain open
integer iTouched;           // Here's a flag to tell the script to ignore touches while the door is open

init()
{
    originalPos = llGetLocalPos();
    vector size = llGetScale();
    // Set up the amount to move by. Use size.y instead of size.x
    // if you want to slide along the y-axis.
    slideBy = size.x - 0.2;
}
openclose()
{
  if (isOpen)
  {
    llSetPos(originalPos);
    isOpen = FALSE;
  }
  else
  {
    llSetPos(<originalPos.x + slideBy, originalPos.y, originalPos.z>);
    // To move along the y-axis, use this instead:
    // llSetPos();
    isOpen = TRUE;
  }
}
default
{
    state_entry()
    {
        init();     // Do initialization stuff if the script resets
    }
    
    on_rez(integer param)
    {
        init();     // Do initialization stuff when the ob ject containing this script is rezzed
    }
    
    touch_start(integer total_number)
    {
        if (!iTouched)  // If nobody has touched the door ....
        {
            iTouched = TRUE;    // OK, somebody HAS touched it now
            openclose();        // Open the door
            llSetTimerEvent(fTimeToMove);   // Set the time to shut it in fTimeToMove seconds
        }
    }
    
    changed(integer change)
    {
        // When the links change, reset the script
        // so that we pick up the changes.
        if (change & CHANGED_LINK)
        {
            llResetScript();
        }
    }
    
    timer()
    {
        llSetTimerEvent(0.0);   //  Turn off the time
        openclose();            // Close the door
        iTouched = FALSE;       // And make it OK for someone else to touch
    }
}

 

Link to comment
Share on other sites

21 hours ago, Rolig Loon said:

OK, now that you have been playing with this script for a while, let me show you what Love and I have been talking about.  Take a good look at the comments, especially at the one thing that we haven't talked about.  9_9   If you've been following along, you should have created something like this yourself by now....
 


integer isOpen = FALSE;
vector originalPos;
float slideBy = 1.5;
float fTimeToMove = 5.0;    // Here's the time to remain open
integer iTouched;           // Here's a flag to tell the script to ignore touches while the door is open

init()
{
    originalPos = llGetLocalPos();
    vector size = llGetScale();
    // Set up the amount to move by. Use size.y instead of size.x
    // if you want to slide along the y-axis.
    slideBy = size.x - 0.2;
}
openclose()
{
  if (isOpen)
  {
    llSetPos(originalPos);
    isOpen = FALSE;
  }
  else
  {
    llSetPos(<originalPos.x + slideBy, originalPos.y, originalPos.z>);
    // To move along the y-axis, use this instead:
    // llSetPos();
    isOpen = TRUE;
  }
}
default
{
    state_entry()
    {
        init();     // Do initialization stuff if the script resets
    }
    
    on_rez(integer param)
    {
        init();     // Do initialization stuff when the ob ject containing this script is rezzed
    }
    
    touch_start(integer total_number)
    {
        if (!iTouched)  // If nobody has touched the door ....
        {
            iTouched = TRUE;    // OK, somebody HAS touched it now
            openclose();        // Open the door
            llSetTimerEvent(fTimeToMove);   // Set the time to shut it in fTimeToMove seconds
        }
    }
    
    changed(integer change)
    {
        // When the links change, reset the script
        // so that we pick up the changes.
        if (change & CHANGED_LINK)
        {
            llResetScript();
        }
    }
    
    timer()
    {
        llSetTimerEvent(0.0);   //  Turn off the time
        openclose();            // Close the door
        iTouched = FALSE;       // And make it OK for someone else to touch
    }
}

 

You're right- I definitely will start adding my own comments to keep track of syntax.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2259 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...