Add implementation of GameScript::InitCallbacks and the C++ holders of the callbacks. This might well be a needless layer of indirection, but it prevents the callbacks being reassigned outside of the class.

This commit is contained in:
hispidence 2021-07-01 19:33:48 +01:00
parent 76aa8cd786
commit 4c02d6a222

View file

@ -389,4 +389,47 @@ void LuaVariables::SetVariable(std::string key, sol::object value)
}
}
void GameScript::OnStart()
{
m_onStart();
}
void GameScript::OnLoad()
{
m_onLoad();
}
void GameScript::OnControlPhase()
{
m_onControlPhase();
}
void GameScript::OnSave()
{
m_onSave();
}
void GameScript::OnEnd()
{
m_onEnd();
}
void GameScript::InitCallbacks()
{
auto assignCB = [this](sol::protected_function& func, char const* luaFunc) {
func = (*m_lua)[luaFunc];
if (WarningsAsErrors && !func.valid())
{
std::string err{ "Level's script file requires callback \"" };
err += std::string{ luaFunc };
err += "\"";
throw std::runtime_error(err);
}
};
assignCB(m_onStart, "OnStart");
assignCB(m_onLoad, "OnLoad");
assignCB(m_onControlPhase, "OnControlPhase");
assignCB(m_onSave, "OnSave");
assignCB(m_onEnd, "OnEnd");
}