Kethri Posted September 14, 2014 Share Posted September 14, 2014 Hi,Can someone explain to me why a for statement does not work like this?ThanksScriptThisPage() { 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 More sharing options...
Ohjiro Watanabe Posted September 14, 2014 Share Posted September 14, 2014 I think that the for should work fine. What is the problem you are having, i.e. what error message. Link to comment Share on other sites More sharing options...
Kethri Posted September 14, 2014 Author Share Posted September 14, 2014 It runs the statement only once instead of repeating it multiple times as it should. Link to comment Share on other sites More sharing options...
Qie Niangao Posted September 14, 2014 Share Posted September 14, 2014 [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 More sharing options...
Ohjiro Watanabe Posted September 14, 2014 Share Posted September 14, 2014 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 More sharing options...
Kethri Posted September 14, 2014 Author Share Posted September 14, 2014 Ok, thank you both for the quick response. Link to comment Share on other sites More sharing options...
Imnotgoing Sideways Posted September 14, 2014 Share Posted September 14, 2014 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 More sharing options...
Nova Convair Posted September 15, 2014 Share Posted September 15, 2014 One detail - instead of: for (ForIndex = min; ForIndex <= max; ++ForIndex && ++ButtonIndex) you can use: for (ForIndex = min; ForIndex <= max; ++ForIndex, ++ButtonIndex) it's valid and no unnecessary and confusing boolean operation Link to comment Share on other sites More sharing options...
Recommended Posts
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