Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

[SQPlus] Call squirrel function from cpp and pass class argument

Last post 08-22-2008, 8:41 AM by DarthDie. 2 replies.
Sort Posts: Previous Next
  •  08-20-2008, 10:54 AM 2706

    [SQPlus] Call squirrel function from cpp and pass class argument

    What I want todo is call a Squirrel function from cpp and pass in a class argument that i've binded using sqplus (its a simple Vector3 class), but I can't seem to find out how todo that.
    Any suggestions?
  •  08-21-2008, 9:23 PM 2711 in reply to 2706

    Re: [SQPlus] Call squirrel function from cpp and pass class argument

    I wanted to know this too, so I did a bit of poking. There's a sample buried in testSqPlus2.cpp of how to do it at about line 1027. Here's my simplified version:

    //Script.nut file
    function MyFunc( vec ) {
       print("X: " + vec.x + ", Y: " + vec.y + ", Z: " + vec.z);
       return vec.x > vec.y;
    }

    //CPP file
    Vector3 v(9.0f, 3.0f, 4.0f);
    SquirrelObject script = SquirrelVM::CompileScript(_T("Script.nut"));
    SquirrelVM::RunScript(script);
    bool result = SquirrelFunction<bool>(_T("MyFunc"))(v);  
    cout << "Result = " << result << endl;


    Note that you have to Run the script before you can retrieve the function. There may be a way around that, but I don't know it yet. Just don't put any code outside of a function and you'll be fine.

    Alos, for the sake of clarity: the template argument of SquirrelFunction is the return type you expect back. void is perfectly acceptable here.

    That clear it up?
  •  08-22-2008, 8:41 AM 2712 in reply to 2711

    Re: [SQPlus] Call squirrel function from cpp and pass class argument

    Hey, thanks for the help but i've found a way to push just like a int,char,float etc. SqPlus::Push
    So I can just do:

    Vector3 v = new Vector3(0,1,2);
    SqPlus::Push(SquirrelVM::GetVmPtr(),v);

    It works great :D.

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems