In C++ with sol3, My code is like this
sol::state _state;
void Func1()
{
auto userType1 = _state.new_usertype<Test>("Test", sol::constructors<Test()>());
userType1["testFunction1"] = &test1;
}
void Func2()
{
auto userType2 = _state.new_usertype<Test>("Test", sol::constructors<Test()>());
userType2["testFunction2"] = &test2;
}
int main()
{
Func1();
Func2();
}
In lua script, I can only call Test.testFunction2 which means that userType2 override userType1. The lua script can not see testFunction1. I wonder if there is a way to return the userType if exist, and create it if not. Then I can call both testFunction1 and testFunction2. As the code shown below.
void Func1()
{
auto userType1 = _state.CreateOrGetUserType<Test>("Test", sol::constructors<Test()>());
userType1["testFunction1"] = &test1;
}
void Func2()
{
auto userType2 = _state.CreateOrGetUserType<Test>("Test", sol::constructors<Test()>());
userType2["testFunction2"] = &test2;
}