Announcement

Collapse
No announcement yet.

MAxscript - Access Rollout UI in a function?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • MAxscript - Access Rollout UI in a function?

    Without setting it to global, is there a way for example I can press a button, call a function, and in the function I change a lable on my UI of my script?
    Kind Regards,
    Morne

  • #2
    You can:
    Code:
    try(destroydialog test1);catch()
    
    rollout test1 "Group Test" width:165 height:85
    (
    	--variable declaration
    	local variable1 = 10
    	local variable2 = "Variable declared in the rollout"
    	
    	--function declaration
    	fn changeText txt =
    	(
    		test1.lb1.text = txt
    	)
    	
    	--ui controls
    	label lb1 "Text" pos:[20,5]
    	button bt1 "New Text" pos:[20,25] width:125 height:25
    	button bt2 "More Text" pos:[20,55] width:125 height:25
    	
    	--event handlers
    	on bt1 pressed do changeText "ABCD"
    	on bt2 pressed do changeText "1234"
    )
    createDialog test1
    If you're using a rollout for your UI it's good practice to declare all your variables and functions within the rollout body. Then you won't need to use any global variables.
    Dan Brew

    Comment


    • #3
      Thanks Dan, that actually sorts out my problem from the other thread also
      Kind Regards,
      Morne

      Comment


      • #4
        Yeah I saw the other thread later. I can't think of a situation where you would need to use more than one global variable. I have a spline modelling tool that has nearly 1200 lines and 8 rollouts but I've only used one global.
        Dan Brew

        Comment


        • #5
          If I stick any commands after defining the rollout, I get an error
          for example any of these give an error "name expected"

          renderers.current = vray()
          rc=renderers.current
          renderSceneDialog.close()

          If I comment out any of them, then the 1st one after the comment fails

          But if I put that all above the rollout, then it works fine. I have to stick it inside the rollout however, otherwise I'm back to square one with defining it in each function. Why is it not working?
          Last edited by Morne; 24-09-2014, 12:12 AM. Reason: Arrrrggggggg
          Kind Regards,
          Morne

          Comment


          • #6
            You can't put code inside a rollout body unless it is inside a function or an event handler. But there are various events like the rollout opening and closing that can trigger calling a function.
            Code:
            try(destroydialog test1);catch()
            
            rollout test1 "Group Test" width:165 height:85
            (
            	--variable declaration
            	local variable1 = 10
            	local variable2 = "Variable declared in the rollout"
            	local rc -- declare rc as a local variable here
            	
            	--function declaration
            	fn rollOpen =
            	(
            		renderers.current = vray()
            		test1.rc=renderers.current --asign here
            		renderSceneDialog.close()
            	)
            	
            	fn rollClose =
            	(
            		print "That's all folks..."
            	)
            	
            	fn changeText txt =
            	(
            		test1.lb1.text = txt
            	)
            	
            	--ui controls
            	label lb1 "Text" pos:[20,5]
            	button bt1 "New Text" pos:[20,25] width:125 height:25
            	button bt2 "More Text" pos:[20,55] width:125 height:25
            	
            	--event handlers
            	on test1 open do rollOpen()
            	on test1 close do rollClose()
            	on bt1 pressed do changeText "ABCD"
            	on bt2 pressed do changeText "1234"
            )
            createDialog test1
            I've added two event handlers to the example I gave above. One runs after the rollout opens and the other when it closes.
            Dan Brew

            Comment


            • #7
              Yes Dan is correct. You can't execute those lines within the context of a rollout, you'd need to be in a function of the rollout to define code like that.

              Also: just as good programming modularity, I would genericize the "ChangeText" function to include the control you want to use to change the text.

              Code:
              	fn changeText cntrl txt =
              	(
              		if isProperty cntrl #text then cntrl.text = txt
              	)
              
              -- event handlers for button presses now declared like this
              	on bt1 pressed do changeText test1.bt1 "ABCD"
              	on bt2 pressed do changeText test1.bt2 "1234"
              Then you can easily change whatever text on any button (or any control with a .text property) by passing the control as an argument to the "changeText" function. So you don't need to make a new function for each button.
              Colin Senner

              Comment

              Working...
              X