0

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;
}
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
Shadow fiend
  • 753
  • 5
  • 7

1 Answers1

0

First check whether _state["Test"] exists (and if you are really paranoid, check that it is a table). If so, use it to construct a sol::usertype<Test> to which you can add the second function. If not, create a new usertype as you are doing.