[quote user="THACO"]Another question on sqPlus. I was trying to create a C function to be used in squirrel. I was able to reproduce the example showed in the documents the print_args function with just squirrel. With squirrel plus the function works //Squirrel sq_pushroottable(v); sq_pushstring(v,"newfunc",-1); sq_newclosure(v,print_args,0); //create a new function sq_createslot(v,-3); sq_pop(v,1); //pops the root table //Squirrel Plus SquirrelVM::CreateFunction(print_args); The above CreateFunction() compiles and seems to run fine, but I cannot call a function in the script, I also do not know what the function would be called in the script, I tried just calling print_args but it says print_args does not exist. I'll be testing more things throughout the day (school hasn't started yet so I have plenty of time to mess around) -Matt[/quote]
SquirrelVM::CreateFunction() calls sq_newclosure(). It needs a table and a name before being called, and a slot must be created in the table (as shown in the Vanilla Squirrel example). I searched through all the code and could not find SquirrelVM::CreateFunction() being used elsewhere. I'm curious how SquirrelVM::CreateFunction(), SquirrelVM::CreateString(), etc., are used?
The following is the result of a quick test:
int testFunc(HSQUIRRELVM v) {
StackHandler sa(v);
int paramCount = sa.GetParamCount();
printf("testFunc: numParams[%d]\n",paramCount);
for (int i=1; i <= paramCount; i++) {
printf("param[%d]: ",i);
switch(sa.GetType(i)) {
case OT_TABLE: printf("OT_TABLE[0x%x]\n",sa.GetObject(i)); break;
case OT_INTEGER: printf("OT_INTEGER[%d]\n",sa.GetInt(i)); break;
case OT_FLOAT: printf("OT_FLOAT[%f]\n",sa.GetFloat(i)); break;
case OT_STRING: printf("OT_STRING[%s]\n",sa.GetString(i)); break;
default:
printf("TYPEID[%d]\n",sa.GetType(i));
} // switch
} // for
return SQ_OK;
} // testFunc
// Creates a function in the table currently on the stack.
void CreateFunction(HSQUIRRELVM v,const SQChar * scriptFuncName,SQFUNCTION func,int numParams=0,const SQChar * typeMask=0) {
sq_pushstring(v,scriptFuncName,-1);
sq_newclosure(v,func,0);
SQChar tm[64];
if (typeMask) {
if (_snprintf(tm,sizeof(tm),"t%s",typeMask) < 0) {
sq_throwerror(v,_T("CreateFunction: typeMask string too long."));
} // if
} else {
tm[0] = 't';
tm[1] = 0;
} // if
sq_setparamscheck(v,numParams+1,tm); // Parameters are table+args (thus, the +1).
sq_setnativeclosurename(v,-1,scriptFuncName);
sq_createslot(v,-3); // Create slot in table (assigning function to slot at scriptNameFunc).
} // CreateFunction
// Create a Global function on the root table.
void CreateFunctionGlobal(HSQUIRRELVM v,const SQChar * scriptFuncName,SQFUNCTION func,int numParams=0,const SQChar * typeMask=0) {
sq_pushroottable(v); // Push root table.
CreateFunction(v,scriptFuncName,func,numParams,typeMask);
sq_pop(v,1); // Pop root table.
}// CreateFunctionGlobal
// In main():
try {
HSQUIRRELVM v = SquirrelVM::GetVMPtr();
CreateFunctionGlobal(v,"testFunc0",testFunc);
CreateFunctionGlobal(v,"testFuncN",testFunc,1,"n");
CreateFunctionGlobal(v,"testFuncS",testFunc,1,"s");
SquirrelObject main = SquirrelVM::CompileBuffer("testFunc0(); testFuncN(1); testFuncN(1.23); testFuncS(\"Hello\");");
SquirrelVM::RunScript(main);
} // try
catch(SquirrelError &e) {
char buff[256];
sprintf(buff,"Error: %s, %s\n",e.desc,_SC("Squirrel::TestObj"));
// OutputDebugString(buff);
puts(buff);
} // catch
John