Page 1 of 2
Lua and C++
Posted: Thu Apr 23, 2009 9:34 pm
by spum
ok i have a question about using lua with C++. Most of the online books and tutorials talk about using lua with "C" and not C++. I'd like to know is there a way to use Lua with the source code in the books and instead of including the headers like this
can i just call them like this
Code: Select all
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
and still be able to use the source from those books. an example of one of the books is Beginning Lua Programming.
Re: Lua and C++
Posted: Thu Apr 23, 2009 10:26 pm
by Falco Girgis
Yes, of course.
Re: Lua and C++
Posted: Fri Apr 24, 2009 12:16 am
by spum
well i guess ppl are going to be looking here because they want tutorials so i shall entertain them. First off if you don't already know lua is bad-ass, ok you can make it suck your programs dick because it does what ever you want. ok so lets show you ppl how to use it with C++.
Ok so to begin. you need to download the lua library which can be found at
http://www.lua.org/download.html then you need to set it up the same way you do with SDL. If you don't already know how to do that then i will explain if for Visual C++ but if your using Dev C++ then you can go to lazyfoo.com and follow the SDL set-up tutorial but do it with lua. so here it goes
Step 1: In Visual C++ click on "tools" then "options". after that you need to expand Project Solutions and click on "VC++ Directiories" and in the drop down box click on include files and make a new directory and direct it to the include file that came wiht the Lua 5.1 download. Next do the same with the library files, and direct it to the lib folder.
Step 2: Now since your compiler knows where to look for the lua files you need to set-up your project to be able to use it. So in your Project properties expand Configuration settings, expand C/C++ and click on "code generation" and next to runtime library change it from multi-threaded debuggin DLL to multi-threaded DLL. Now expand Linker and click on input and next to additional dependencies type in lua5.1.lib
For the last part of Step 2 copy the 2 .dll files from the lib folder in lua (if you installed lua with default settings then it should be in C:\Program Files\Lua\5.1\lib. and paste them in the same folder as your project executable. Now your ready to use lua and its library.
NOTE* you will have to repeat step 2 for every project
Step 3: the actual code.
ok first copy and paste this into notepad and save it as hello.lua
Now copy and past this code into your main.cpp.
Code: Select all
#include <stdio.h>
#include <iostream>
using namespace std;
//this part is very important since lua is written in C we have to include it as C but if you want to use straight C instead of C++ then just remove the extern "C". but other than that we're including the lua header files for the calls to lua such as lua_open()
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
//This is the lua interpreter you need this for your program to understand the lua
//functions that you use.
lua_State* L;
int main ( int argc, char *argv[] )
{
//here we initilize lua with lua_open();
L = lua_open();
//here we open the libraries with luaL_openlibs(L)
luaL_openlibs(L);
//this is where we call the lua script for the lua interpreter to interpert
luaL_dofile(L, "hello.lua");
//and with lua_close(L); we clean up lua because its been a dirty whore
lua_close(L);
//and here you should recognize this peice of code ^_^
cout << "Press enter to exit" << endl;
getchar();
return 0;
}
ok so i hoped you liked this tutorial on intergrating lua with C++. i'll be posting later tutorials on how to code in lua, the real treat with lua is being able to call C++ functions straight from lua. so hope to see you next time.
Re: Lua and C++
Posted: Fri Apr 24, 2009 3:25 am
by hellknight
Superrrrrr..... :P just made the lua code run from my c++ program...... weeeeeeee
(P.S. seems like "luaL_dofile(L, "hello.lua");" doesnt give u warning if it doesnt find the "hello.lua",just copy the script next to ur main file. :P)
Re: Lua and C++
Posted: Fri Apr 24, 2009 11:17 am
by Falco Girgis
I'm pretty sure it returns a bool.
Re: Lua and C++
Posted: Fri Apr 24, 2009 12:28 pm
by hellknight
ohhh damn....i was thinking to it throwing something....my bad.......
(me->remeberCstyleCoding()....ok now i m good :P.)
just saw something very strange...(P.S. i m new to lua...so help me out plz :P)
if the source lua file is ok,only then it gets executed.(and i thought interpreters work one line at a time....nvr md me)
......so how does that api actually work?.......does the lua file get converted to a compiled code first or it is just interpreted on fly? and if it happens on the fly,then how can it know that the source lua file is "bad"? is it possible to modify the lua source file during run time?
(just saw the video where falco created that lua debug window..and was wondering how he did it........my guess was to create a lua file (empty if possible). make it call every frame during the "heart beat" (as falco says).then use a saprate thread to modify its content (yeah i know syncronisation problem :P),but will it work?
(P.S i have this annoying problem of putting "....." during chating ,forums etc.....dont mind me)
Re: Lua and C++
Posted: Fri Apr 24, 2009 1:13 pm
by spum
bassically what happens is we load the lua script with luaL_dofile(L, "hello.lua") what this does is not only loads it. but its able to execute the code becuase the L's in the dofile point to the lua interpreter we declared in the top of the prgram lua_State* L . the lua interpreter executes the lua file and then goes onto the rest of the program. its almost like a function in C++
Re: Lua and C++
Posted: Fri Apr 24, 2009 10:55 pm
by hellknight
spum wrote:bassically what happens is we load the lua script with luaL_dofile(L, "hello.lua") what this does is not only loads it. but its able to execute the code becuase the L's in the dofile point to the lua interpreter we declared in the top of the prgram lua_State* L . the lua interpreter executes the lua file and then goes onto the rest of the program. its almost like a function in C++
hmm...seems like there is a checking of the lua file when it is run. Interpreter checks it to be valid lua file,only then executes.
Re: Lua and C++
Posted: Sun Apr 26, 2009 2:12 pm
by spum
ok thats fine, but i'm still learning lua as well so i expect to be corrected if i'm wrong. But i should have another tutorial soon about calling lua functions from C/C++ and calling C/C++ functions from lua(which in my opinion is the pride and joy of lua
Re: Lua and C++
Posted: Sun Apr 26, 2009 11:01 pm
by hellknight
spum wrote:ok thats fine, but i'm still learning lua as well so i expect to be corrected if i'm wrong. But i should have another tutorial soon about calling lua functions from C/C++ and calling C/C++ functions from lua(which in my opinion is the pride and joy of lua
coooooool..........i will be waiting. :D
Re: Lua and C++
Posted: Sun May 03, 2009 1:23 am
by Zer0XoL
hellknight wrote:spum wrote:ok thats fine, but i'm still learning lua as well so i expect to be corrected if i'm wrong. But i should have another tutorial soon about calling lua functions from C/C++ and calling C/C++ functions from lua(which in my opinion is the pride and joy of lua
coooooool..........i will be waiting. :D
I will also be waiting..
Re: Lua and C++
Posted: Fri May 08, 2009 2:39 am
by szdarkhack
hellknight wrote:ohhh damn....i was thinking to it throwing something....my bad.......
(me->remeberCstyleCoding()....ok now i m good :P.)
just saw something very strange...(P.S. i m new to lua...so help me out plz :P)
if the source lua file is ok,only then it gets executed.(and i thought interpreters work one line at a time....nvr md me)
......so how does that api actually work?.......does the lua file get converted to a compiled code first or it is just interpreted on fly? and if it happens on the fly,then how can it know that the source lua file is "bad"? is it possible to modify the lua source file during run time?
(just saw the video where falco created that lua debug window..and was wondering how he did it........my guess was to create a lua file (empty if possible). make it call every frame during the "heart beat" (as falco says).then use a saprate thread to modify its content (yeah i know syncronisation problem :P),but will it work?
(P.S i have this annoying problem of putting "....." during chating ,forums etc.....dont mind me)
I'm pretty sure that when compiling lua as C++ code, exceptions are enabled as the main error handling mechanism (it checks for #ifdef __cplusplus)
But check the main lua site to be sure.
Re: Lua and C++
Posted: Fri May 08, 2009 4:23 am
by M_D_K
hellknight wrote:ohhh damn....i was thinking to it throwing something....my bad.......
(me->remeberCstyleCoding()....ok now i m good :P.)
just saw something very strange...(P.S. i m new to lua...so help me out plz :P)
if the source lua file is ok,only then it gets executed.(and i thought interpreters work one line at a time....nvr md me)
......so how does that api actually work?.......does the lua file get converted to a compiled code first or it is just interpreted on fly? and if it happens on the fly,then how can it know that the source lua file is "bad"? is it possible to modify the lua source file during run time?
(just saw the video where falco created that lua debug window..and was wondering how he did it........my guess was to create a lua file (empty if possible). make it call every frame during the "heart beat" (as falco says).then use a saprate thread to modify its content (yeah i know syncronisation problem :P),but will it work?
(P.S i have this annoying problem of putting "....." during chating ,forums etc.....dont mind me)
dude its called
luaL_dostring. Sorry Falco you've no longer got the edge
Re: Lua and C++
Posted: Fri May 08, 2009 10:39 am
by Falco Girgis
M_D_K wrote:hellknight wrote:ohhh damn....i was thinking to it throwing something....my bad.......
(me->remeberCstyleCoding()....ok now i m good :P.)
just saw something very strange...(P.S. i m new to lua...so help me out plz :P)
if the source lua file is ok,only then it gets executed.(and i thought interpreters work one line at a time....nvr md me)
......so how does that api actually work?.......does the lua file get converted to a compiled code first or it is just interpreted on fly? and if it happens on the fly,then how can it know that the source lua file is "bad"? is it possible to modify the lua source file during run time?
(just saw the video where falco created that lua debug window..and was wondering how he did it........my guess was to create a lua file (empty if possible). make it call every frame during the "heart beat" (as falco says).then use a saprate thread to modify its content (yeah i know syncronisation problem :P),but will it work?
(P.S i have this annoying problem of putting "....." during chating ,forums etc.....dont mind me)
dude its called
luaL_dostring. Sorry Falco you've no longer got the edge
Eh, whatever. I'm still the original badass.
And it's not so much about the function that calls it as much as having a Lua system that is integrated badassed-ly enough to allow you to do cool things from a debug prompt. That's the real challenge.
Re: Lua and C++
Posted: Fri May 08, 2009 1:05 pm
by M_D_K
GyroVorbis wrote:
And it's not so much about the function that calls it as much as having a Lua system that is integrated badassed-ly enough to allow you to do cool things from a debug prompt. That's the real challenge.
That is so true dude.
Now I seem to remember something about the "original badass" complementing me on my clever/sneaky attitude to my console(I have logs too XD)