YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Passing native class to squirrel
mipk
#1 Posted : Monday, August 27, 2018 7:43:16 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 8/27/2018(UTC)
Posts: 1

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
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?
absence
#2 Posted : Thursday, August 30, 2018 9:49:31 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 127
Man
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 2 times
Was thanked: 10 time(s) in 10 post(s)
just store it where it's referenced (bindenv does not create a reference), e.g.
Code:

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({func=callback,environ=nc});
}

// invoke event handler
for (local i=0; i<3; i++) {
    eventHandler[i].func();
}
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.098 seconds.