PDA

View Full Version : help me ,simple script!


YY3010966
10-16-2007, 04:47 PM
hi all ,im a MaxScript beginner ,there is a short script puzzled me ,here

global foo=23,x=20
y=10
format "context level global :foo=%\n" foo
if x>y then
( local baz=foo+1
local foo=y-1
format "context level 1:foo=%\n" foo
b=box()
b.pos.x=foo
if (foo>0) then
(
local a
local foo=y-x
format "context level 2:foo=%\n" foo
a=sin foo
format "a=%\n" a
)
format "context level 1:foo=%\n" foo
)
format "context level global:foo=%\n" foo

result is :
20
10
context level global :foo=23
OK
context level 1:foo=9
context level 2:foo=-10
a=-0.173648
context level 1:foo=-10
OK
context level global:foo=-10
OK
OK

but i think the result should be


20
10
context level global :foo=23
OK
context level 1:foo=9
context level 2:foo=-10
a=-0.173648
context level 1:foo=9
OK
context level global:foo=23
OK
OK

i dont konw why......... :sad: waiting someone kind will light me ,thanks!!

Bobo
10-16-2007, 09:52 PM
Surprisingly (!;)), MAXScript is right.

IF statements do NOT define local scopes. The first IF does though, because a local scope is defined by the outer-most parentheses inside a global scope.

From the MAXScript Reference:

The initial MAXScript scope context is global, and new scope contexts are opened for the following:




Top-level open parentheses in a script file or in the Listener


Start of a function body


Start of a for loop body


Start of a utility, rollout, right-click menu, Macro Script, or tool definition


Start of a rollout, utility, right-click menu, Macro Script, or tool event handler


Start of a when body




As you can see, your code contains only ONE local scope, the one of the outer IF. The inner IF does NOT define a new scope, so the foo you declare as local simply overrides the already existing local foo.

Hope this helps...

YY3010966
10-17-2007, 02:04 PM
wow!!you are Really A BIG SHOT!!
now,i understant it . sorry for wasting you time to reply so easy question

and thank you so much !

(next time ,hope you can reply me again!!:) )

Joel Hooks
10-17-2007, 03:43 PM
wow!!you are Really A BIG SHOT!!
now,i understant it . sorry for wasting you time to reply so easy question

and thank you so much !

(next time ,hope you can reply me again!!:) )

I'd argue that scope issues in MXS are not an easy topic. :banghead: :sad:

CGTalk Moderation
10-17-2007, 03:43 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.