Re: Trivial pointers result in unregistered class
Daniel Walter <[email protected]> Tue, 18 Jun 2013 19:11:52 +0200
| Newsgroups | gmane.comp.lang.lua.bind.user |
|---|---|
| Organization | Helm & Walter IT-Solutions |
| Message-ID | <[email protected]> |
I think this does not fit my needs. "FooManager" does not have any
public pointers that need to be accessed directly by lua. You are
accessing your pointers through "getComponent" function.
Can you expose "m_iFooVector" with something like this:
".def_readonly("iFooVector", &FooManager::m_iFooVector,
luabind::return_stl_iterator)"
And then iterate and access it in your lua script?
for foo in fooManager.iFooVector do
print(foo.AnyField)
end
On 18.06.2013 19:01, rod haxton wrote:
> Take a look at this. I do this and it works with a boost::ref on class,
> see "FooManager".
>
> I know...I know...I hate when folks give you a lot of code to look at
> for an example. But, perhaps you can glean something from looking at the
> "FooManager" class, and see that I assign it as a global.
>
> //The registrate function takes a Class
> // name and lua_State, then calls its registrate method
> template <typename T>
> void registrate(lua_State *pLuaState) {
> T::registrate(pLuaState);
> }
>
>
> class FooManager
> {
> public:
> FooManager() {};
> ~FooManager() {};
>
> voidaddComponent(IFoo* pFoo) { m_iFooVector.push_back(pFoo); }
> IFoo*getComponent(int nIndex) { return m_iFooVector[nIndex]; }
>
> static void registrate(lua_State *pLuaState)
> {
> luabind::module(pLuaState) [
> luabind::class_<FooManager>("FooManager")
> .def(luabind::constructor<>())
> .def("getComponent", (IFoo*(FooManager::*) (int)) &FooManager::getComponent)
> ];
> }
>
> void doAwake(void)
> {
> for(std::vector<IFoo*>::iterator it = m_iFooVector.begin(); it !=
> m_iFooVector.end(); it++)
> {
> IFoo *pIFoo = *it;
> pIFoo->Awake();
> }
> }
>
> void doUpdate(void)
> {
> for(std::vector<IFoo*>::iterator it = m_iFooVector.begin(); it !=
> m_iFooVector.end(); it++)
> {
> IFoo *pIFoo = *it;
> pIFoo->update();
> }
> }
>
> private:
> std::vector<IFoo*> m_iFooVector;
> };
>
> int main()
> {
> lua_State *myLuaState = luaL_newstate();
>
> // Connect LuaBind to this lua state
> luabind::open(myLuaState);
>
> // open the lua libraries
> luaL_openlibs(myLuaState);
>
> // register the luaBind classes
> registrate<IFoo>(myLuaState);
> registrate<Foo>(myLuaState);
> registrate<Foo2>(myLuaState);
> registrate<FooManager>(myLuaState);
>
> // create all the objects
> Foo *pFoo = new Foo("Rod's Test: 1-2-3!", myLuaState);
> Foo2 *pFoo2 = new Foo2("Zimba", myLuaState);
> FooManager* pfooManager = new FooManager();
>
> pfooManager->addComponent(pFoo);
> pfooManager->addComponent(pFoo2);
> // set object manager global
> luabind::globals(myLuaState)["fooManager"] = boost::ref(pfooManager);
>
> //reh 16jun13 the Game Manager will also pass the Instigator to the
> call, i.e., doAwake(theInstigator). This way the object has the object
> interacting with it.
> pfooManager->doAwake();
> pfooManager->doUpdate();
>
> lua_close(myLuaState);
> // clean up
> delete pFoo;
> delete pFoo2;
>
> return 0;
> }
>
>
> On Tue, Jun 18, 2013 at 12:42 PM, Daniel Walter
> <[email protected] <mailto:[email protected]>>
> wrote:
>
> hi rod,
> thank you for your answer.
> what about the struct "A" in my example? In my project I can not change
> struct "A" to use boost::ref or any getters. Of course, I could write a
> wrapper class, but this hurts.
>
> Is there any other way?
>
>
> On 18.06.2013 18:17, rod haxton wrote:
> > give boost:ref a try...
> >
> > luabind::globals(L)["x"] = boost::ref(x);
> >
> >
> > On Tue, Jun 18, 2013 at 10:41 AM, Daniel Walter
> > <[email protected]
> <mailto:[email protected]>
> <mailto:[email protected]
> <mailto:[email protected]>>>
> > wrote:
> >
> > Hi folks,
> >
> > I have a struct with some pointers to trivial datatypes like
> this:
> >
> > struct A {
> > int* x;
> > int* y;
> > };
> >
> > I bound it to lua like this:
> >
> > luabind::module(L)
> > [
> > luabind::class_<A>("A")
> > .def_readonly("x", &A::x)
> > .def_readonly("y", &A::y)
> > ];
> >
> > If I am using this class in my lua script the runtime throws this
> > exception: "Trying to use unregistered class"
> > I managed this using a getter function returning the dereferenced
> > pointer, but there must be a better way.
> >
> > The same exception happens when i am using such an trivial
> pointer
> > directly:
> >
> > int* x = new int(42);
> > luabind::globals(L)["x"] = x;
> >
> > How to handle this? What i am missing?
> >
> > Thanks in advance,
> > Daniel
> >
> >
> ------------------------------------------------------------------------------
> > This SF.net email is sponsored by Windows:
> >
> > Build for Windows Store.
> >
> > http://p.sf.net/sfu/windows-dev2dev
> > _______________________________________________
> > luabind-user mailing list
> > [email protected]
> <mailto:[email protected]>
> > <mailto:[email protected]
> <mailto:[email protected]>>
> > https://lists.sourceforge.net/lists/listinfo/luabind-user
> >
> >
> >
> >
> >
> ------------------------------------------------------------------------------
> > This SF.net email is sponsored by Windows:
> >
> > Build for Windows Store.
> >
> > http://p.sf.net/sfu/windows-dev2dev
> >
> >
> >
> > _______________________________________________
> > luabind-user mailing list
> > [email protected]
> <mailto:[email protected]>
> > https://lists.sourceforge.net/lists/listinfo/luabind-user
> >
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
> _______________________________________________
> luabind-user mailing list
> [email protected]
> <mailto:[email protected]>
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
>
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Windows:
>
> Build for Windows Store.
>
> http://p.sf.net/sfu/windows-dev2dev
>
>
>
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user
>
------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:
Build for Windows Store.
http://p.sf.net/sfu/windows-dev2dev