Hey guys. Been doing some basic lua exercises since about thursday last week, just to get me familiar with its capabilities before integrating a script manager with my engine. Im using the latest version of Lua (5.1 iirc)
Basically the lua stuff has gone fine, not had any problems using lua itself, and the language is super easy to pick up (annoying debugging syntax errors though!).
I went as far as using the virtual stack to push and pop vars, so that a) C++ could access vars in a lua file (worked fine) and b) lua could call functions from c++ (again worked fine, called cpp_add()/cpp_subtract()/cpp_hello world() from within my lua script. Although it was rather tedius and error prone creating wrappers, and then pushing / popping vars etc each time, and returning vars in the right order / right number of args!
Then i read about classes having to be bound to a table to define its structure and functionality being even more a pain in the ass.
But wait! LuaBind to the rescue! (so matt Buckland claims on p276 of programming game AI by example)
So i dlded the luabind h and cpps (luabind 0.7), and included them directly into my project, and all seemed to be going well apart from every time i compiled id get a lot of warnings from things within the luabind code, and it took ages which was REALLY annoying quickly. SO as suggested i compiled the files to a static lib. I included the lib and now compiling was much faster.
I did a basic example whereby i used luabind to declare a class 'animal' and a class 'dog' , and it seemed to work fine! i created them in c++, registered them with luaBind, and could successfully create and use them from within my luaScript.
However, i then came to (what i thought should surely be simpler) binding a function declared in C++, so that it could be called in luabind. Ive followed examples in buckland's book to the letter ( i mean its an example calling helloWorld() (defined in c++) from your lua script for gods sake it doesnt get much simpler than that!)
However, although my example compiles, when my code reaches a certain line (end of registering the function) it crashes and takes me to an point in a luabind file 'function.cpp' (ln 33) :
std::vector<overload_rep>::iterator i = std::find(
m_overloads.begin(), m_overloads.end(), o);
i totally dont know what im doing wrong!!! (it doesnt even get to the line executing the script so i know it isnt that). Its just wierd that i can use classes fine (and class functions) but cant seem to register standalone functions.
here is my very simple code!!! (ignore the syntax errors on teh includes, it copied badly! but they work fine)
#include <boost/lambda/lambda.hpp>
#include <iostream>
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#pragma comment(lib, "lua5.1.lib")
#pragma comment(lib, "luabind/luabindlib.lib")
#include <string>
using namespace std;
#include <luabind/luabind.hpp>
using namespace luabind;
void HelloWorld()
{
std::cout << "[C++]: Hello World!";
}
int main()
{
// reate a lua state
lua_State* pLua = lua_open();
// Open the lua libaries - new in lua5.1
luaL_openlibs(pLua);
// Open luabind
luabind::open(pLua);
// declare function interface to lua
module(pLua)
[
def("HelloWorld", &HelloWorld)
]; /* crashes around here (steps into code in luabind, callbrowser traced back to here)*/
//load and run the script here (doesnt get this far)...
//..tidy up, close lua, return
}
id REALLY appreciate any help. I have to use this for my 3rd year project (it plays a pretty important part), and ive pretty much spent about 24 hours now trying to get luabind to work.