MaxScript: MacroScript vs. callbacks

MaxScript: MacroScript vs. callbacks

istan
Advisor Advisor
889 Views
4 Replies
Message 1 of 5

MaxScript: MacroScript vs. callbacks

istan
Advisor
Advisor

In a MacroScript all functions are declared within the MacroScript(). But when I need for instance a selection changed callback, a function from inside such a MacroScript cannot be used for the callback. But when I declare the callback function outside as global function, it will not be executed, when the MacroScript is assigned to a button. What is the workaround?

0 Likes
Accepted solutions (1)
890 Views
4 Replies
Replies (4)
Message 2 of 5

Swordslayer
Advisor
Advisor

Without knowing what you want to use the callback for and what sort o a callback it is, it's hard to give an advice. You can always have a global struct inside the macroscript and either create a singleton instance when the macroscript gets evaluated first (in the top-level scope, provided that the 'on execute' handler is there) or a new instance on each run (in the 'on execute' scope). Depending on what you want to achieve, you can either assign the handler in the struct create event or from elsewhere in your code.

0 Likes
Message 3 of 5

istan
Advisor
Advisor

This is what I want to do:

 

MacroScript CallbackTest category:"__My Std Tools"
(
  global var1 = 1234
  
  fn mychangehandler obj = (
    format "selected! %\n" obj
  )

  rollout testRollout "CallbackTest" width:400 height:300
  (
	button Inst "inst" pos:[100,100] width:50 height:30
	button UnInst "uninst" pos:[100,140] width:50 height:30
  
	on Inst pressed do (
	  callbacks.addScript #selectionSetChanged "mychangehandler( var1 )"
	)
	
	on UnInst pressed do (
	  callbacks.removeScripts #selectionSetChanged   
	)
  )
  
  CreateDialog testRollout
)

0 Likes
Message 4 of 5

Swordslayer
Advisor
Advisor
Accepted solution

If it's just one function, make it a global.

 

global mychangehandler, var1 = 1234

Also, get into habit of using the 'on execute' handler, if it's present, only its contents are evaluated when the macroscript is called, otherwise everything is re-evaluated (so for example your locals are overwritten by the initial values etc.). In your example, it's enough to replace the last line with

 

on execute do createDialog testRollout
Message 5 of 5

istan
Advisor
Advisor

Thanks!

Unbelievable how simple this was.

And also thanks for the "on execute" hint.

But C++ is still easier for me 😉

0 Likes