Show Changes Show Changes
Edit Edit
Print Print
Recent Changes Recent Changes
Subscriptions Subscriptions
Lost and Found Lost and Found
Find References Find References
Rename Rename
Administration Page Administration Page
Topic Locks Topic Locks

Search

History

2/22/2017 3:49:48 AM
-92.222.238.172
9/2/2015 9:44:54 PM
-36.78.208.180
5/13/2014 6:33:40 PM
-85.171.209.107
5/12/2014 12:41:04 PM
-85.171.209.107
3/21/2014 3:05:53 PM
-46.65.64.100
List all versions List all versions

RSS feed for the SquirrelWiki namespace

Squirrel Call To Cpp
.
Summary

We will first create perut buncit wanita some helper functions then the main code will follow

1. Callback Function.

This is function we wish to call from squirrel. Function must be in form of

 SQInteger fn (HSQUIRRELVM v)

where fn is function to be bound.

This is the example from the documentation (http://www.horoskop-2016.org/) :

 SQInteger print_args(HSQUIRRELVM v)
 {
        SQInteger nargs = sq_gettop(v); //number of arguments
        for(SQInteger n=1;n<=nargs;n++)
        {
                printf("arg %d is ",n);
                switch(sq_gettype(v,n))
                {
                case OT_NULL:
                        printf("null");
                        break;
                case OT_INTEGER:
                        printf("integer");
                        break;
                case OT_FLOAT:
                        printf("float");
                        break;
                case OT_STRING:
                        printf("string");
                        break;
                case OT_TABLE:
                        printf("table");
                        break;
                case OT_ARRAY:
                        printf("array");
                        break;
                case OT_USERDATA:
                        printf("userdata");
                        break;
                case OT_CLOSURE:
                        printf("closure(function)");
                        break;
                case OT_NATIVECLOSURE:
                        printf("native closure(C function)");
                        break;
                case OT_GENERATOR:
                        printf("generator");
                        break;
                case OT_USERPOINTER:
                        printf("userpointer");
                        break;
                default:
                        return sq_throwerror(v,"invalid param"); //throws an exception
                }
        printf("\n");
        }


        sq_pushinteger(v,nargs); //push the number of arguments as return value
        return 1; //1 because 1 value is returned
 }

2. Register Function

This is helper function to register global functions (taken from documentation).

 SQInteger register_global_func(HSQUIRRELVM v,SQFUNCTION f,const char *fname)
 {
        sq_pushroottable(v);
        sq_pushstring(v,fname,-1);
        sq_newclosure(v,f,0); //create a new function
        sq_createslot(v,-3);
        sq_pop(v,1); //pops the root table
        return 0;
 }

3. Run Script Function

 void run_script(HSQUIRRELVM v,char* cmd)
 {
        sq_compilebuffer(v,cmd,(int)strlen(cmd)*sizeof(SQChar),"compile",1);
        sq_pushroottable(v);
        sq_call(v,1,1,0);
 }

3. Main Code

        HSQUIRRELVM sqvm = sq_open(1024);
        register_global_func(sqvm,::print_args,"prt");
        char* cmd = "prt (100);";
        run_script(sqvm,cmd);

Output...

        Arg 1 is table
        Arg 2 is integer

In this example the print_args function tv one live streaming was registered in Squirrel as "prt". The squirrel script called this function with one integer argument. It can also be called with multiple arguments from the squirrel script.

Not logged in. Log in

squirrel's community wiki

This is FlexWiki, an open source wiki engine.

Change Style

Recent Topics