Re: Trouble binding a class - should be simple

Tony Kostanjsek <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
Have you tried to add a binding for the default constructor?




On 05.05.2011, at 11:53, Simon Pickles <[email protected]> wrote:

> On 05/05/2011 09:48, feng qiu wrote:     
>> 
>> Please try v:getX().
>> 
>> On 5 May 2011 15:56, Simon Pickles <[email protected]> wrote:
>> 
>> Hello,
>> 
>> I am trying to expose part of the btVector3 class from BulletPhysics to Lua:
>> 
>> http://www.bulletphysics.com/Bullet/BulletFull/classbtVector3.html
>> 
>> My first attempt:
>> 
>> // C++
>>   void RegisterPhysicsTypesWithLua( lua_State* lua )
>>   {
>>     module( lua )
>>     [
>>       class_<btVector3>( "btVector3" )
>>       .def( "getX",&btVector3::getX )
>>       .def( "getY",&btVector3::getY )
>>       .def( "getZ",&btVector3::getZ )
>>       .def( "setX",&btVector3::setX )
>>       .def( "setY",&btVector3::setY )
>>       .def( "setZ",&btVector3::setZ )
>>     ];
>>   }
>> 
>> --Lua
>> print("Testing btVector3 in lua")
>> v = btVector3()
>> print("btVector3 created")
>> 
>> print( "x = " .. v.getX() )  -- should print x = 0.0
>> v.setX(99.9)
>> print( "x = " .. v.getX() )  -- should print x = 99.9
>> 
>> ------------------------------------------
>> This first attempt produces the error:
>> "Attempt to call a nil value"
>> 
>> 
>> So I added a constructor and tried again:
>> 
>> // C++
>>   void RegisterPhysicsTypesWithLua( lua_State* lua )
>>   {
>>     module( lua )
>>     [
>>       class_<btVector3>( "btVector3" )
>>       .def( constructor<  float, float, float>() )
>>       .def( "getX",&btVector3::getX )
>>       .def( "getY",&btVector3::getY )
>>       .def( "getZ",&btVector3::getZ )
>>       .def( "setX",&btVector3::setX )
>>       .def( "setY",&btVector3::setY )
>>       .def( "setZ",&btVector3::setZ )
>>     ];
>>   }
>> 
>> --Lua
>> print("Testing btVector3 in lua")
>> v = btVector3(1.0,2.0,3.0)
>> print("btVector3 created")
>> 
>> print( "x = " .. v.getX() )  -- should print x = 1.0
>> v.setX(99.9)
>> print( "x = " .. v.getX() )  -- should print x = 99.9
>> 
>> ------------------------------------------
>> This second attempt produces the error:
>> "No such operator defined"
>> 
>> 
>> I have double checked that I am definately calling the
>> RegisterPhysicsTypesWithLua with the correct LuaState.
>> 
>> I expected this to be reasonably trivial as I have been exposing my own
>> classes with ease. This is however, the first time I have tried to
>> expose a 3rd party class. Am I missing something obvious?
>> 
>> Thanks very much
>> 
>> Simon
> Well spotted (too used to Python!). The second version works!
> 
> However doesn't explain why the first version (with default ctor) fails. It never gets to the getX() call:
> Testing btVector3 in lua
> Error: Attempt to call a nil value
> 
> 
> btVector3 does indeed have a default ctor: 
> http://www.bulletphysics.com/Bullet/BulletFull/classbtVector3.html#a82b4c5da54ecd4c91bd9b056dea0d288
> 
> Thanks
> 
> ------------------------------------------------------------------------------
> WhatsUp Gold - Download Free Network Management Software
> The most intuitive, comprehensive, and cost-effective network 
> management toolset available today.  Delivers lowest initial 
> acquisition cost and overall TCO of any competing solution.
> http://p.sf.net/sfu/whatsupgold-sd
> _______________________________________________
> luabind-user mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/luabind-user

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network 
management toolset available today.  Delivers lowest initial 
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.