YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Embedded Squirrel - Running a function
Jaberwocky
#1 Posted : Thursday, October 31, 2019 1:50:36 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/31/2019(UTC)
Posts: 1

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
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...
Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.144 seconds.