Home Technical Talk

[MAXScript] Passing variables to a function within a callback

polycounter lvl 13
Offline / Send Message
Wesley polycounter lvl 13
Hey folks, I'm a little stuck here. I'm trying to get a function to run while the view is changing. But I'm struggling to pass off anything to the function within the callback.

This won't work for instance:
fn theFun theModel =
(
	print theVar.name as string
)

try (destroyDialog theScript) catch()
rollout theScript "The Script"
(
	--Filter for pick buttons, makes sure user can only select geometry
	fn meshFilter obj = superClassOf obj == GeometryClass
	group "Random Group"
	(
		pickButton meshPick "Mesh Pick" width:140 height:30 filter:meshFilter autoDisplay:true
	)
	button theButton "BUTTON" width:140 height:30
	
	--Combine button
	on theButton pressed do
	(
		theModel = meshPick.object
		callbacks.addScript #viewportChange "theFun theModel" id:#theViewCall
	)
)
createDialog theScript 160 160

If I change "theFun theModel" to "callFun = theFun theModel" like I'd usually use it doesn't work either.

If I just call the function without the callback it works fine. This is the first time I've tried using callbacks so I'm probably just noobing it up here.

Thanks for reading.

Replies

  • Norman3D
    Options
    Offline / Send Message
    Norman3D polycounter lvl 14
    I don't quite understand what you are trying to do. I don't think how you'll be able to print "theVar.name". The value is not defined anywhere in the script.
  • BcM
    Options
    Offline / Send Message
    BcM
    even though theVar never gets defined anywhere, the simplest solution is to make theModel a global var. Not the most beautiful solution but it works.
  • Wesley
    Options
    Offline / Send Message
    Wesley polycounter lvl 13
    Well this was a simple demonstration of my problem, in my actual script I was trying to use some UI variables and that was my real problem. But I can just create some global variables from those before the callback. Thanks for the help guys!
  • monster
    Options
    Offline / Send Message
    monster polycounter
    Did you try?
    callbacks.addScript #viewportChange "theFun meshpick.object" id:#theViewCall
    

    If you put theFun inside the rollout you can just do:
    try (destroyDialog theScript) catch()
    rollout theScript "The Script"
    (
    	--Filter for pick buttons, makes sure user can only select geometry
    	fn meshFilter obj = superClassOf obj == GeometryClass
    	group "Random Group"
    	(
    		pickButton meshPick "Mesh Pick" width:140 height:30 filter:meshFilter autoDisplay:true
    	)
    
    	fn theFun =
    	(
    		print meshpick.object.name
    	)
    
    	button theButton "BUTTON" width:140 height:30
    	
    	--Combine button
    	on theButton pressed do
    	(
    		--theModel = meshPick.object
    		callbacks.addScript #viewportChange "theFun()" id:#theViewCall
    	)
    )
    createDialog theScript 160 160
    
Sign In or Register to comment.