PDA

View Full Version : callback functions needs to be global?


am7
07-31-2009, 07:02 PM
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\n"
)

fn whendeleted = --define a callback function
(
callbacks.removeScripts #selectedNodesPreDelete
format "callback function entered, trying to call foo2()\n"
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!
)

am7
07-31-2009, 07:21 PM
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\n"
)

fn whendeleted = --doesn't work inside scope! needs to be global
(
callbacks.removeScripts #selectedNodesPreDelete
format "callback function entered, trying to call foo2()\n"
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!
)

denisT
07-31-2009, 07:52 PM
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\n"
when_select = whenselect2
foo()
)
fn whenselect2 = --define a callback function
(
callbacks.removeScripts id:#test_callback
format "selection changed 2\n"
when_select = whenselect1
foo()
)
fn foo =
(
callbacks.removeScripts id:#test_callback
callbacks.addscript #selectionSetChanged "when_select()" id:#test_callback
format "reset select callback\n"
)
when_select = whenselect1

foo() -- test it!
)


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

CGTalk Moderation
07-31-2009, 07:52 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.