Search code examples
variablesscopemaxscript

Scope variable in maxscript


How do I pass the variable from one scope to another? I cannot predeclare it as the object is defined only when the user picks the object. Actually this code fails because it tries to read a local variable defined in another scope.

In pseudo code:

1.pick an object 2.copy the selected object

And the actual code:

macroScript Variables
category:"Stackoverflow"
toolTip:""

(

rollout Roll "Scopes"
(
 pickbutton pickobj "Pick object"
on pickobj picked objectPicked do if isValidNode objectPicked do pickobj.tooltip = "You picked " + objectPicked.name 

button pass "Pass the variable"
on pass pressed do (

newObj = copy objectPicked
print newObj

)
)
createDialog Roll 300 80

)

Solution

  • In this case, it's really simple as you can access the pickButton's object property:

    rollout Roll "Scopes"
    (
        pickButton pickObj "Pick object" autoDisplay:true
        button pass "Pass the variable"
    
        on pickObj picked objectPicked do
            if isValidNode objectPicked do
                pickobj.tooltip = "You picked " + objectPicked.name 
    
        on pass pressed do
        (
            local newObj = copy pickobj.object
            print newObj
        )
    )
    createDialog Roll 300 80
    

    But even without that, you can assign the node to a variable that's in a accessible scope:

    rollout Roll "Scopes"
    (
        local pickedObj
    
        pickbutton pickobj "Pick object" autoDisplay:true
        button pass "Pass the variable"
    
        on pickobj picked objectPicked do
            if isValidNode objectPicked do
            (
                pickedObj = objectPicked
                pickobj.tooltip = "You picked " + objectPicked.name
            )
    
        on pass pressed do
        (
            local newObj = copy pickedObj
            print newObj
        )
    )
    createDialog Roll 300 80