2020-01-29 03:01:34 +08:00
|
|
|
#define SOL_ALL_SAFETIES_ON 1
|
|
|
|
#include <sol/sol.hpp>
|
|
|
|
|
|
|
|
|
|
|
|
struct Vector {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
|
|
|
Vector() = default;
|
|
|
|
|
|
|
|
Vector(int _x, int _y) : x { _x }, y { _y } {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
sol::state lua;
|
|
|
|
lua.open_libraries(sol::lib::base);
|
|
|
|
|
2021-03-06 23:14:48 +08:00
|
|
|
lua.new_usertype<Vector>("Vector",
|
|
|
|
sol::constructors<Vector(), Vector(int, int)>(),
|
|
|
|
"x",
|
|
|
|
sol::property(&Vector::x, &Vector::x),
|
|
|
|
"y",
|
|
|
|
sol::property(&Vector::y, &Vector::y));
|
|
|
|
|
|
|
|
lua.script(
|
|
|
|
"vectors = { Vector.new(3, 6), Vector.new(6, 3) }");
|
2020-01-29 03:01:34 +08:00
|
|
|
auto vectors = lua["vectors"].get<std::vector<Vector>>();
|
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(vectors[0].x == 3);
|
|
|
|
sol_c_assert(vectors[0].y == 6);
|
2020-01-29 03:01:34 +08:00
|
|
|
|
2021-03-06 14:03:23 +08:00
|
|
|
sol_c_assert(vectors[1].x == 6);
|
|
|
|
sol_c_assert(vectors[1].y == 3);
|
2020-01-29 03:01:34 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|