Senuka Harbinger
A-Life, one bit at a time
Join date: 24 Oct 2005
Posts: 491
|
12-04-2005 19:53
the schoolpos seems to add up great inside of the for loop, but once I try calling the value of the vector after the math has been preformed inside the loop, it returns me the zero vector sensor(integer num_detected) { integer i; for (i = 0; i <= (num_detected - 1); i++) { vector schoolpos = schoolpos + llDetectedPos(i); //llOwnerSay((string) schoolpos + " " + (string) i + " " + (string) llDetectedPos(i)); } llOwnerSay((string)schoolpos); }
[edit] seems that if I declare the schoolpos as a vector inside the for loop the value of schoolpos is only kept while it's in the loop if I declare it as a vector globally and get rid of the vector statement from the loop it seems to work okay. I take it this is a standard way for variables to work in programming languages?
_____________________
My SLExchange shopTypos are forgiven; desecrating the english language with reckless abandon and necrophilic acts is not. The function is working perfectly fine. It's just not working the way you wanted it to work.
|
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
|
12-04-2005 20:11
Yes. The curly braces define the scope of a variable, which basically define its life. A global variable is one that's outside of any functions, and is therefore available to all functions. If you had: sensor(integer num_detected) { integer i; vector schoolpos; for (i = 0; i <= (num_detected - 1); i++) { schoolpos = schoolpos + llDetectedPos(i); //llOwnerSay((string) schoolpos + " " + (string) i + " " + (string) llDetectedPos(i)); } llOwnerSay((string)schoolpos); }
This would make the scope of the variable be the sensor event handler. You could have another schoolpos in a different function or event handler and it would be a completely different variable. If you'd typed: vector schoolpos;
default { ... sensor(integer num_detected) { integer i; for (i = 0; i <= (num_detected - 1); i++) { schoolpos = schoolpos + llDetectedPos(i); //llOwnerSay((string) schoolpos + " " + (string) i + " " + (string) llDetectedPos(i)); } llOwnerSay((string)schoolpos); }
Now it's a global, and visible to the whole script. It's interesting that you get back the zero vector. C++ wouldn't compile your original script, complaining that the variable schoolpos isn't defined for the llOwnerSay call. Which is correct - scholpos is only defined inside the for loop block, it doesn't exist outside. I guess the LSL compiler doesn't fully check for scope issues like this.
|
Zuleica Sartre
Registered User
Join date: 27 Sep 2005
Posts: 105
|
12-04-2005 20:54
Yes, declare it outside the default{} and it's global to the script. Declare it inside a state{} and it's local to that state only...leave that state and that variable is returned to the heap.
And, btw, you can simplify your for with; i<number_detected
|