This isn't tested, but it's basically what I've gotten working in my own project as of now.
RTTR is really cool and will let you save type information for elsewhere in your project (maybe an editor if it's a game engine), but also using visitors will allow you to automatically integrate with other existing libraries like sol2, chaiscript, etc.
The biggest pain point I found was working with the usertypes generically. The documentation is kind of bad in this case.
I thought I could just do this:
template <typename T>
void visit_property(const property_info<T>& info) {
auto name = info.property_item.get_name().to_string();
auto table = info.property_item.get_declaring_type().get_name().to_string();
// Register the property on the usertype!
lua[table][name] = info.property_accessor;
}
However, it seems like accessing the usertype through direct indexing doesn't quite work. Indexing in this way uses table proxies and such to let you do fancy chained indexing, lazy evaluation, etc.
Instead, you have to explicitly retrieve the table as a sol::usertype<T>
:
template <typename T>
void visit_property(const property_info<T>& info) {
// We can grab the underlying class type here (in this example it's vector3)
using Class = typename property_info<T>::declaring_type;
auto name = info.property_item.get_name().to_string();
auto table = info.property_item.get_declaring_type().get_name().to_string();
// Explicitly get a sol::usertype<T> from the global table.
auto usertype = lua.get<sol::usertype<Class>>(table);
usertype[name] = info.property_accessor;
}