PDA

View Full Version : problem understanding scope with function


Caprier
03-31-2009, 08:57 AM
I have a both a variable and a function defined in the same block, in that order, and the function calls the variable.


rollout test "test" height:30 width:60
(
button testIt "T"

on testIt pressed do (
local a=123
fn testF = a
testF
)
createDialog test


I get a compile error: No outer local variable references permitted here
in line: fn testF = a

I've been through the section in maxscript about variable scope several time but I still don't understand what is happening.
Can someone explain this to me, please?

LoneRobot
03-31-2009, 09:31 AM
Hi Pat,

im not 100% either to be fair, Maybe you could explain a little more as to why you are structuring it this way. (I guess you know you missed the last bracket off your posted code)

here are the two options I looked at -


rollout test "test" height:30 width:60
(
local a
button testIt "T"

on testIt pressed do
(
a=123
fn testF = a
print (testF())
)

)
createDialog test

rollout test "test" height:30 width:60
(

button testIt "T"

on testIt pressed do
(
local a=123
fn testF a = a
print (testF a)
)

)
createDialog test

I noticed you define the function in your code but you don't actually call it. So it seems that the function itself isn't registering the call to retrieve the variable 'a'. Not sure this is really shedding any more light on the problem, so anyone, Help?!

SyncViewS
03-31-2009, 10:58 AM
Hi guys,
my shoot is: you cannot define a function inside an event handler block, and it's not a scope problem, even though the error says so.


rollout test "test" height:30 width:60
(
button testIt "T"

fn testF a = a

on testIt pressed do
(
local a = 123
print (testF a)
)
)
createDialog test

-- output: 123

This code works, puts the function equal to the value of 'a', which must be passed as argument to the function, since the 'a' variable exists in the event handler block only, and cannot be seen by the function in the rollout. How far from the bull's eye? :)

- Enrico

Caprier
03-31-2009, 05:27 PM
Thanks for the answers, guys (and sorry about the missing brackets, copy/paste was a mess and I retyped it in a hurry. My bad.)

In my real code, the function is passed an object, uses a plane (among other things) to perform its task and returns the modified object.
As it is always the same plane (and other things) that is used, it seems rather wasteful to pass it to the function each time it's called. Which can be several thousand times in my script.
Declaring the plane at the rollout level works fine but I don't want that plane to exist before the button pressed event.

As the function is called from a single place, it's not really needed and the same code in a loop would do just fine. So I'm not stuck in my script trying to find a way around. It's more about understanding what is happening when the compile error is triggered. Under the hood, so to speak.

CGTalk Moderation
03-31-2009, 05:27 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.