So unless you clear them, AS could be holding in to any global variables?
Absolutely. This gets me a lot. I often THINK something is working only to discover the crazy global variable still has data from an early version of the script. When I restart AS that global is gone and now my code isn't working properly. You really need to restart AS to clear that memory if you work with important global variables that need to hang around awhile.
IMPORTANT!
Most scripts start off as a function declaration similar to this:
Code: Select all
function LayerScript(moho)
... lots of code here
local bob = 6
... llots of code here
end
Any local variable declared within the "LayerScript()" function (bob for instance) is available ANYWHERE inside that function no matter how deep the structure goes. This is like a "mini" global. It's available throughout the script but is still a local variable. As long as you don't use another "local bob" again which would override that one.
Something like that?
Like that yes. It's best to create new "unique" variables to avoid the confusion. You can really get confused quickly if you use the same local variable names within nested loops or structures. Hard to keep track. 6 weeks down the road if you haven't commented the code you have no idea which variable is doing what.
One area where this happens is the "for" loop. It is used ALL THE TIME for going through the bone list (or layer list, or point list etc). The for loop can be used to check each bone or item one at a time and compare it to some value or another bone ID etc.
Code: Select all
local skel = moho:Skeleton() -- a variable we use to save on typing
local boneCount = skel:CountBones() -- saving on typing is fun!
for 1=0, boneCount-1 do -- do something to all the bones one at a time
.... do stuff here
end
But what if you need to compare each bone in the list to ALL the other bones in the list:
Code: Select all
local skel = moho:Skeleton()
local boneCount = skel:CountBones()
for 1=0, boneCount - 1 do -- check all the bones one at a time
for j = 0, boneCount - 1 do -- compare 1 bone (i) to all the other bones
if(something is true here) then
.... do something
end
end
end
In this case you have the for loop which has a "built in" local variable that we assign "i = 0" in this case. The second variable (boneCOunt) is how many times to do the for loop; the number of bones - 1 because we started at 0.
If there are 10 bones and we count from and include 0 (bone ids start at 0) then we count from 0 to 9, one less than the total number of bones to get 10. If we do 1 to 10 we miss a bone, bone ID 0.
The second internal loop again uses the "for" structure. We can't use "i" here because it refers to the "i" in the first part. We need another different local variable or else it won't work. So in this case we use "j" or we could have used "n" or "x". Whatever.
Keep in mind, once that loop reaches the "end" and moves out of the "for" loop, i and j no longer exist. If you need that info you would have to put it in a variable higher up in the scope.
Code: Select all
local theBoneID -- a variable to store a result from the loop below
local skel = moho:Skeleton()
local boneCount = skel:CountBones()
for 1=0, boneCount - 1 do -- check all the bones one at a time
for j = 0, boneCount - 1 do -- compare 1 bone to all the other bones
if(something is true here) then
theBoneID = i
end
end
end
Now we have the result of any matches during the for loop, to use anywhere within the scope that theBoneID was declared. If theBoneID is "nil" we know there was no match.
Phew! It seems so simple and straight forward to me now but I can still remember trying to wrap my brain around this. I still get confused every so often. Especially with really "obtuse" nested if and for loops.
-vern