Lua and C++

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

spum
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Dec 23, 2008 10:41 pm

Lua and C++

Post 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

Code: Select all

 #include "lua.h"
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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Lua and C++

Post by Falco Girgis »

Yes, of course.
spum
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Dec 23, 2008 10:41 pm

Re: Lua and C++

Post 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

Code: Select all

print("Hello World")
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.
User avatar
hellknight
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 37
Joined: Fri Apr 24, 2009 2:54 am
Current Project: Non
Favorite Gaming Platforms: Pc, NES, PS1, PS2, xBOX
Programming Language of Choice: c,c++,c#,JS, haskell
Location: India

Re: Lua and C++

Post 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)
Dont Mind the typos...i m too lazy to correct them. :P
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Lua and C++

Post by Falco Girgis »

I'm pretty sure it returns a bool.
User avatar
hellknight
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 37
Joined: Fri Apr 24, 2009 2:54 am
Current Project: Non
Favorite Gaming Platforms: Pc, NES, PS1, PS2, xBOX
Programming Language of Choice: c,c++,c#,JS, haskell
Location: India

Re: Lua and C++

Post 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)
Dont Mind the typos...i m too lazy to correct them. :P
spum
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Dec 23, 2008 10:41 pm

Re: Lua and C++

Post 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++
User avatar
hellknight
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 37
Joined: Fri Apr 24, 2009 2:54 am
Current Project: Non
Favorite Gaming Platforms: Pc, NES, PS1, PS2, xBOX
Programming Language of Choice: c,c++,c#,JS, haskell
Location: India

Re: Lua and C++

Post 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.
Dont Mind the typos...i m too lazy to correct them. :P
spum
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Dec 23, 2008 10:41 pm

Re: Lua and C++

Post 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
User avatar
hellknight
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 37
Joined: Fri Apr 24, 2009 2:54 am
Current Project: Non
Favorite Gaming Platforms: Pc, NES, PS1, PS2, xBOX
Programming Language of Choice: c,c++,c#,JS, haskell
Location: India

Re: Lua and C++

Post 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
Dont Mind the typos...i m too lazy to correct them. :P
User avatar
Zer0XoL
ES Beta Backer
ES Beta Backer
Posts: 54
Joined: Fri Apr 24, 2009 1:18 pm
Current Project: Zelda untitled multiplayer game
Favorite Gaming Platforms: PC, GBA, N64
Programming Language of Choice: C++, Lua
Location: Sweden
Contact:

Re: Lua and C++

Post 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.. :)
Image
Im Blue
User avatar
szdarkhack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 61
Joined: Fri May 08, 2009 2:31 am

Re: Lua and C++

Post 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.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Lua and C++

Post 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 :lol:
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Lua and C++

Post 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 :lol:
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.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Lua and C++

Post 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)
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
Post Reply