ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.16k stars 504 forks source link

How to iterate on usertypes #1403

Open xhighway999 opened 2 years ago

xhighway999 commented 2 years ago

Hi, I am struggling to iterate over the properties of a sol usertype. Let's imagine the following vector example:

sol::usertype<itero::Vector3f> vector3_type = s.new_usertype<itero::Vector3f>("Vector3f",
                                                                               sol::constructors<itero::Vector3f(),
                                                                                itero::Vector3f(float,float,float)>());
    vector3_type["x"] = &itero::Vector3f::x;
    vector3_type["y"] = &itero::Vector3f::y;
    vector3_type["z"] = &itero::Vector3f::z;

How can I find the x,y, and z properties iteratively without a name? I am looking for something like this:

local vec = Vector3f.new()
for k,v in pairs(vec) do
    print(k)
end

The output would be something like

x
y
z

Is this already available? I couldn't find anything in the documentation. Thanks in advance, xhighway999

otristan commented 1 year ago

see https://github.com/ThePhD/sol2/issues/146