Add function to destroy the current state machine

This commit is contained in:
Malin E
2021-10-21 11:16:59 +02:00
parent c887b1fa80
commit d23f220fd2
3 changed files with 22 additions and 0 deletions

View File

@@ -69,6 +69,11 @@ void StateMachineModule::initializeStateMachine(const ghoul::Dictionary& states,
}
}
void StateMachineModule::deinitializeStateMachine() {
_machine.reset();
_machine = nullptr;
}
bool StateMachineModule::hasStateMachine() const {
return _machine != nullptr;
}
@@ -148,6 +153,13 @@ scripting::LuaLibrary StateMachineModule::luaLibrary() const {
"the identifier of the desired initial state. If left out, the first state "
"in the list will be used."
},
{
"destroyStateMachine",
&luascriptfunctions::destroyStateMachine,
{},
"",
"Destroys the current state machine and deletes all the memory."
},
{
"goToState",
&luascriptfunctions::goToState,

View File

@@ -43,6 +43,7 @@ public:
void initializeStateMachine(const ghoul::Dictionary& states,
const ghoul::Dictionary& transitions,
const std::optional<std::string> startState = std::nullopt);
void deinitializeStateMachine();
bool hasStateMachine() const;

View File

@@ -47,6 +47,15 @@ int createStateMachine(lua_State* L) {
return 0;
}
int destroyStateMachine(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 0, "lua::destroyStateMachine");
StateMachineModule* module = global::moduleEngine->module<StateMachineModule>();
module->deinitializeStateMachine();
return 0;
}
int goToState(lua_State* L) {
ghoul::lua::checkArgumentsAndThrow(L, 1, "lua::goToState");
std::string newState = ghoul::lua::value<std::string>(L);