Jump to content

Indexes, loops etc..


Reiramon
 Share

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

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

Recommended Posts

I'm sorry if this is a really dumb question but recently got back to scripting simple, but I cant grasp the whole idea about indexes, how to loop some part of script. 

How do I know where the index comes from? How do I know it's 0,1,2?

Does someone have an informative guide? 

Thanks. 

Link to comment
Share on other sites

29 minutes ago, Reiramon said:

How do I know where the index comes from? How do I know it's 0,1,2?

You created it, so you should know what it is called and what its value is.  An index is simply a counter.  If you have a collection of items, the index increases as you count them (1... 2....3....4....5....), just as if you were standing in front of them and counting on your fingers to figure out how many there are.

A loop is just a sequence of activities repeated over and over and over again until you reach a stopping point.  There are many ways to operate a loop (follow your nose through http://wiki.secondlife.com/wiki/Category:LSL_Flow_Control ), but they all amount to repeating the same things and keeping track of how many times you've done them by watching a counter.  Here's a really boring example ....

integer i;     // This is a counter, initially always given the default value of ZERO unless you said something else.
while ( i < 5 )      // As long as i is less than 5, do the following things....
{
     llSay(0, "My value is " + (string) i);     // Say the current value of the counter in chat
     ++i;     // Increase the value of the counter by 1
}     // Here's the end of the loop

When this loop is executed, it will run five times and will say, " My value is 1," "My value is 2," ... and so on until i = 5, at which point it will stop.  That's all.   A basic counting exercise.  You can count your way through anything you like: the number of objects in a collection, the number of people in a room, the number of values in a list .....  

  • Thanks 1
Link to comment
Share on other sites

if you wish to try cyclic loops, here are some ideas..

integer stage;
integer numStages = 5;
integer test = 1; // change this ( 1,2,or 3 )
default
{
    state_entry()
    {      
    }   
    touch_start(integer total_number)
    {
        // cycle thru stages( 0-4 ), ... to get 1-5, add +1 to the end of your calculations.
        if( test == 1) // 0 to 4
        {  stage = ++stage % numStages;
           llOwnerSay("stage: " + (string)stage);
        } 
        if( test == 2) // 0 thru -4
        {  stage = --stage % -numStages;         
           llOwnerSay("stage: " + (string)stage);
        }  
        if( test == 3) // 4 backwards to 0
        {  stage = --stage % numStages;
           if (stage < 0) { stage = numStages - 1; }
           llOwnerSay("stage: " + (string)stage);
        }   
    }   
}

 

  • Thanks 1
Link to comment
Share on other sites

On 9/21/2020 at 4:35 AM, Reiramon said:

I'm sorry if this is a really dumb question but recently got back to scripting simple, but I cant grasp the whole idea about indexes, how to loop some part of script. 

How do I know where the index comes from? How do I know it's 0,1,2?

Does someone have an informative guide? 

Thanks. 

It's hard to give specific advice if we don't know what exactly you're trying to do. Rolig linked a page that lists all of the different ways you can control when a script does something.

 

The simplest loop is the while loop.

while ( condition )
{
    do something
}

 

So what even is a "condition?" It's some expression that can be either "true" or "false." That might open more questions, so here's a practical example:

integer count = 0;

while ( count < 3 )
{
    do something
}

Here, the condition is determined by the value of count and whether or not it's less than 3.
"While count is less than 3, do something."

 

In this case, you could think of count as your index. We could do...

integer count = 0;

while ( count < 3)
{
    llOwnerSay( "index: " + (string)count );
    ++count;
}

Here, we're using llOwnerSay, typecasting the integer value to a string, combining two strings together, and incrementing count by one.

  1. The variable count is created, and given the value 0.
     
  2. The condition is checked. count is less than 3. The condition is true, the condition passes and we enter the while-loop.
  3. The script will say "index: 0"
  4. count is incremented by one. count is now 1.
     
  5. The condition is checked again. 1 is less than 3. The condition is true and we will re-enter the loop.
  6. The script will say "index: 1"
  7. count is incremented by one. count is now 2.
     
  8. The condition is checked again. 2 is less than 3. The condition is true and we will re-enter the loop.
  9. The script will say "index: 2"
  10. count is incremented by one. count is now 3.
     
  11. The condition is checked again. 3 is not less than 3. The condition is false and we will skip the loop. (the curly-braces)
  12. If there is no code after the while-loop, nothing more will happen. The script becomes idle until something triggers an event.

If you made the very common mistake of not changing the value of count, you would create an infinite loop. Generally speaking, infinite loops are bad because nothing can stop them, besides restarting the script. The loop will continue working forever without letting the script do anything else. It might even cause the script to crash if it's using too much script-memory.

Edited by Wulfie Reanimator
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

19 minutes ago, Kyrah Abattoir said:

<Nevermind it's 2:30 am and I'm tired>

In case this was it was intented at me. 

No, I honestly couldn't understand it and the answers helped, saved the replies in case something happens to the thread even,so I am thankful for them. 

I didn't have a particular issue in mind when I asked, I simply didn't understand what the scripting wiki was telling me. 

If you were trying to provide some more information, then ignore this comment :P I can't tell. 

Edited by Reiramon
Link to comment
Share on other sites

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