PDA

View Full Version : variable visibility issue


xenry
06-10-2008, 11:23 AM
hi people,
why this does not run ?
e.g. although i assign sth to g_rollStr before the rollout, it is undefined in there ?

(
struct aRollStruct(
rollW = 0,
rollH = 0,

fn init = (
rollW = 300
rollH = 200

print "i have been inited"
)
)

global g_rollStr = aRollStruct()
g_rollStr.init()

rollout aRoll "sample" (

local myWidth = g_rollStr.rollW
local myHeight = g_rollStr.rollH

label aLbl "i am a label"
)

createdialog aRoll g_rollStr.rollW g_rollStr.rollH
)

ZeBoxx2
06-10-2008, 01:09 PM
you're only thinking you're defining it before the rollout; you're not.

Your surrounding parentheses (I see them used a lot on CGTalk; I suppose it's to prevent "no local declarations at top level" error messages while testing) are placing everything in a local scope. You can't really define a rollout at a scope other than the top level (exceptions being custom attributes, scripted plugins, etc.). Rather than throwing an error, MaxScript just takes your rollout definition and evaluates it at the top level anyway, before evaluating the rest of your script.

That's my guess anyway :D

Changing it to:

globalVars.remove #g_rollStr; gc() -- this just to remove the global variable, for testing

(
global g_rollStr
struct aRollStruct(
rollW = 0,
rollH = 0,

fn init = (
rollW = 300
rollH = 200

print "i have been inited"
)
)

g_rollStr = aRollStruct()
g_rollStr.init()

)

rollout aRoll "sample" (
local myWidth = g_rollStr.rollW
local myHeight = g_rollStr.rollH
label aLbl "i am a label"
)
createdialog aRoll g_rollStr.rollW g_rollStr.rollH

... works, for example.

CGTalk Moderation
06-10-2008, 01:09 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.