From 464dcba75139c2c010209fac311456d4a3d173aa Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Thu, 23 Jun 2022 21:41:18 +0100
Subject: [PATCH] handle non-existant states more gracefully

---
 fggl/app.cpp                 | 5 ++++-
 fggl/scenes/game.cpp         | 2 +-
 include/fggl/util/states.hpp | 8 ++++++--
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/fggl/app.cpp b/fggl/app.cpp
index 0d6ec0d..7d1c223 100644
--- a/fggl/app.cpp
+++ b/fggl/app.cpp
@@ -46,7 +46,10 @@ namespace fggl {
         while ( m_running ) {
 			// trigger a state change if expected
 			if ( m_expectedScene != m_states.activeID() ) {
-				m_states.change(m_expectedScene);
+				auto result = m_states.change(m_expectedScene);
+				if ( !result ) {
+					m_expectedScene = m_states.activeID();
+				}
 			}
 
             auto& state = m_states.active();
diff --git a/fggl/scenes/game.cpp b/fggl/scenes/game.cpp
index 607ef4e..2dea4c4 100644
--- a/fggl/scenes/game.cpp
+++ b/fggl/scenes/game.cpp
@@ -43,7 +43,7 @@ namespace fggl::scenes {
 	}
 
 	void Game::update() {
-		assert( m_world );
+		assert( m_world && "called game update, but there was no world - was activate called?" );
 
 		if ( m_input != nullptr ) {
 			bool escapePressed = m_input->keyboard.pressed(glfwGetKeyScancode(GLFW_KEY_ESCAPE));
diff --git a/include/fggl/util/states.hpp b/include/fggl/util/states.hpp
index 6edee1f..fa2c713 100644
--- a/include/fggl/util/states.hpp
+++ b/include/fggl/util/states.hpp
@@ -48,12 +48,16 @@ namespace fggl::util {
 				return (T *) (m_states[name].get());
 			}
 
-			void change(const Identifer &name) {
-				assertm(m_states.find(name) != m_states.end(), "state does not exist");
+			bool change(const Identifer &name) {
+				if ( m_states.find(name) == m_states.end() )  {
+					debug::error("attempted to change to non-existent state {}, ignoring you.", name);
+					return false;
+				}
 
 				active().deactivate();
 				m_active = name;
 				active().activate();
+				return true;
 			}
 
 			StateType &active() const {
-- 
GitLab