Squirrel Call To Cpp
Last changed: -92.222.238.172

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