Notifications
Clear all

[Closed] variable visibility issue

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
  )
  
1 Reply

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

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.