Issue with using pointers with LuaBridge

Started by
5 comments, last by Daixiwen 6 years, 10 months ago

Hey,

I am currently using LuaBridge as the C++ interface to Lua, and I'm having trouble passing a simple pointer to Lua (as a unique identifier for an object) and then back again. The Lua script calls "object(...)" which returns a pointer, and the pointer is stored in a local: "local obj = tbl.object(....)". If I then proceed to use the variable obj for anything, it's always "nil". What's going on here?

I would think this was the most basic and repeating pattern in Lua scripting to use memory addresses as identifiers as well as simplify access to engine internals.


static Object* object(int x, int y, int w, int h, int floor) {
  auto obj = std::make_unique<Object> (gworld(), x, y, w, h, floor);
  gworld().objects().add(std::move(obj));
  return obj.get();
}
...
static void object_frame(LuaRef id, int tex, int x, int y, int w, int h) {
  printf("object_frame(%p, tex=%d)\n", id.cast<Object*> (), tex);
}
...
  gns.beginNamespace("dm")
    .addFunction("object", object)
...
  local obj = dm.object(x,y, 8,8, 0)
  dm.object_frame(obj, gui.tex, 80 + 9*idx,0, 9,8)

The value of id, which should just be a memory address, in the "object_frame" function is always nil.

Advertisement

I solved this, for now, by passing uintptr_t around. Thought about putting the objects index into the 16 unused address bits, but I haven't needed it yet.

It's very strange though, that LuaBridge can't pass even void* around. I would understand it if it tried to translate objects (I've seen the more complex usage), but void* should just be passed around as an opaque pointer, which is what it was designed as. Disappointing too.

And why am I not Prime Members anymore :(

I haven't used LuaBridge, but yes, lua has the "light userdata" type which is exactly equivalent to a void*. Pointers should generally be implemented as these light-userdata types, or as heavy userdata in order to attach RTTI info / a table of members, etc, to it.

Did you also register your Object class in LuaRef? In your beginNamespace() you must have somewhere at least a .beginClass<Object>("Object") somewhere before the LuaRef can be casted to the Object class.

Definition of a man-year: 730 people trying to finish the project before lunch
On 19.6.2017 at 9:32 AM, Daixiwen said:

Did you also register your Object class in LuaRef? In your beginNamespace() you must have somewhere at least a .beginClass<Object>("Object") somewhere before the LuaRef can be casted to the Object class.

I actually haven't tried that interface yet. All I wanted to do was pass a memory address to Lua, which Lua should interpret as opaque and just pass back as parameters to my engine interface.

I will probably try the class-like method at some point and see how useful it is.

I understand, but it will only work if you register your class. LuaRef can only be used with the standard Lua types, and the types registered with .beginClass. If you don't register your class, Luabridge will miss some information to do the conversion properly between a Lua lightuserdata object and a C++ pointer.

Definition of a man-year: 730 people trying to finish the project before lunch

This topic is closed to new replies.

Advertisement