# Is there an ACTUAL luabind tutorial

This topic is 2117 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ive spent... countless.. COUNTLESS, hours attempting to learn luabind.. but there is Nothing out there... and if there is it is a minute amount of information...

What I would really kill for is a working example I could pick apart and make work for my needs.

What I am trying to do is expose a class so that I can call a function inside of it, passing an int and a string... My issue is that I have to pass in two maps to the creation of the class.. So I pretty much need to be able to pass in objects... but when ever I use luabind::gobals() I get exception problems

So what I wanted to know.. Is there any resource out there which will help me learn how to expose a class and pass objects to it.
(hmm, I mean pass object references, as I need to be able to access the data in them after the exposed class has modified them)

##### Share on other sites
I'm assuming the documentation isn't sufficient?

##### Share on other sites

I'm assuming the documentation isn't sufficient?

Not really... I am slowly getting a hold of it, but without other teams at my uni I wouldn't be able to do it.. the biggest thing I needed, luabind::globals has almost no info on it in the documentation, If I get to fully understand it I might make a guide (after this semester)

##### Share on other sites
The docs says luabind::globals() returns the global environment table, which is a luabind::object. Section 11 of the docs gives you a synopsis of luabind::object's interface. Is there anything else you need?

##### Share on other sites
for instance... I cannot find a way in hell to fix this error:

CompCompiler.cpp 1>g:\crsm-svn\luatesting\externallibraries\include\luabind\detail\call.hpp(293): error C2027: use of undefined type 'lua_State' 1> g:\crsm-svn\luatesting\externallibraries\include\lua.h(50) : see declaration of 'lua_State' 1> g:\crsm-svn\luatesting\externallibraries\include\luabind\detail\call.hpp(89) : see reference to function template instantiation 'int luabind::detail::invoke_normal<F,boost::mpl::vector3<T0,T1,T2>,Policies>(lua_State *,const luabind::detail::function_object &,luabind::detail::invoke_context &,const F &,Signature,const Policies &,boost::mpl::long_<N>,boost::mpl::true_)' being compiled 1> with 1> [ 1> F=luabind::detail::construct<construct_type,pointer,signature>, 1> T0=void, 1> T1=const luabind::adl::argument &, 1> T2=lua_State, 1> Policies=luabind::detail::null_type, 1> Signature=boost::mpl::vector3<void,const luabind::adl::argument &,lua_State>, 1> N=2 1> ] ......... ........

which is being caused by this class:
#pragma once #include <lua.hpp> #include <luabind/luabind.hpp> class CompCompiler { public: CompCompiler(); ~CompCompiler(); void setSomeData(int dataz); void setMoreData(bool statezzz); int getSomeData(){return(someData);}; bool getMoreData(){return(moreData);}; void setupLuaBinds(lua_State *L); private: int someData; bool moreData; }; 
implementation:
#include "CompCompiler.h" CompCompiler::CompCompiler() { someData = 2; moreData = true; } void CompCompiler::setupLuaBinds(lua_State *L) { luaL_openlibs(L); luabind::open(L); luabind::module(L) [ luabind::class_<CompCompiler>("CompCompiler") .def(luabind::constructor<lua_State>()) .def("setSomeData",&CompCompiler::setSomeData) .def("setMoreData", &CompCompiler::setMoreData) .def("getSomeData", &CompCompiler::getSomeData) .def("getMoreData", &CompCompiler::getMoreData) ]; lua_close(L); } CompCompiler::~CompCompiler() { } 

Now, the reason why I am getting this error is probably something trivial.. but as I have no knowledge in luabind I am hitting a brick wall.

I thought, hey maybe it's because inside of a constructor lua_state is unacceptable for some stupid reason, bummer, now my neat idea ain't gonna be as need as I wanted it to be, but when I changed my code (to what you see there) so that lua_state wasn't in the compiler, it was in a fully compiled class, it still gets the error -.-
so apparently atm luabind is angry at me for passing a lua_state... can you even pass a lua_state?

So I am stuck at a brick wall... I am trying to keep my LuaInstances class as abstract as possible (i.e. all it does is hold and distribute the states, the classes using them initiate their part in a given instance (yes I know, if I wanted to be fully abstract I would make a separate class for initiating the class being binded to luabind, but I can't be bothered) can you not do this?

I am trying to make it so that there are multiple instances of lua_state so that I can keep my lua states COMPLETLY seperate so that when who ever is writing the scripts, is writing the scripts, they only have access to a portion of the functions in my code for a given task.. I don't want somone who is changing key bindings having access to entity management -.-.. I considered namespaces, but people would (to the best of my knowledge) be able to access the functions/globals I don't want them having access to.

##### Share on other sites
btw, if anyone wants to try to help me, I get these more easy to read errors:

Error 1 error C2027: use of undefined type 'lua_State' g:\crsm-svn\luatesting\externallibraries\include\luabind\detail\call.hpp 293

Error 2 error C2664: 'void luabind::detail::construct_aux<Arity,T,Pointer,Signature>::operator ()(const luabind::adl::argument &,lua_State) const' : cannot convert parameter 2 from 'a1' to 'lua_State' g:\crsm-svn\luatesting\externallibraries\include\luabind\detail\call.hpp 294

##### Share on other sites
Your lua/luabind includes look a bit iffy. Read sections 4 and 5 of this.

##### Share on other sites
ive build a working program using these libraries before, I will though look at my other version which isn't statically linked

edit:

nope, that wasn't the problem, and both of these builds have worked in different situations

edit:
I think I know the problem, it is a static class, therefore accessing it's includes might cause derp

##### Share on other sites
turns out error is hear:
luabind::module(L) [ luabind::class_<CompCompiler>("CompCompiler") .def(luabind::constructor<lua_State>()) .def("setSomeData",&CompCompiler::setSomeData) .def("setMoreData", &CompCompiler::setMoreData) .def("getSomeData", &CompCompiler::getSomeData) .def("getMoreData", &CompCompiler::getMoreData) ];

 .def(luabind::constructor<lua_State>()) 

originally the constructor TRIED to accept a lua_state in the constructor, I changed. sigh... the error message didn't help me at all

edit:
lol one problem fixed, another pop's its head up

##### Share on other sites
seriously.. anybody out there with ANY knowledge of luabind PLEASE PLEASE help... I have no idea how it works and now I'm getting errors which are either me getting pissed off or luabind being a peice of *(@T... I have 2 lua states being held in a map... I used .find(instEnum)->second to access them.. but when ever I pass this back through a function it causes access violations

 First-chance exception at 0x0009facd in LuaInstances.exe: 0xC0000005: Access violation reading location 0xfeeeff32. Unhandled exception at 0x77df15ee in LuaInstances.exe: 0xC0000005: Access violation reading location 0xfeeeff32. 

I know its probably somthing so soo simple.. but I am dead.. luabind is making me want to murder ducklings

##### Share on other sites
Take it slower. Bind one piece of functionality at a time, test it and then add the next bit. I get the impression you're making this far harder than it needs to be. Why is it that you're passing a Lua_State to the constructor?

The first step I think, would be:

 module(L) [ class_<CompCompiler>("CompCompiler") .def(constructor<>()) ]; 

Try that. Fix any typos/errors. Then add one (and only one) additional method:

 module(L) [ class_<CompCompiler>("CompCompiler") .def(constructor<>()) .def("setSomeData", &CompCompiler::setSomeData) ]; 

Try that. Fix any typos/errors. Add the next method...

##### Share on other sites
I'm no longer passing a lua_state to the constructor... I decided that that would be the problem 100 problems ago... still get errors -.-

Now I think it might be that putting it into a map is a bad idea, gonna try not using a map.

##### Share on other sites
Have you tried the smallest possible example, and built up from there, as suggested? I get the impression that you're your own worst enemy at the moment.

##### Share on other sites
I have started from scratch 5 times now... every time... access violations... there is NO information on how to build actual systems with luabind, rather than just toys...

##### Share on other sites
The Luabind documentation has been used to build "actual systems". I know it's hard to believe, but there's the tiniest chance that there's a bug somewhere in your code.

Ok, so taking the smallest example I gave, run it through your debugger until the point of failure. What does it tell you?

##### Share on other sites
ok, this is what is causing the problem.. maybe if u know luabind u can help...
I have multiple lua_States... This is because I don't want those who write scripts to be able to call functions that were not designed for execution during what they are doing, for example, I don't want the constructor calling keybinding configuration variables... to combat this I made an array of lua_State's
#HEADER# lua_State **states; #IMPLIMENTATION# LuaInstances::LuaInstances() { states = new lua_State*[10]; // increase this to add more states states[construction] = lua_open(); states[keyboard] = lua_open(); .....
when I debug, I can see that there are two diferent pointers inside of the states array...
then I call:
 CompCompiler *example; example = new CompCompiler(); 
which calls:
 void CompCompiler::setupLuaBinds(lua_State *L) { luaL_openlibs(L); luabind::open(L); luabind::module(L) [ luabind::class_<CompCompiler>("CompCompiler") .def(luabind::constructor<>()) .def("setSomeData",&CompCompiler::setSomeData) .def("setMoreData", &CompCompiler::setMoreData) .def("getSomeData", &CompCompiler::getSomeData) .def("getMoreData", &CompCompiler::getMoreData) ]; lua_close(L); } 

This executes fine...

this is the problem:
 luabind::open(L); luabind::globals(L)["CompCompiler"] = example; lua_close(L); 
when I call luabind::open(L); it causes an Unhandled exception.. if I remove the lua_close(L); at the end of void CompCompiler::setupLuaBinds(lua_State *L), and remove the subsequent luabind::open(L); it goes through without a hitch... but I need to be able to close and open luabind states so I can keep the tools in which users use in a lua restricted... maybe namespaces can do this.. but I don't know.. because theres not enough documentation on it.

do you have ANY idea how I can setup a system which has multiple lua states so that I am able to restrict access to globals and functions when executing a lua file or a lua string?

EDIT:
so all of it works when I don't close the lua state.. so I know that my code is working (ive tested that it executes a lua, and it does, it even modifies the objects I give it) its just that I can't open/close a lua state, which I kinda need to be able to do

##### Share on other sites

I have multiple lua_States... This is because I don't want those who write scripts to be able to call functions that were not designed for execution during what they are doing, for example, I don't want the constructor calling keybinding configuration variables...

[...]

when I call luabind::open(L); it causes an Unhandled exception.. if I remove the lua_close(L); at the end of void CompCompiler::setupLuaBinds(lua_State *L), and remove the subsequent luabind::open(L); it goes through without a hitch...

Right, because closing the lua state destroys it, as it says in the documentation. It's akin to trying to use a FILE* after you've called fclose() on it.

but I need to be able to close and open luabind states so I can keep the tools in which users use in a lua restricted...
[/quote]
But I thought this was the point of using different Lua states? What has closing and re-opening states got to do with segregation of functionality?

do you have ANY idea how I can setup a system which has multiple lua states so that I am able to restrict access to globals and functions when executing a lua file or a lua string?
[/quote]
Keep them open.

Your problem is with your understanding of Lua and nothing at all to do with Luabind, it seems. I think the problem is 50% attitude and 50% inexperience. Complaining about the quality of things you're misusing or haven't taken the time to understand isn't going to help your case.

So, take a deep breath, do things one step at a time (don't just pretend to do so to get more help). Understand each step before proceeding to the next. Run the code each time and check it works. Look at all the Lua/Luabind functionality you're using and understand what it does before proceeding. Luabind is an advanced library. It's going to eat people that don't yet have much C++ experience under their belts if they're not methodical about how they use it.

To this end, I recommend you step sideways temporarily and try to do something smaller before coming back to your main project. For example, create a program with 2 Lua states. Bind one C(++) function in to each state (a different function in each) and check neither Lua state can see the function bound in to the other by running some snippets of Lua code that call those functions.

When you're happy that you've got that working, return to the main project and incrementally build it up.

May I also recommend an std::vector<lua_State *> to hold your states.

##### Share on other sites
I would say my problem is more 10% inexperience 10% [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

### attitude and 80% stress because the assignment (game engine) is due in a week... I havn't been putting it off.. it's just a stupidly massive assignment, I've been working on for 2 months[/font]

##### Share on other sites
thanks for your help, I am fairly sure that the problem (I'm almost certain) that is was

lua_close()...

[font=helvetica, arial, verdana, tahoma, sans-serif][color=#282828]

##### Share on other sites
I can't comment on the down vote in your last comment (wasn't me), but there seems to be a theme to your posts: blaming someone else. Even in your apology, you implicitly blame your teacher.

Anyway, I'm not great at dealing with stress either, so I empathise somewhat. But what I'd recommend is doing the simplest thing that will work and refining that code if you have time left at the end. This will help build momentum, which in turn will relieve some of the stress.

For example, I'd assume that the Lua sandboxing you're attempting here isn't actually necessary for your assignment, but could be a nice extra you could add at the end i.e. a single Lua state is probably sufficient for everything, even if it's not the ideal solution.

##### Share on other sites
Nah, it is a requirement... I could do a bad job at it so our team doesn't fail, but I take pride in my work, so I wanted a useful system.
anyway, I think that all my problems came from not knowing enough about lua and calling
lua_close(L);

thinking it did something else

I should be able to get it working how I need now, once again, thanks for your help

edit:
doesn't everone know, it makes u feel better when you blame someone else