2

Lua classes can be created using the OO system that Luabind exposes to Lua:

http://www.rasterbar.com/products/luabind/docs.html#defining-classes-in-lua

class 'MyClass'
function MyClass:__init()    
    self.a1 = true
    self.a2 = "MyClass"
end

After creating the above class, if I want the attributes which I have added in Lua(i.e. a1 and a2), I do the following:

obj = MyClass()

info = class_info(obj)
print("Methods:")
for k,v in pairs(info.methods) do 
    print(k .. ': ' .. tostring(v))
end

print("Attributes:")
for k,v in pairs(info.attributes) do 
    print(k .. ': ' .. tostring(v))
end

But I see only __init and no attributes. Is there a way to get the attributes a1 & a2?

I have stepped through get_class_info() while debugging (see https://github.com/luabind/luabind/blob/master/src/class_info.cpp#L33)

But that function does not seem to iterate more than once before exiting. Does anyone know the object format which is used by luabind? Or how I can extract these attributes?

Note: If I create a class in C++ and expose it through Luabind, then I do see any attributes that I exposed from C++. Its the attributes in Lua which are the issue.

safe_malloc
  • 824
  • 2
  • 12
  • 29
  • What is `class_info`? – hjpotter92 Sep 29 '15 at 08:52
  • Also tell us the version of the Lua interpreter that you're using. – legends2k Sep 29 '15 at 08:53
  • Its a function which luabind provides globally to retrieve information about objects created through its object-oriented system. Referring to line https://github.com/luabind/luabind/blob/master/src/class_info.cpp#L106 , the get_class_info() function is exposed to Lua as class_info() – safe_malloc Sep 29 '15 at 08:53
  • I am using Lua 5.3, luabind 0.9.1. Apart from some simple function name changes to adapt the luabind code to lua 5.3, there are no changes with respect to the released code in https://github.com/luabind/luabind – safe_malloc Sep 29 '15 at 08:57
  • You won't have `a1` and `a2` available until `__init()` gets executed. – hjpotter92 Sep 29 '15 at 09:00
  • The line obj = MyClass() ensures that. I can print then after that line. So they are in there...somewhere :D. Not sure how to iterate through them though. – safe_malloc Sep 29 '15 at 09:02
  • Check if `print(type(info.attributes))` prints `table`; if it's `user_data` you mightn't be able to iterate over them. – legends2k Sep 29 '15 at 09:12
  • Can you iterate over obj? – ryanpattison Sep 29 '15 at 10:53

0 Answers0