aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-04 02:58:07 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-04 02:58:07 -0400
commit1549ecad22294cae6f1a2e6e6b256b2192b0c3d3 (patch)
tree9f2f99fcb9b09216b252abcf2cbbd0b7b0e32e38
parent3f39453ca9d3bacfffaa6cdb5e4f531fee9020dd (diff)
Values in entity table are now saved in the save file
-rw-r--r--src/components/Script.hpp56
-rw-r--r--src/engine.cpp10
-rw-r--r--src/main.cpp2
3 files changed, 61 insertions, 7 deletions
diff --git a/src/components/Script.hpp b/src/components/Script.hpp
index f792750..82381e9 100644
--- a/src/components/Script.hpp
+++ b/src/components/Script.hpp
@@ -20,6 +20,8 @@
#define COMPONENT_SCRIPT_HPP_
#include "Component.hpp"
+#include <cereal/types/vector.hpp>
+#include <cereal/types/tuple.hpp>
struct Scripted : Component<Scripted>
{
@@ -61,8 +63,58 @@ public:
caller["RenderIdle"](caller);
}
- void serialize([[maybe_unused]] cereal::JSONOutputArchive& ar) final {}
- void serialize([[maybe_unused]] cereal::JSONInputArchive& ar) final {}
+ void serialize(cereal::JSONOutputArchive& ar) final {
+ std::vector<std::tuple<std::string, std::string>> table_components;
+ caller.for_each([&table_components](sol::object key, sol::object value){
+ if (value.get_type() == sol::type::string)
+ table_components.push_back(std::make_tuple(
+ key.as<std::string>(),
+ std::string("return \"" + value.as<std::string>() + "\"")
+ ));
+ else if (value.get_type() == sol::type::number)
+ table_components.push_back(std::make_tuple(
+ key.as<std::string>(),
+ std::string("return " + value.as<std::string>())
+ ));
+ else if (value.get_type() == sol::type::boolean)
+ table_components.push_back(std::make_tuple(
+ key.as<std::string>(),
+ std::string("return " + value.as<std::string>())
+ ));
+ //else if (value.get_type() == sol::type::function) {
+ // sol::state lua;
+ // lua.open_libraries(sol::lib::base, sol::lib::string);
+
+ // sol::function dump = lua.script("return string.dump");
+
+ // sol::function f = value;
+ // std::string gg = dump(f);
+ // table_components.push_back(std::make_tuple(
+ // key.as<std::string>(),
+ // std::string("return (loadstring or load)(" +
+ // gg + ")")
+ // ));
+ //}
+ });
+
+ ar(CEREAL_NVP(table_components));
+ }
+
+ void serialize(cereal::JSONInputArchive& ar) final {
+ sol::state lua;
+ lua.open_libraries(sol::lib::base, sol::lib::math, sol::lib::string);
+
+ std::vector<std::tuple<std::string, std::string>> table_components;
+ ar(CEREAL_NVP(table_components));
+
+ for (auto &s : table_components) {
+ std::string key = std::get<0>(s);
+ std::string value = std::get<1>(s);
+ sol::object ret = lua.script(value);
+ caller[key.c_str()] = ret;
+ }
+
+ }
std::string serializeName(void) const final {
return "Scripted";
diff --git a/src/engine.cpp b/src/engine.cpp
index fe9af3d..9e77b40 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -100,11 +100,6 @@ void Engine::logicLoop(void)
elapsed += dt;
std::this_thread::yield();
}
-
- // Remove all Lua references from entities
- entities.each<Scripted>([](entityx::Entity, Scripted &f){ f.cleanup(); });
- entities.each<EventListener>([](entityx::Entity, EventListener &f){
- f.cleanup(); });
}
void Engine::renderLoop(void)
@@ -130,6 +125,11 @@ void Engine::run(void)
// Save the entities' data
GameState::save("save.json", entities);
+
+ // Remove all Lua references from entities
+ entities.each<Scripted>([](entityx::Entity, Scripted &f){ f.cleanup(); });
+ entities.each<EventListener>([](entityx::Entity, EventListener &f){
+ f.cleanup(); });
}
bool Engine::shouldRun(void)
diff --git a/src/main.cpp b/src/main.cpp
index 7876f8a..a0632fd 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -18,6 +18,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
+#define SOL_ALL_SAFETIES_ON = 1
+
#include "engine.hpp"
#include <SDL2/SDL.h>