Home Technical Talk

MAXSCRIPT Changehandler + object creation

polycounter lvl 15
Offline / Send Message
leslievdb polycounter lvl 15
I want an object to be created that is checked lateron with a changehandler. The problem is the creation of the object itself

when i evaluate the code below it complains about the variable not being an object (undefined variable)

when i try to create the object directly below the macroscript line it will create my object only when i evaluate and then run it the first time after that it won't anymore untill i evaluate the code again

what can i do to make this work?
macroScript Example category:"Example" ButtonText:"Example"
(
 local m_orientationsphere

on Execute do
    (
   m_orientationsphere = sphere radius:1 pos:[0,0,0]
   createDialog...
) 
when transform m_orientationsphere changes do
(
   dostuff
)
)

Replies

  • SyncViewS
    Offline / Send Message
    SyncViewS polycounter lvl 13
    Hi, if I get it right, following code should do what you're looking for.
    macroScript Example category:"Example" ButtonText:"Example"
    (
        local m_orientationsphere = undefined
    
        on execute do
        (
            m_orientationsphere = Sphere radius:1 pos:[0,0,0]
    
            deleteAllChangeHandlers id:#whenHandler
            
            when transform m_orientationsphere changes id:#whenHandler do
            (
                print "Sphere transformed!"
            )
        
            when m_orientationsphere deleted id:#whenHandler do
            (
                print "Sphere deleted!"
                deleteAllChangeHandlers id:#whenHandler
            )
        )
    )
    
  • monster
    Offline / Send Message
    monster polycounter
    Ha, I was about to post the same thing, but you know... mine was better.
  • leslievdb
    Offline / Send Message
    leslievdb polycounter lvl 15
    great gonna try it right away :) tnx syncviews ( and monster for the better one that is so good it can't be seen :P)
  • monster
    Offline / Send Message
    monster polycounter
    One thing I was going to say though, I don't usually use "on execute do" unless I need the isChecked, isEnabled, or isVisible event handlers.

    From the MaxScript help under "MacroScript":
    The <macro_script_body> can be one of two forms.
    The body can be either a single MAXScript expression, or a set of event handlers.
    

    Is in this case I would have done everything exactly like you did, except just remove the execute event handler.

    The help text leads me to believe that the event handlers have their own local scope which is why Ravenslayer's script didn't work to begin with. He was using both an expression and event handlers.
  • SyncViewS
    Offline / Send Message
    SyncViewS polycounter lvl 13
    Event handlers have their own scope as regular functions do. I'm almost certain of this, because you can call, in example, the code inside "on button pressed" event like a function, with button.pressed().

    But he declared the variable holding the pointer to the Sphere in macroScript scope, resolving the event scope issue.

    His specific issue was he wanted to set a "when change handler" on an object that didn't exist yet, which is an error.

    MacroScript scope issues are another subject matter because variables declared in the macroScript scope act like "private globals": the values they hold remain in place over many executions but only code in that construct can see the variable and so it does not conflict with other globals. Which implies that if you need to access, in example, a variable defined in a macroScript from a callback (which needs to reach it from the global scope), you must to declare it as global.

    Using "on execute" alone is perfectly fine. If you don't specify it, every time the macroScript is called, the whole body is evaluated, while if you define the "on execute" block, first time the macroScript is called the full body is evaluated, included the "on execute" block, from the second time that's the only code run at each call.
    macroScript Example category:"Example" ButtonText:"Example"
    (
        print "Macro evaluated"
    
        on execute do
        (
            print "Macro executed"
        )
    )
    
    -- First call:
    "Macro evaluated"
    "Macro executed"
    
    -- Second and subsequent calls:
    "Macro executed"
    
  • monster
    Offline / Send Message
    monster polycounter
    Cool man, thanks for explaining.
Sign In or Register to comment.