[Closed] fn compile error
Hey guys,
This may be a simple issue but I don’t understand it.
I wrote a recursive function based off this thread that goes through a selection, looks for named list controllers, and sets them active.
The function works but only if I evaluate the the function inside the function first, and then evaluate the entire function as a whole. Can anyone explain why that is and how I can avoid it?
/*
Example Usage:
Select objects and run:
setActiveList "animation" -- sets all list controls named 'animation' to active
setActiveList "zero" -- sets all list controls named 'zero' to active
*/
fn setActiveList keyWord =
(
fn RecurseSubAnims o active =
(
-- do something
listTypes = #(float_list, position_list, rotation_list, scale_list)
for j = 1 to listTypes.count do
(
if (isKindOf o.controller listTypes[j]) == true do
(
for k = 1 to o.controller.count where (o.controller.getName k) == active do o.controller.setActive k
)
)-- end j loop
-- recurse
for i = 1 to o.numsubs do RecurseSubAnims o[i] active
) -- end RecurseSubAnims
for h in selection do
(
RecurseSubAnims h keyWord
)-- end h loop
) --end fn
If I try and evaluate the above code I get this error:
-- Compile error: No outer local variable references permitted here: RecurseSubAnims
-- In line: for i = 1 to o.numsubs do RecurseSubAnims o
But if I evaluate RecurseSubAnims separately I get no errors. Once that’s defined I can evaluate the entire thing with no errors.
How do I need to write the code so that I can define the entire thing in one pass?
Thanks for any help!
But why? And why does it work when I execute them separately? Is it because the recursive function is global?
What would be the correct way to get the same result? Sorry, I’m just trying to get a better understanding. Thanks!
Your assumption is correct, it works because you define it globally. Nested functions have the limitation that they can not access the local scope of the enclosing function. The nested function itself, however, is a local value of the enclosing function, so it can not call itself.
You can ibstead define both functions in a struct, or in an implicit local scope.