Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Is there an ACTUAL luabind tutorial


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 falconmick   Members   -  Reputation: 80

Like
0Likes
Like

Posted 05 April 2012 - 02:07 AM

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)

Sponsor:

#2 Antheus   Members   -  Reputation: 2397

Like
0Likes
Like

Posted 05 April 2012 - 09:10 AM

I'm assuming the documentation isn't sufficient?

#3 falconmick   Members   -  Reputation: 80

Like
0Likes
Like

Posted 05 April 2012 - 11:37 AM

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)

#4 e‍dd   Members   -  Reputation: 2105

Like
0Likes
Like

Posted 05 April 2012 - 12:24 PM

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?

#5 falconmick   Members   -  Reputation: 80

Like
-1Likes
Like

Posted 06 April 2012 - 01:35 AM

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:
header:
#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.

#6 falconmick   Members   -  Reputation: 80

Like
-1Likes
Like

Posted 06 April 2012 - 01:48 AM

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

#7 teutonicus   Members   -  Reputation: 518

Like
0Likes
Like

Posted 06 April 2012 - 02:38 AM

Your lua/luabind includes look a bit iffy. Read sections 4 and 5 of this.

#8 falconmick   Members   -  Reputation: 80

Like
-1Likes
Like

Posted 06 April 2012 - 03:08 AM

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

#9 falconmick   Members   -  Reputation: 80

Like
-1Likes
Like

Posted 06 April 2012 - 03:23 AM

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

#10 falconmick   Members   -  Reputation: 80

Like
-3Likes
Like

Posted 06 April 2012 - 04:03 AM

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

#11 e‍dd   Members   -  Reputation: 2105

Like
2Likes
Like

Posted 06 April 2012 - 09:22 AM

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

#12 falconmick   Members   -  Reputation: 80

Like
-1Likes
Like

Posted 06 April 2012 - 09:46 AM

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.

#13 e‍dd   Members   -  Reputation: 2105

Like
0Likes
Like

Posted 06 April 2012 - 10:03 AM

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.

#14 falconmick   Members   -  Reputation: 80

Like
0Likes
Like

Posted 06 April 2012 - 10:26 AM

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

#15 e‍dd   Members   -  Reputation: 2105

Like
0Likes
Like

Posted 06 April 2012 - 10:30 AM

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?

#16 falconmick   Members   -  Reputation: 80

Like
0Likes
Like

Posted 06 April 2012 - 10:40 AM

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

#17 e‍dd   Members   -  Reputation: 2105

Like
4Likes
Like

Posted 06 April 2012 - 11:06 AM

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

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?

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.

#18 falconmick   Members   -  Reputation: 80

Like
-4Likes
Like

Posted 06 April 2012 - 11:15 AM

I would say my problem is more 10% inexperience 10%

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



#19 falconmick   Members   -  Reputation: 80

Like
-1Likes
Like

Posted 06 April 2012 - 11:36 AM

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

lua_close()...


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

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

#20 e‍dd   Members   -  Reputation: 2105

Like
1Likes
Like

Posted 06 April 2012 - 12:14 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS