Page 1 of 1

Lua Simple Implemenation (C/C++)

Posted: Sat Dec 10, 2011 7:16 pm
by Light-Dark
I thought it was about time i contribute something useful to this forum, what i bring you is a hopefully not already done lua implementation tutorial, this isn't going to be to fancy its basicly just a Get value, Do something with it then set it to something new system. My choice of this is because when i was trying to get lua in i found a few tutorials here and there and i after a while of googling i pieced together a nice little system that works for me, i don't know maybe this will ruin the experince for people so read at your own risk.

USEFUL LINKS:
http://www.lua.org/
http://www.lua.org/manual/5.1/#index (This entire page in general is very useful!)

First off lets get lua all setup so get the includes remember to extern c them because the lua API was originally written for C unless your using C of course

Code: Select all

extern "C" {
#include <lua.h>    //Use quotations if < > doesn't work for you
#include <lualib.h>
#include <lauxlib.h>
}
Great now lets get the main state set-up

Code: Select all

lua_State* L = lua_open();
Now open up the lua libraries

Code: Select all

int main()
{
luaL_openlibs(L);
cout << "Lua Libraries Initialized!" << endl;
return 0;
}
If you did it correctly lua should be all nice and set-up but we haven't utilized it yet, now im going to show you how to create C/C++ functions that a lua script can call!
first off create a static int function like so:

Code: Select all

static int RandomLuaFunction(lua_State *L)
{
return 0;
}
ok so now we have a function which takes the the lua state as an arguement, still it won't do anything you can either have it return a value to lua or get a value passed through a lua function or just make it do what ever you want when called from lua here are some examples:

Code: Select all

static int ReturnValFunc(lua_State *L)
{
int x = 10;
lua_pushnumber(L,x);    //Pushes the number out to the function, lua does more than just numbers it can do strings and such!
return 1;   //return 1 arguement
}
static int GetValFunc(lua_State *L)
{
int n = lua_gettop(L);   //Get the ammount of arguements being passed
if(n == AMOUNT YOU WANT){
  GetValFuncVal = lua_tonumber(L,-1) //Get The Last Arguement Being passed
}
else
{
cout << "Error 1 Arguement Expected Got:" << n << endl;
}
return 0;
}
bool Toggle = false;
//This function will be used to represent that you don't always need arguements going back and forth it could be something like a toggle!
static int ToggleExample(lua_State *L)
{
if (Toggle == false)
{
Toggle = true;
}
else
{
Toggle = false;
}
}
Congrats! you have made a function for use in a script, but guess what you still gotta register it or else when you run the script lua will be like, hey man what the fuck are you talking about that function doesn't exist?

so i prefer to split my function registration into a function that will be called during initialization, like so:

Code: Select all

void RegisterLuaFunctions(lua_State *L)
{
lua_register(L,"ReturnVal",ReturnValFunc);
lua_register(L,"GetVal",GetValFunc);
lua_register(L,"Toggle",ToggleExample);
cout << "Lua Functions Registered!" << endl;
}
Alright so were already to go we got all our functions registered so we can start making a lua script, heres an example!

Code: Select all

local x = ReturnVal()
GetVal(x-10)
Toggle()
Save this as a .lua file, now we gotta make the file run in order to use it so make sure you call your registration functions and such in the main function like so:

Code: Select all

int main()
{
luaL_openlibs(L); // Open the libs
RegisterLuaFunctions(L); //Register Functions
luaL_dofile(L,"SCRIPTWITHFILEPATH"); // Will execute a whole script at the designated file path

cout << "Lua Libraries Initialized!" << endl;
return 0;
}
There you go you have all the basics covered for implementing lua in this format, now you dont always have to run a script to do something in lua, you can actually execute strings using the luaL_dostring(L,"LUA STUFF HERE"); function which can be quite useful, example:

Code: Select all

luaL_dostring(L,"Toggle()");
This will call the toggle function we created instead of you having to create a whole new script for it you can just call it using this convient function!.

And lastly don't forget to close lua when your done using the lua_close function:

Code: Select all

lua_close(L);
=====CONCLUSION=====

Im aware this may not be the best way to have lua in but hey it has worked for me, anyways this is the bare minimum of what you could be doing if you want more information such as more luaL_ and lua_ functions check out the links above they provide pretty good documentaion if you don't understand or think they are to vague just GOOGLE it google is bound to be your best option and will yield at least some useful results. Regardless i hope this helps you with lua implementation and i hope its not to terribly flawed XD and have fun scripting!

Re: Lua Simple Implemenation (C/C++)

Posted: Sun Dec 11, 2011 12:24 am
by TheBuzzSaw
In C++, you can just #include <lua.hpp>. They thought of the extern nonsense ahead of time. :)

Re: Lua Simple Implemenation (C/C++)

Posted: Sun Dec 11, 2011 12:38 am
by Light-Dark
TheBuzzSaw wrote:In C++, you can just #include <lua.hpp>. They thought of the extern nonsense ahead of time. :)
Hmmm never knew that, i outta try it out :)