How to bind a raw pointer with Luabind

By : Sun
Source: Stackoverflow.com
Question!

I'm attempting to use luabind to bind box2d so that I can use it within my lua scripts. I've hit a problem where I cant seem to bind raw pointers with luabind. Below is my code:

luabind::module(luaState)[
   luabind::class_<b2Shape>("b2Shape")
];

luabind::module(luaState)[
    luabind::class_<b2PolygonShape, luabind::bases<b2Shape>>("b2PolygonShape")
    .def(luabind::constructor<>())
    .def("GetChildCount", &b2PolygonShape::GetChildCount)
    .def("SetAsBox", (void (b2PolygonShape::*) (float32 hx, float32 hy) ) &b2PolygonShape::SetAsBox)
    .def("SetAsBox", (void (b2PolygonShape::*) (float32 hx, float32 hy, const b2Vec2& center, float32 angle) ) &b2PolygonShape::SetAsBox)
    .def("TestPoint", (void (b2PolygonShape::*) (const b2Transform& transform, const b2Vec2& p) ) &b2PolygonShape::TestPoint)
    .def("ComputeAABB", (void (b2PolygonShape::*) (b2AABB* aabb, const b2Transform& transform, int32 childIndex) ) &b2PolygonShape::ComputeAABB)
    .def("GetVertexCount", (void (b2PolygonShape::*) () ) &b2PolygonShape::GetVertexCount)
    .def("GetVertex", (const b2Vec2& (b2PolygonShape::*) (int32 index) ) &b2PolygonShape::GetVertexCount)
    .def("Validate", &b2PolygonShape::Validate)
];

luabind::module(luaState)[
    luabind::class_<b2FixtureDef>("b2FixtureDef")
    .def(luabind::constructor<>())
    .def_readwrite("shape", &b2FixtureDef::shape)
    .def_readwrite("friction", &b2FixtureDef::friction)
    .def_readwrite("restitution", &b2FixtureDef::restitution)
    .def_readwrite("density", &b2FixtureDef::density)
    .def_readwrite("isSensor", &b2FixtureDef::isSensor)
    .def_readwrite("filter", &b2FixtureDef::filter)
];

Here's my lua code:

local anchorBodyDef = b2BodyDef()
anchorBodyDef.position = b2Vec2(20.0, 0.0)

local anchorShape = b2PolygonShape()
anchorShape:SetAsBox(2.0, 0.5)

local anchorFixDef = b2FixtureDef()
anchorFixDef.shape = anchorShape

Everytime I attempt to assign a shape to my fixtureDef using anchorFixDef.shape = anchorShape, lua throws an error:

terminate called after throwing an instance of 'luabind::error'
what():  lua runtime error

How would you go about binding something like const b2Shape* shape; in luaBind since something like .def_readwrite("shape", &b2FixtureDef::shape) is giving me problems. I've seen some code in the docs which use smart pointers in the class_ binding statements, but that hasn't fixed the problems.

Thanks.

By : Sun


Answers

By exposing

By : Arun R


This video can help you solving your question :)
By: admin