Jump to content

For statement


Kethri
 Share

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

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

Recommended Posts

Hi,

Can someone explain to me why a for statement does not work like this?

Thanks

ScriptThisPage()
{
    for (ForIndex = min; ForIndex <= max; ++ForIndex && ++ButtonIndex)
    {
        
        string item =  llGetInventoryName(INVENTORY_ALL, ForIndex);
        if (item != ME)
        {
            integer ButtonIndex = ButtonIndex+1;
            MESSAGE += (string)ButtonIndex+" "+item+"\n";
            BUTTONS += (string)ButtonIndex;
        }
        else
        {
            --ButtonIndex;
        }
    }
}

 

Link to comment
Share on other sites

[EDIT: Can't really tell how many times it should execute without knowing the values of min and max.]

There is a likely bug in the handling of the ButtonIndex variable. It's evidently global, but it's also being declared as a local (integer) within the scope of the if-block, so that will be a different ButtonIndex than the one in the larger scope.

It's pretty whacked anyway, given that (the global) ButtonIndex is incremented in the for statement then decremented in the else condition, whereas the if condition does a bunch of stuff with that local variable of the same name -- including incrementing it again.

Link to comment
Share on other sites

Well this runs fine:

 

 

integer min = 0;integer ForIndex;integer max = 10;integer ButtonIndex;ScriptThisPage(){    for (ForIndex = min; ForIndex <= max; ++ForIndex && ++ButtonIndex)    {        llOwnerSay("Hello " + (string)ForIndex);           }}default{    state_entry()    {           }    touch_start(integer total_number)    {        llSay(0, "Touched.");        ScriptThisPage();    }}

 

so just build it up bit at a time and work out where your logic is in error.

 

 

Link to comment
Share on other sites

Here's what I can see:

Line 3:

* "ForIndex", "min", "max", "ButtonIndex" have all not been declared in this scope.  This will not compile.

* The "&&" logical comparison serves no functionality in the increment.  It may compile (I haven't checked) but it appears to be an unreasonable expense of resources in order to increment two numbers.

** Reference to Line 15 * Why increment a number just to conditionally decrement it again?  Why not just increment it when needed?

Line 7:

* "ME" has not been declared or defined in this scope.  This will not compile.

Line 9:

* An attempt to declare "ButtonIndex" and define it with the value of "ButtonIndex+1" will not compile for multiple reasons.  In plain English, it's reading like "Create a value which doesn't exist yet and define it with a value which still doesn't exist incremented by an integer value of 1."

Line 10:

* "REFERENCE" has not been declared in this scope.  This will not compile.

Line 11:

* "BUTTONS" has not been declared in this scope.  This will not compile.

Line 15:

* "ButtonIndex" has not been declared in this scope.  This will not compile.

 

Here's the approach I would take.

ScriptThisPage(){    integer min = *something small*;    integer max = *something bigger than min*;    integer ForIndex = 0;    integer ButtonIndex = 0;    list MESSAGE;    list BUTTONS;    string ME = llGetScriptName();    for (ForIndex = min; ForIndex <= max; ForIndex++)    {                string item =  llGetInventoryName(INVENTORY_ALL, ForIndex);        if (item != ME)        {            MESSAGE += (string)ButtonIndex+" "+item+"\n";            BUTTONS += (string)ButtonIndex;            ButtonIndex++;        }    }}

(^_^)

Link to comment
Share on other sites

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