Skip to main content
GameDev.net gamedev.net
🔒 Locked

Calling a Lua function from c++ every frame

Started by ishkabible Jun 9, 2010 at 4:48 PM 3 replies 2.2k views
Original Post
ishkabible
ishkabible
ok so i'm a little fuzzy on how to do this, the function dose not need to return any thing. i'm confused on how lua_getglobal works, i want to use a function in Lua that i'm going to call "main_loop" but after a declare it as a global variable using get global how do i call it? i think i just use lua_call(L,0,0) since there are no return values or arguments. but i'm unsure thats why i'm asking.
Nanoha
Nanoha
did you try it? what happened when you did?

I'd suggest taking a look at somehting like luabind if your planning on using lua alot, it makes all this (and more) really simple. Sooner you start using it, easier it'll all be.
Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.
ishkabible
ishkabible
it said "PANIC: error in call to Lua API (attempt to call nil valuue)"

here is my test programs code:
#include <iostream>extern "C" {    #include "lua.h"    #include "lualib.h"    #include "lauxlib.h"}using namespace std;lua_State* L;void LuaPrint() { lua_getglobal(L, "PrintMsg"); lua_call(L, 0, 0);}int main(){    L = lua_open();    luaL_openlibs(L);    luaL_dofile(L, "foo.lua");    LuaPrint();    lua_close(L);    return 0;}


and here is the lua script:

function PrintMsg() {
io.write("ok this is how you will call it");
}
m_switch
m_switch
That's not valid Lua.

It should look like:

function PrintMessage()   io.write("...")end


You need to check the return value from luaL_dofile so you can catch these sorts of errors. Likely you'd want to use lua_pcall over lua_call and check its return value also.
ishkabible
ishkabible
o oops, now it's working thank you. i guess i need to remember to shift gears from c++ to Lua, not C++ to some mixture between flash and Lua, any way thanks again

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.