diff --git a/src/api/types/shapes.c b/src/api/types/shapes.c index c609d1ce..9406471f 100644 --- a/src/api/types/shapes.c +++ b/src/api/types/shapes.c @@ -44,3 +44,27 @@ int l_lovrShapeSetEnabled(lua_State* L) { lovrShapeSetEnabled(shape, enabled); return 0; } + +int l_lovrShapeGetUserData(lua_State* L) { + Shape* shape = luax_checktypeof(L, 1, Shape); + int ref = (int) lovrShapeGetUserData(shape); + lua_rawgeti(L, LUA_REGISTRYINDEX, ref); + return 1; +} + +int l_lovrShapeSetUserData(lua_State* L) { + Shape* shape = luax_checktypeof(L, 1, Shape); + uint64_t ref = (int) lovrShapeGetUserData(shape); + if (ref) { + luaL_unref(L, LUA_REGISTRYINDEX, ref); + } + + if (lua_gettop(L) < 2) { + lua_pushnil(L); + } + + lua_settop(L, 2); + ref = luaL_ref(L, LUA_REGISTRYINDEX); + lovrShapeSetUserData(shape, (void*) ref); + return 0; +} diff --git a/src/physics/physics.c b/src/physics/physics.c index 479c2a76..0abf2393 100644 --- a/src/physics/physics.c +++ b/src/physics/physics.c @@ -295,3 +295,11 @@ void lovrShapeSetEnabled(Shape* shape, int enabled) { dGeomDisable(shape->id); } } + +void* lovrShapeGetUserData(Shape* shape) { + return dGeomGetData(shape->id); +} + +void lovrShapeSetUserData(Shape* shape, void* data) { + dGeomSetData(shape->id, data); +} diff --git a/src/physics/physics.h b/src/physics/physics.h index d6de448f..55b767be 100644 --- a/src/physics/physics.h +++ b/src/physics/physics.h @@ -80,3 +80,5 @@ Body* lovrShapeGetBody(Shape* shape); void lovrShapeSetBody(Shape* shape, Body* body); int lovrShapeIsEnabled(Shape* shape); void lovrShapeSetEnabled(Shape* shape, int enabled); +void* lovrShapeGetUserData(Shape* shape); +void lovrShapeSetUserData(Shape* shape, void* data);