Notifications
Clear all

[Closed] Callback persistence problem

I鈥檓 having a problem with a simple callback

code:

fn test = print 鈥渢est success鈥?br>
try ( callbacks.removeScripts #filePostOpen id:#ccTest ) catch()
try ( callbacks.addScript #filePostOpen 鈥渢est()鈥?id:#ccTest persistent:true ) catch()

this resides in an empty scene, when I open this maxfile by double-clicking the project file, I鈥檒l get an error :
>> MAXScript Callback script Exception: 鈥?Type error: Call needs function or class, got: undefined <<

if I open the file through max, I still get the error. It only works correctly when I run the script, then re-open the file.

the problem occurs with and without the try()catch() statements

I鈥檓 trying to unregister/register callbacks in a larger file, but can鈥檛 seem to register them when I open the file. What am I missing?

7 Replies

Hi,

You should declare your test fn as global.

Light

global test()
fn test = print 鈥渢est success鈥?br>
callbacks.removeScripts #filePostOpen id:#ccTest
callbacks.addScript #filePostOpen 鈥渢est()鈥?id:#ccTest persistent:true
鈥揷allbacks.addScript #postSystemStartup 鈥渢est()鈥?id:#ccTest persistent:true

this still gives me the same error. I鈥檝e tried declaring test and test() with and w/out the parenthesis. No luck. The #postSystemStartup callback didn鈥檛 work for me either.

What I鈥檓 trying to do is build some custom controls for a character and put them in a floater that pops up when you select any of the animation control objects. I can get that to work fine, but I can鈥檛 get the script to be localized in the file correctly, so when I pass the file off to the animator, they don鈥檛 have to wory about installing a script on their machine.

I can work around this persistence thing, but that would involve installing a startup script on each animators machine. Is there a way to get around that with a persistent callback?

hey Ben,

Try this version –


  fn test = 
  (
  messagebox "test success"
  --print "test success"---  
  --callbacks.removeScripts id:#postfileCB
  )
  
  callbacks.addScript #filePostOpen "test()" id:#postfileCB persistent:true
  

i think the way the persistent boolean works is that you open the file, run the script, and then save it. When you re-open the file it will execute the script. If you want to do this only once, uncomment the line which removes the callback. If you want it to run a piece of code everytime that file is opened you obviously dont want to unregister it since nothing will happen. Im sure someone will correct me if that鈥檚 not true!

hope this helps.

whoo! okay now when i quit max and re-load the file i get the function error. Damn those global variables!

I tried with the function declared as a global but it bums out if i restart max. Does it have to be a persistent global if it resides in max after a restart? i thought a global will be relevant outside the script/rollout etc鈥?but dissapears when max is restarted??? anyone???

callbacks.removeScripts #filePostOpen id:#ccTest
callbacks.addScript #filePostOpen 鈥減rint 鈥渢est success鈥濃€?id:#ccTest persistent:true

this works. I can open a fresh copy of max, and load my test file that had this script ran and was then saved. note that the callback doesn鈥檛 have a function call now. Seems like the error before is due to the fact that the function definition isn鈥檛 staying persistent in the file, even if it is declared as a global. Whenever max loads fresh, globals are only available when they are explicitly declared through a startup or user-run script.

is there a way to make a global variable, such as a function definition 鈥渟tick鈥?within the file, so it鈥檚 declared when the file is loaded? hmm鈥?/p>

edit
callbacks.addScript #filePostOpen 鈥済lobal test; test()鈥?id:#ccTest persistent:true
鈥揹id not work either

seems like only the declarations within the callback statement will be seen as persistent. outside function declarations have to be loaded separately before the callback

Hi Ben,

That makes sense about the function call throwing up the error. As for saving variables with a max file there is a topic that outlines 鈥減ersistent globals鈥?ie variables that can be saved with the max file. But I can鈥檛 seem to get my head round the syntax of it within a function though.
I guess the problem is that you want to be able to execute some code when the file is loaded irrelevant of which computer it is loaded on.
I thought you could call a macroscript with the code in from the global event handler, but then you鈥檇 need the script on each machine and you鈥檇 have the same problem. Hmmmphh.

okay ben,

ive tried your workling code and and put in a few script calls.


  callbacks.removeScripts #filePostOpen id:#ccTest
 callbacks.addScript #filePostOpen "print \"test success\" 
 messagebox \"and again\" 
 macros.run \"Track View\" \"LaunchDopeSheetEditor\"" id:#ccTest persistent:true

(the dopesheet macro should be on every copy of max)

i guess in order for the callback to be persistent to the maxfile you have to not only set the boolean to equal true but also it has to be specified within the string after the callback type. If you wanted to call a separate function you鈥檇 need to have that in a separate file and placed in the scripts>startup directory on each machine. using the
newline switch you could specify multiple script conditions to be executed when the file opens without any undefined variables crashing the callback out.