Hey Squirrel-Forum,
I think I'm losing my mind - currently I'm investigating Squirrel for use in one of our projects. However, I fail with the most basic feature: calling a function from C/C++ into Squirrel.
My squirrel code looks like this:
Code:
function tuna()
{
print("Blaaaaa");
}
tuna();
I would love to execute the function tuna via the Squirrel API. I followed the examples from the Documentation and on the C-Side I wrote:
Code:SQInteger readFile(SQUserPointer file)
{
char buffer;
if(fread(&buffer, sizeof(char), 1, (FILE*)file) > 0)
{
return buffer;
}
return 0;
}
bool compileScript(HSQUIRRELVM vm, const char *fileName)
{
FILE *file = nullptr;
if(fopen_s(&file, fileName, "r") == 0)
{
const bool success = SQ_SUCCEEDED(sq_compile(vm, readFile, file, "Leviathan", true));
fclose(file);
return success;
}
return false;
}
void printfunc(HSQUIRRELVM v, const SQChar *s, ...)
{
va_list arglist;
va_start(arglist, s);
vprintf(s, arglist);
va_end(arglist);
}
In my main I execute "compileScript" with my freshly setup VM and the path to my "hello.nut" file.
Code:
HSQUIRRELVM vm = sq_open(1024);
printf("Compiling %s...[%s]\n", argv[1], (compileScript(vm, argv[1])) ? "OK" : "FAILED");
sq_setprintfunc(vm, printfunc, printfunc); //sets the print function
From there it depends! If I do:
Code:sq_pushroottable(vm);
sq_call(vm, 1, true, true)
Then I will get my output! But If I try to find my function "tuna" with:
Code:
sq_pushroottable(vm); //pushes the global table
sq_pushstring(vm, "tuna", -1);
printf("-> %i\n", sq_get(vm, -2));
Then I get -1 and I will not be able to call it. I feel like I'm doing something obviously wrong - but I can't figure it out...