End of my tether with simple luaBind stuff!

Started by
7 comments, last by NovaBlack 15 years, 4 months ago
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.
Advertisement
hmm i just tried going back to including all the luabind h n cpp files seperately (lovely long compile times and warnings yay!) rather than using the lib.

and i CAN register functions without crashing now.. so i gues its something wring with my lib? (does that sound about right?). Im fairly new to using dlls and libs tbh, so although i thought i built the lib correctly (and it seems to work for everything other than function binding) i perhaps may not have.

Dont suppose anyone could walk me through how to do it in VS2005?
http://www.rasterbar.com/products/luabind/docs.html

That short, but rather useful tutorial might be of some use. Try to compare your stuff with that :)
http://sourceforge.net/projects/pingux/ <-- you know you wanna see my 2D Engine which supports DirectX and OpenGL or insert your renderer here :)
cheers, yeah i tried that earlier to build my lib. Set all teh environment variables like it says and managed to build a lib, but that was the one that (although 99.9% worked) crashed when i tried to register functions.

so frustrating!
I suggest trying luabind 0.7.1 as a first step. This is a very simple usage which is obviously tested, so it should work.
I had the same problem some time ago, in the end, my lib had been badly compiled
as you can see here. I downloaded a pre build lib from here.
cheers for the advice. (ill dl that lib and give it a try!)

The wierd thing is that although its now working (its currently working using manual inclusion and compilation of all luabind .ccp and .h files) .. i just went back to a sample project from matt buckland's programming game AI by example book. I downloaded the sample solution. (its a simple FSM with a miner using a scripted state machine)

In his sample solution he uses a luabind.lib to access the luabind functionality. WIERDLY though, i tried running it nd could NOT get it to run. After spending (too much) time, i tracked down that a crash would happen at runtime when a function was registered, but only if that function was one which accepted a parameter. Essentially i noticed by commenting out all the setters and just leaving the getters, i could get it to successfulyl run the code to register stuff.

[lang = "cpp"]void RegisterMinerWithLua(lua_State* pLua){  module(pLua)    [         class_<Miner, bases<Entity> >("Miner")        .def("getGoldCarried", &Miner::GoldCarried)         //.def("SetGoldCarried", &Miner::SetGoldCarried)  // uncomment causes crash here         //.def("AddToGoldCarried", &Miner::AddToGoldCarried)// uncomment causes crash here        .def("isFatigued", &Miner::Fatigued)        .def("DecreaseFatigue", &Miner::DecreaseFatigue)        .def("IncreaseFatigue", &Miner::IncreaseFatigue)         .def("getFSM", &Miner::GetFSM)    ];  }



Obviously the actual run script stuff couldnt be tested, since the setters werent registered..

However, i tried commenting out the original project lines that included the lib, and included all the h and cpp files manually. And... hey presto.. i could uncomment the lines and everythign worked...

Is there some strange known problem when cramming luabind into a static lib???
I mean even the buckland book example didnt work with a supplied lib. So i guess i didnt compile my original lib incorrectly , as i got the same error.







Luabind is dependent upon the Boost library which inturn is dependent upon the STL library, both are heavily templated libraries. Staic linking of templated libraries are known to have incompatiblity issues unless all the templated implementations are exactly the same (on your side and the static libary), which you can't guarnetee since u really don't know the exact state in which a libary was compiled (with respect to other dependent templated libraries, there is no common versioning protocol), Boost for instance has many versions and linking to a incompatibale Boost version would lead to instant crashing or worse hidden random crashing.

Also Luabind ofcoruse has many versions as well, linking to an incompatiable version would also lead to the crashes your seeing. With templated libraries like Luabind, its best to include it manually in your project, that way you're guarnetted to have the correct version and interfaces with the same STL and Boost versions as your code.

Good Luck!

-ddn
Quote:Original post by ddn3
Luabind is dependent upon the Boost library which inturn is dependent upon the STL library, both are heavily templated libraries. Staic linking of templated libraries are known to have incompatiblity issues unless all the templated implementations are exactly the same (on your side and the static libary), which you can't guarnetee since u really don't know the exact state in which a libary was compiled (with respect to other dependent templated libraries, there is no common versioning protocol), Boost for instance has many versions and linking to a incompatibale Boost version would lead to instant crashing or worse hidden random crashing.

Also Luabind ofcoruse has many versions as well, linking to an incompatiable version would also lead to the crashes your seeing. With templated libraries like Luabind, its best to include it manually in your project, that way you're guarnetted to have the correct version and interfaces with the same STL and Boost versions as your code.

Good Luck!

-ddn



lol im actually glad you said what you did!! I have been manually including it so far so it means im actually doing the right thing!!

The main thing is that at least i know im not going mad! (it was driving me crazy, and was pretty darn random)

thanks for the pearls of wisdom, REALLY appreciated to save me from banging my head against a brick wall any longer, it is rather sore!

Cheers everyone!

This topic is closed to new replies.

Advertisement