🎉 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!

Basic Lua Interpreter

Started by
5 comments, last by Ironica 20 years, 11 months ago
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 =)
Advertisement
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.
dofile() will load and then execute the file as a chunk, from the docs
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
Love & Peace ^_^vCapt. Jean-Luc Pikachu
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 =)
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 filebool 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.

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.

This topic is closed to new replies.

Advertisement