27 Apr, 2009, DotDotDash wrote in the 1st comment:
Votes: 0
I am completely new to embedding scripting languages in another language and I'm trying to make my C code extendable with Lua. I have the basics working, however I am quite confused about how to make a library visible to Lua. I'm not sure I have all the steps correct, but I'll post my example code below and hopefully someone can give me some valuable pointers.

void l_doTest()
{
int s;

s = luaL_dofile(L, "script.lua");

if(s != 0)
fprintf(stderr, "%s\n", lua_tostring(L, -1));
}

static int l_get_test_msg(lua_State *L)
{
lua_pushstring(L, "This message was sent by C, yay!");
return 1;
}

static const struct luaL_reg l_test_lib[] =
{
{"getTestMsg", l_get_test_msg},
{NULL, NULL}
};

void initLuaTest()
{
luaL_register(L, "test", l_test_lib);
}


Now I'm not sure if I am totally wrong here but here is the Lua code I have for the above:

– This is a simple test script

print("Lua received the test message: " .. test.getTestMsg)

– End of test script


This just simply does not work and spits back some error message about test not existing basically. All help would be appreciated, thanks!
27 Apr, 2009, David Haley wrote in the 2nd comment:
Votes: 0
This isn't really enough information to go by; you'd need to show us how you're creating your state.

Note that it should be test.getTestMsg() – note parentheses.
27 Apr, 2009, DotDotDash wrote in the 3rd comment:
Votes: 0
Two functions really, one when the mud starts up, and another when it shuts down. For reference, I am using a global Lua state rather than seperate ones.

lua_State *L;
void initLuaTest();

void init_lua_engine()
{
L = lua_open();
luaL_openlibs(L);
initLuaTest();
}

void close_lua_engine()
{
lua_close(L);
}


I hope that helps a bit more.
27 Apr, 2009, David Haley wrote in the 4th comment:
Votes: 0
I guess there's something that's not visible from what you've posted; the following works:

$ cat test.c

#include <stdio.h>
#include <lua5.1/lua.h>
#include <lua5.1/lauxlib.h>
#include <lua5.1/lualib.h>

static int l_get_test_msg(lua_State* state)
{
lua_pushstring(state, "C message");
return 1;
}

static const struct luaL_reg l_test_lib[] =
{
{"getTestMsg", l_get_test_msg},
{NULL, NULL}
};

int main(int argc, char* argv[])
{
lua_State* state = lua_open();
luaL_openlibs(state);
luaL_register(state, "test", l_test_lib);

int err = luaL_dostring(state, "print(test.getTestMsg())");

if (err) {
fprintf(stderr, "%s\n", lua_tostring(state, -1));
}

lua_close(state);

return 0;
}

$ gcc -Wall test.c -llua5.1
$ ./a.out
C message
27 Apr, 2009, elanthis wrote in the 5th comment:
Votes: 0
Hmm, it looks correct to me, other than the missing parenthesis David pointed out. I cut-n-pasted your code into a single source file, wrote a simple main() that called init_lua_script(), l_doTest(), and close_lua_script(), and compiled/ran it. Gave me:

script.lua:3: attempt to concatenate field 'getTestMsg' (a function value)

Which is the parenthesis thing. I added () after getTestMsg and reran, and got:

Lua received the test message: This message was sent by C, yay!

So the code is correct. Something else you're doing is causing it to break, in which case we can't help since you're not showing us everything else. What version of Lua are you using?
27 Apr, 2009, David Haley wrote in the 6th comment:
Votes: 0
IIRC, the behavior of opening libraries changed between 5.0 and 5.1, which might explain why it worked for Elanthis and me but isn't working for you – if you're using 5.0.
27 Apr, 2009, DotDotDash wrote in the 7th comment:
Votes: 0
Thanks David, it works now. Great help, thanks a bunch!

EDIT: I am using Lua 5.1
27 Apr, 2009, David Haley wrote in the 8th comment:
Votes: 0
Glad to hear that it's sorted out now. What was the problem? It might help somebody else figure out the same problem in the future, especially as more and more people are embedding Lua in their MUDs.
27 Apr, 2009, DotDotDash wrote in the 9th comment:
Votes: 0
I believe as soon as I added the parenthesis it worked. I'll keep testing and let you know if I come across anymore problems, but right now all is running fine.
27 Apr, 2009, David Haley wrote in the 10th comment:
Votes: 0
Ah. :wink: Well, in that case, you should have been more precise in your initial report of the error message, since you told us it was saying that 'test' wasn't found, not the message that Elanthis posted. :tongue: The best way to get good help quickly is to give a precise and accurate description of the problem.
0.0/10