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

Instatiating a squirrel object and calling its functions from C
JonHodgson
#1 Posted : Tuesday, May 31, 2016 12:26:37 PM(UTC)
Rank: Member

Groups: Registered
Joined: 5/19/2016(UTC)
Posts: 15
Location: UK

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hi,

I have a squirrel script that defines a class, then instantiates an object of that class, then runs a function of that object.

So right now the last two lines read

Code:

local my_view_handler = MyViewHandler(textLabel);
my_view_handler.onViewAttached();


from c++ I put the value of textLabel onto the stack, run the script, and the correct thing happens.

So far so good, but that's just a test to make sure that MyViewHandler actually does what it should.

I need to do what those two lines do from C++.

i.e. first instantiate a squirrel object of a given class, and then call functions of that object.

I've found instructions on how to call functions from C

http://www.squirrel-lang...calling_a_function.html

But not on how to call a function that it part of an object.

I'm guessing it's simple when you know how, but I'm new to the squirrel way and so far my experiments have drawn a blank.

regards

Jon
fagiano
#2 Posted : Wednesday, June 1, 2016 6:55:07 AM(UTC)
Rank: Advanced Member

Groups: Registered, Administrators
Joined: 6/11/2005(UTC)
Posts: 1,056

Thanks: 0 times
Was thanked: 78 time(s) in 60 post(s)
JonHodgson wrote:


I've found instructions on how to call functions from C

http://www.squirrel-lang...calling_a_function.html

But not on how to call a function that it part of an object.

I'm guessing it's simple when you know how, but I'm new to the squirrel way and so far my experiments have drawn a blank.



Is the same as the example but you need to replace the "roottable" with the "object".

So instead of:
-push root table
-fetch function
-call function

Is something like:
-push root
-fetch object
-fetch function
-call function


Alberto
Follow me on Twitter @squirrellang
JonHodgson
#3 Posted : Wednesday, June 1, 2016 2:04:11 PM(UTC)
Rank: Member

Groups: Registered
Joined: 5/19/2016(UTC)
Posts: 15
Location: UK

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Hi Alberto,

thanks.

That was how I thought it might work, but I wasn't getting anywhere... but once I knew I was on the right path I dug a bit further and found that I had to declare the object with a <- assignment to put it into the root table.

So now I can get the object, and run the function, but I run into another issue.

My squirrel code looks like this

Code:
class ViewHandler {
    constructor(view)
    {
        view_ = view;
    }
   
    view_ = null;
}

class MyViewHandler extends ViewHandler

    function onViewAttached()
    {
        view_.setText("Hello World");
    }   
}

my_view_handler <- MyViewHandler(textLabel);
my_view_handler.onViewAttached();


And it works fine (textLabel is an instance of an Sqrat bound class).


However when I try to replicate the functionality of that last line in C++

Code:
    runScript(script_body.get());  // Uses SQRat Sripyt CompileString and Run methods

    sq_pushroottable(getVM());
    sq_pushstring(getVM(), _SC("my_view_handler"), -1);
    SQRESULT result = sq_get(getVM(), -2);
    sq_pushstring(getVM(), _SC("onViewAttached"), -1);
    result = sq_get(getVM(), -2);
    sq_pushroottable(getVM()); //'this' (function environment object)
    sq_call(getVM(), 1, SQFalse, SQTrue);
    sq_pop(getVM(), 2); //pops the roottable and the function


I get an error message

A Script Error Occured: the index 'view_' does not exist

So it seems that when it finishes running the script, it forgets about _view.

So I have two questions

1) Why the difference in behaviour when calling from inside squirrel versus from C++
2) How do I fix it?

regards

Jon
fagiano
#4 Posted : Wednesday, June 1, 2016 4:13:33 PM(UTC)
Rank: Advanced Member

Groups: Registered, Administrators
Joined: 6/11/2005(UTC)
Posts: 1,056

Thanks: 0 times
Was thanked: 78 time(s) in 60 post(s)
If you call a member function of an object, the "this"(aka environment object) must be the object itself and not root.Replace the second sq_pushroottable with sq_push(v,-2) (it repushes the object as first parameter).

ciao
Alberto
Follow me on Twitter @squirrellang
JonHodgson
#5 Posted : Wednesday, June 1, 2016 4:26:23 PM(UTC)
Rank: Member

Groups: Registered
Joined: 5/19/2016(UTC)
Posts: 15
Location: UK

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
Thanks, that makes sense.. and it worked :-)

It would be handy to have some simple examples of things like this in the documentation or on the Wiki. Once your brain starts to cotton on to the squirrel way it all starts to seem a bit obvious, but really it took me quite a while to realize about <- for example.

Now my next step is to work out how to create the object from C++.


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.135 seconds.