Hello,
I'm using sqrat to expose C++ classes to squirrel (for UI Management). Now I wanted to add event handlers (in squirrel) that get executed via native code but stumbled upon a little problem.
I have some native code that creates and works with "NativeClass" but I want to be able to do stuff with squirrel with it (for example: event callback/handling). Like this (simplified):
Code:
// add events to our "eventHandler" (array-like structure)
for (local i=0; i<3; i++) {
local nc = getNativeClass(i); // get i-th native class
local callback = function() {
::print("this=" + this); //|this| will be null
}.bindenv(nc);
eventHandler.append(callback);
}
// invoke event handler
for (local i=0; i<3; i++) {
eventHandler[i]();
}
When invoking the event handler all references to "nc"/NativeClass are lost and therefore it will be released and the environment passed into our callback will be null.
Is it possible to extend the lifetime of "nc" to allow passing it around? Do I have to change my code so that when creating a NativeClass a squirrel object will be created too?