Notifications
Clear all

[Closed] callback functions needs to be global?

 am7

Does the callback functions need to be declared globally? If this is the case I have to keep all functions that needs to be accessed from the callback global too? (ie: foo2())

For example, I don’t want to declare foo2 as a global function – I still want it to stay inside my scope level. Does anyone have a solution to this?

Once you put the callback function inside the scope level it fails.


  (
	fn foo2 =
  	(
  		format "another function
"
  	)

  	fn whendeleted = --define a callback function
  	(
  		callbacks.removeScripts #selectedNodesPreDelete
  		format "callback function entered, trying to call foo2()
"
  		foo2()
  	)
  
  	fn foo =
  	(
  		-- make sure we remove the callback
  		callbacks.removeScripts #selectedNodesPreDelete
  		
  		--register the function as general callback
  		callbacks.addscript #selectedNodesPreDelete "whendeleted()"
  		
  		mypot1 = teapot() --create a teapot
  		select mypot1
  		max delete --trigger the callback function
  	)
  	
  	foo() -- test it!
  )
  
2 Replies
 am7

I did an initial global function decleration, and suddenly it works!

Can anyone explain why? Underneath is the working example:


 global whendeleted
 (
 	fn foo2 =
 	(
 		format "another function
"
 	)
 	
 	fn whendeleted = --doesn't work inside scope! needs to be global
 	(
 		callbacks.removeScripts #selectedNodesPreDelete
 		format "callback function entered, trying to call foo2()
"
 		foo2() -- can't access foo2() inside scope level beneath
 	)
 
 	fn foo =
 	(
 		-- make sure we remove the callback
 		callbacks.removeScripts #selectedNodesPreDelete
 		
 		--register the function as general callback
 		callbacks.addscript #selectedNodesPreDelete "whendeleted()" persistent:true
 		
 		mypot1 = teapot() --create a teapot
 		select mypot1
 		max delete --trigger the callback function
 	)
 	
 	foo() -- test it!
 )
 

as minimum a pointer to function in callback function has to be global, but here is a trick:


(
 global when_select
 local foo, whenselect1, whenselect2
 fn whenselect1 = --define a callback function
 (
  callbacks.removeScripts id:#test_callback
  format "selection changed 1
"
  when_select = whenselect2
  foo()
 )
 fn whenselect2 = --define a callback function
 (
  callbacks.removeScripts id:#test_callback
  format "selection changed 2
"
  when_select = whenselect1
  foo()
 )
 fn foo =
 (
  callbacks.removeScripts id:#test_callback
  callbacks.addscript #selectionSetChanged "when_select()" id:#test_callback
  format "reset select callback
"
 )
 when_select = whenselect1
 
 foo() -- test it!
)

so, you can use local functions in callback assigning them to global pointer