Is there an ACTUAL luabind tutorial

Started by
19 comments, last by MichaelCrook 12 years ago
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...
Advertisement
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.
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.
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...
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?
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

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.
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]

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]

In my defence though.. I am attempting to learn how to set up lua binding with my only experience being fiddling with lua code in WoW addons.. I don't really count that though as everything I did was more writing lua, not implementing it. I just wish there was a better guide out there :\ and I sware I looked up every function I called and could never find [/font]


http://www.lua.org/m....html#lua_close.. I must just be blind...

The best way to learn lua and luabind I would say is NOT through beating your head against the wall as fast as possible till you break the wall down, probably stepping through the door (i.e. learn lua, then learn luabind) but I just don't have time... haha I should upload the work sheet we were given in which to learn lua and impliment it into a project.. the funniest thing about it was that the version of luabind they gave us was bugged + not statically linked... when ever you modified a file that had included luabind it would corrupt the link and you would have to clean/rebuild... so I had to learn how to build lua + boost + luabind... The worst part is that it is a requirement for the assignment, but when I ask my tutor/lector for help in lua/luabinding they couldn't, I had to ask my class mates who did it last semester (and they had just as much joy initially)

edit:
hahaha now I understand why I was getting access violations, lua had free'd up the space, but I still had a pointer to that space #DOH!#

edit 2:
it's sad that a random interwebs person is a better teacher than my teacher -.-
they should just fire the lecturer and the tutor guys and employ google full time

any particular reason for a -1 on this?
I am admitting my fault + thanking you?
:( you gotta understand I am stressing a lil atm

but I am defiantly gonna make a tutorial when I finish this unit, hell, if I help one person it will be worth it
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.

This topic is closed to new replies.

Advertisement