[Closed] NodeEventCallBack function in struct issue
I am getting seemly random error messages to do with the nodeEventCallbacks I’m using in my script. I have found one solid way to reproduce the error and that is by merging in a maxfile.
The following code appears to work when creating and deleting objects, but then occasionally it will randomly error, but the only sure way to get it to error that I know of is to merge in a maxfile. It doesn’t error if I don’t have the function in a struct, but I’d rather have it in a struct.
Could anyone explain why this doesn’t work?
thanks
Phil
(
::newNodeEventCallback
newNodeEventCallback = undefined
gc light:true
struct FunctionStruct
(
fn eventTestFn node event =
(
print (random 1 100)
)
)
global fnSt = FunctionStruct()
newNodeEventCallback = NodeEventCallback nameChanged:fnSt.eventTestFn hideChanged:fnSt.eventTestFn deleted:fnSt.eventTestFn added:fnSt.eventTestFn delay:200
)
btw…
::newNodeEventCallback
doesn’t make sense for me
fn eventTestFn node event
is in backward order
why you don’t want to have both NodeEventCallback and callback function in the same structure?
global NodeCallbackMonitor
(
struct NodeCallbackStruct
(
eventCallback,
fn disposeCallback = if isstruct NodeCallbackMonitor do
(
NodeCallbackMonitor.eventCallback = undefined
gc light:on
),
fn callbackMonitor event handles =
(
format "event:% handles:%
" event handles
),
on create do
(
disposeCallback()
eventCallback = NodeEventCallback all:callbackMonitor
eventCallback.enabled = on
)
)
NodeCallbackMonitor = NodeCallbackStruct()
ok
)
Thank you for the response, I’ll have to check through my code tomorrow.
In my actual script I had a struct for the UI and a Struct for the functions, and I was creating the nodeCallbackEvent in the UI struct and pointing it to the functions in the other one.
But to be honest, like you suggested, it probably makes sense to have the callback call a fn in the same struct even if then that fn calls the other struct, and If it fixes my issues then that’s the main thing.
Still don’t understand why it only partly works, but your code seems to work for me fine, so thank you for that example.
I was trying to use the :: to set it as a global, but now I see I was confused, as the situation was different to http://forums.cgsociety.org/showthread.php?f=98&t=1097449
and there was no need to do that, because I wasn’t setting it as a local before that point.
i can confirm that this snippet
try
(
newNodeEventCallback = undefined
gc light:true
)
catch()
global newNodeEventCallback
struct FunctionStruct
(
fn eventTestFn event handles =
(
format "event:%
" event
)
)
global functions = FunctionStruct()
newNodeEventCallback = NodeEventCallback all:functions.eventTestFn
newNodeEventCallback.enabled = on
crashes after any garbage collection (what merge actually does do). it’s a max bug. there is no a reasonable explanation of this.
It seems to work ok if you have everything in one structure…
(
::functions = undefined
gc light:true
struct FunctionStruct
(
newNodeEventCallback,
fn eventTestFn event handles =
(
format "event:%
" event
),
fn init =
(
newNodeEventCallback = NodeEventCallback all:eventTestFn
),
start = init()
)
global functions = FunctionStruct()
)
Max 2013 btw.