🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Calling Registered Lua Functions

Started by
6 comments, last by D3DXVECTOR3 19 years, 11 months ago
Lets says I have a C function

void LoadFile(string file)
{
  //blablablabla
}


And I have registered this C Function with Lua like this

static void ThisCFunction(lua_State *L)
{
  LoadFile(lua_tostring(L, -1));
}

void Main()
{
  // Initialize Lua and other things
  .
  .
  lua_register(lua_State, "Function", ThisCFunction);
  .
  .
  // More code
}


Now what I would like to do is call ThisCFunction through Lua (by calling Function("file.bin")) lets say lua_dofunction existed then it woule look like this : lua_dofunction(lua_, "Function("file.bin"))"); The reason why I want it like this is becase it is for a Input Console and the called function can be anything
Advertisement
sounds like you want lua_dobuffer, which allows you to compile and execute arbitrary lua code (which in this case will consist of a call to a function).
Hmm it looks like it might be what I need.
When I try to implement is my program crashes, I also can't find any good example on how to use it. The Lua Manual has very little info about it.

EDIT

Nope it didnt work.... sortof.
When I call this from my main class it does what it needs to do., But when move the EXACT code to my console class my programm crashes. There is only 1 pLuaM so It cant be that.
RunRegisteredFunction(pLuaM, "AddPntLight(0,0,10,true,false)");

//-----------------------------------------------------------------------------// Name: RunRegisteredFunction()// Desc: //-----------------------------------------------------------------------------void RunRegisteredFunction(lua_State *pLua, string sFunction){	lua_dobuffer(pLua, sFunction.c_str(), strlen(sFunction.c_str()), "BUFFER");}


[Edited by - D3DXVECTOR3 on July 28, 2004 9:18:32 AM]
What sort of crash? Does code withinlua_dobuffer crash, or does your program crash later? Also, you should check the return value of lua_dobuffer.
BTW, the cleanest way to use lua_dobuffer with std::strings (which it works with very, very cleanly) is lua_dobuffer(L, myString.data(), myString.length(), "name"). Just a minor note. If you're using C strings (char arrays), lua_dostring is a more elegant method.
I looks like it's working now but my program still crashed after it, but not at lua_dobuffer but rather in another function that has absolutely nothing to do with LUA.
Could it be that I need to call another lua function after lua_dobuffer!?

Nah it does crash on Lua in this function:

int luaD_protectedparser (lua_State *L, ZIO *z, int bin) {  struct SParser p;  int status;>>  ptrdiff_t oldtopr = savestack(L, L->top);  /* save current top */  p.z = z; p.bin = bin;  luaZ_initbuffer(L, &p.buff);  status = luaD_rawrunprotected(L, f_parser, &p);  luaZ_freebuffer(L, &p.buff);  if (status != 0) {  /* error? */    StkId oldtop = restorestack(L, oldtopr);    seterrorobj(L, status, oldtop);  }  return status;}
Hmm, that's odd. Post the stack trace.
Oke I fixed it, this is what I did.

I had the lua_State as a static in some header. I initialized it and called lua_dofile and this has alwasy worked, but after that lua_State became 0x00000000. So I thought oke lets try it with putting it in a class. The class looks like this:

#define pLuaMain (CLuaMain::Instance())//-----------------------------------------------------------------------------// Lua Main Class//-----------------------------------------------------------------------------class CLuaMain{public:	CLuaMain();	~CLuaMain();	lua_State *pLuaM;	static CLuaMain* Instance();private:	static CLuaMain* _instance;};CLuaMain* CLuaMain::_instance = NULL;CLuaMain* CLuaMain::Instance(){	if(!_instance)		_instance = new CLuaMain;	return _instance;}


The constructor is just blank. After I initialized Lua as I normally would do. I called lua_dofile (as I alwasy did to do initialization based in the script file). Now lua_State stays valid and I can call any registered function via the console and Lua does the rest :D

Edit

The reason why I had is declared as a static was because I was lazy to create a class and It was only for testing. I wanted to see fast results. I feel ashamed :(

[Edited by - D3DXVECTOR3 on July 30, 2004 3:24:34 PM]

This topic is closed to new replies.

Advertisement