Archived

This topic is now archived and is closed to further replies.

Ironica

Basic Lua Interpreter

Recommended Posts

Ironica    128
Hey guys, could someone please show me how to actually interpret a Lua file? I''ve been reading the manual, and going over the source code, but I keep getting lost in other things. I''ve seen the function luaL_loadfile, but that doesn''t print anything back to the screen. I don''t need it to print anything, but does that actually interpret the lua in the file? If I had something like this:
lua_State *l = lua_open();
openstdlibs(l); // won''t bother including this, but you get the idea
luaL_loadfile(l, "file.lua");
lua_close(l);
 
Would that interpret file.lua? Doesn''t matter if it doesn''t print back to the screen, but would save me a lot of time testing it, if someone could just tell me =) And if not, how do I do it? Hehe. Sorry for a possibly dumb question, but I keep confusing myself by trying to figure it out =)

Share this post


Link to post
Share on other sites
Sneftel    1788
luaL_loadfile() reads in, parses, and bytecode-compiles a script into a function, which it pushes onto the stack. In order to execute the script, you must then call the function.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
dofile() will load and then execute the file as a chunk, from the docs

Share this post


Link to post
Share on other sites
jeanlucpikachu    122
The docs are terrible. Roberto Ierusalimschy is working on a book, the draft of which is online. It''s for lua 4, but looking through it will give you a basic idea of how to proceed.

http://www.inf.puc-rio.br/~roberto/luabook1.pdf

You want Part V.

Love & Peace ^_^v
Capt. Jean-Luc Pikachu

Share this post


Link to post
Share on other sites
Ironica    128
Thanks guys =)
That''s a great book, jeanlucpikachu, I''ve downloaded it.
I saw the function lua_dostring(), and guessed lua_dofile() was what I was looking for. Bingo! Thanks for the help =)

Share this post


Link to post
Share on other sites
Toolmaker    967
If you are writing a game/program that supports complete scripting and you want to be able to call several functions from within your program from the script, you need to load the script, push it onto the stack and execute it once. In my mud server, there are several files which are loaded that way. Now I can execute a specific function from within my code, without have for each function a different file. This is my code:


// Loading a lua file

bool CBasicController::LoadScript(const char *szScriptName)
{
if (luaL_loadfile(LuaVM, szScriptName) != 0)
return (false);

// Do a function call to execute the loaded chunk

if (lua_pcall(LuaVM, 0, 0, 0) != 0)
return (false);;

return (true);
}


And here I execute a specific function, which is in one of the files using lua_pcall(Protected call, returns non zero on error):

// Execute OnConnect script

if (New >= 0)
{
lua_pushstring(LuaVM, "OnConnect");
lua_gettable(LuaVM, LUA_GLOBALSINDEX);
lua_pushnumber(LuaVM, New);
if (lua_pcall(LuaVM, 1, 0, 0) != 0)
{
Errorlog.Write("Error in script: OnConnect call failed");
Serverlog.Write("Error in script: OnConnect call failed");
cout << "Error in script: OnConnect call failed" << endl;
}
}


I hope this helped.

Toolmaker



-Earth is 98% full. Please delete anybody you can.

Share this post


Link to post
Share on other sites
Ironica    128
Oooh, thanks for that, Toolmaker.

What I was planning on doing, was running the Lua script. The Lua script could have some defined functions.
Something like:
l = lua_open();
openstdlibs(l);
lua_dofile(l, INIT_FILE);

Then I could just call:
lua_dostring(l, "myfunction()"); 

to call the function. I have to admit, it does seem kinda messy. Is there anything wrong with it though? I prefer what you said, but this is my first time using Lua, so I''ll have to RTFM a bit more.

Share this post


Link to post
Share on other sites