Trouble binding a class - should be simple

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Trouble binding a class - should be simple

Simon Pickles-3

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



------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Trouble binding a class - should be simple

feng qiu-5
Please try v:getX().

On 5 May 2011 15:56, Simon Pickles <[hidden email]> 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



------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user



--
Q..

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Trouble binding a class - should be simple

Simon Pickles-3
On 05/05/2011 09:48, feng qiu wrote:
Please try v:getX().

On 5 May 2011 15:56, Simon Pickles <[hidden email]> 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Trouble binding a class - should be simple

Tony Kostanjsek
Have you tried to add a binding for the default constructor?




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

On 05/05/2011 09:48, feng qiu wrote:
Please try v:getX().

On 5 May 2011 15:56, Simon Pickles <[hidden email]> 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
[hidden email]
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Trouble binding a class - should be simple

Simon Pickles-3
On 05/05/2011 11:35, Tony Kostanjsek wrote:
Have you tried to add a binding for the default constructor?




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

On 05/05/2011 09:48, feng qiu wrote:
Please try v:getX().

On 5 May 2011 15:56, Simon Pickles <[hidden email]> 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


Thanks Tony, Didn't know I could do that.

Is this a top-posting list then? ;)

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Trouble binding a class - should be simple

Ryan Pavlik-4
You might also want to look at using the "property" mechanism to automate use of set/get:

.property("x", &Object::getX, &Object::setX)

that way you can do
bla = Object()
bla.x = 5
print(bla.x)

Ryan

On Thu, May 5, 2011 at 9:17 AM, Simon Pickles <[hidden email]> wrote:
On 05/05/2011 11:35, Tony Kostanjsek wrote:
Have you tried to add a binding for the default constructor?




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

On 05/05/2011 09:48, feng qiu wrote:
Please try v:getX().

On 5 May 2011 15:56, Simon Pickles <[hidden email]> 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


Thanks Tony, Didn't know I could do that.

Is this a top-posting list then? ;)

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user




--
Ryan Pavlik
HCI Graduate Student
Virtual Reality Applications Center
Iowa State University

[hidden email]
http://academic.cleardefinition.com
Internal VRAC/HCI Site: http://tinyurl.com/rpavlik

------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user