]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Values in entity table are now saved in the save file
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Wed, 4 Sep 2019 06:58:07 +0000 (02:58 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Wed, 4 Sep 2019 06:58:07 +0000 (02:58 -0400)
src/components/Script.hpp
src/engine.cpp
src/main.cpp

index f7927503dfb5a990b1625055d0fa90a32fdd0f1d..82381e953a22e9c2b28334a94abc0f4c5169df01 100644 (file)
@@ -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";
index fe9af3d8d53b4d111b9aeedeab23f4059087635d..9e77b401a74b1858849dd94d7adc82038302d2b0 100644 (file)
@@ -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)
index 7876f8a47594c24b0fe5d85cd25cff6d32b772e2..a0632fd925cc9e10b83bffe32209867d9be463e3 100644 (file)
@@ -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>