Simple invocation of pd->lua->callFunction fails

Windows, SDK 2.4.2:

Lua code in 'main.lua':

function callback() print("callback") end

C code:

char* outerr = NULL;

pd->lua->callFunction("callback", 0, &outerr);

if (outerr)
{
    pd->system->logToConsole("C->Lua invocation failed: %s", outerr);
}

Result on Simulator console:

C->Lua invocation failed: no such function 'callback'

Am I doing something wrong, or are there still issues with pd->Lua->callFunction?

This is somewhat of a show-stopper for me, I'm working on a project where being able to call into Lua from C is pretty fundamental.

Edit: this turned out to be cockpit error, much thanks to @scratchminer for the fix!

As background, this issue arose while integrating a scripting language for use in a game using LDtk with a C/Lua RPG (see here ). I'm hoping to have interacting with LDtk-defined entities be driven by scriptlets like this (very simple example):

In the example above, the script would pop up a pdDialog in-game.

I tried bypassing the pd->lua stuff and accessing Lua directly, doesn't seem to be possible(?):

extern int lua_getglobal(lua_State* L, const char* name);
extern int lua_pcall(lua_State* L, int nargs, int nresults, int msgh);
:
// trying to upcall into Lua directly using lua_getglobal and lua_pcall <snipped>
:
(linker bails out with)
vm.obj : error LNK2019: unresolved external symbol lua_getglobal referenced in function DialogSayNative
vm.obj : error LNK2019: unresolved external symbol lua_pcall referenced in function DialogSayNative

I mean, I could work around this (build a message queue in C rather than upcalling to Lua, then poll from the Lua side down into C and process accordingly) but that would be such a kludge.

Where are you calling the callback function? In the game loop, or at initialization?
The function should exist if you're calling it from within the game loop, but if it's in kEventInitLua, no Lua code has actually been run yet so the function doesn't exist.

2 Likes

Yes, that was it!

Instead of (Lua) testing the C code like this (global, running when the Lua starts up):

engine = cstuff.new()
engine:test()

Running it like this:

engine = cstuff.new()
:
DoneOnce = false

function pd.update()
    if DoneOnce == false then
        DoneOnce = true
        engine:test()
    end
end

Resolves the issue and the pd->lua->callFunction works.

Thanks!