sol2
sol2 copied to clipboard
usertype "struct" to c++?
I have a struct, that is used in lua. But then I need to use that struct in my save function in c++. Should this code work or am I missing something?
I'm using the oldest version of sol2
struct oData {
int mystuff = 0;
float x = 0, y = 0;
}
void SaveData(oData* data) {
Savestuff(data);
}
int main() {
sol::state lua;
lua.set_function("save", &SaveData);
lua.new_usertype<oData>("Data", "mystuff", &oData::mystuff, "x", &oData::x, "y, &oData::y);
lua.script(R"(local dt = Data.new()
dt.mystuff = 1
dt.x = 20
dt.y = 25
save(dt)
)";
}
The code seems to work on a newer version of Sol. Unless something fundamental has changed I would assume it works on some older versions too.
Do you have reason to believe it does not work? It seems that the code you have posted has some syntax errors in it so it wont compile at least, but after correcting them the code ran fine.