Lua Wrapper component can now be serialized

This commit is contained in:
2019-06-26 20:29:28 +00:00
parent 7ff9e21b4d
commit 7d55563ec8
7 changed files with 174 additions and 62 deletions

View File

@@ -16,8 +16,16 @@
#include "ecs.h"
#include "ecs_components.h"
inline void handle_error(sol::optional<std::string> maybe_msg) {
std::cerr << "Lua error, aborting!\n";
if (maybe_msg) {
const std::string& msg = maybe_msg.value();
std::cerr << "Error: " << msg << '\n';
}
}
int main() {
sol::state lua;
sol::state lua(sol::c_call<decltype(&handle_error), &handle_error>);
lua.open_libraries(sol::lib::base, sol::lib::package);
ecs::lua::init(lua);
@@ -31,7 +39,7 @@ int main() {
[](std::ostream& os, ecs::Component* component) {
ecs::lua::Wrapper* wrapper = (ecs::lua::Wrapper*)component;
iohelper::write<std::string>(os, wrapper->name);
// iohelper::write<std::string>(os, wrapper->name);
// #todo .size() does not work
size_t size = 0;
@@ -55,7 +63,8 @@ int main() {
break;
case sol::type::number:
iohelper::write<int32_t>(os, b.as<int32_t>());
// @todo These should be doubles instead of floats
iohelper::write<float>(os, b.as<float>());
break;
case sol::type::boolean:
@@ -73,11 +82,10 @@ int main() {
}
}
},
[](std::istream& is, ecs::Component* component) {
[&lua](std::istream& is, ecs::Component* component) {
ecs::lua::Wrapper* wrapper = (ecs::lua::Wrapper*)component;
wrapper->name = iohelper::read<std::string>(is);
std::cout << wrapper->name << '\n';
// @todo Only do this if table is not created yet
wrapper->table = lua.create_table();
size_t size = iohelper::read_length(is);
std::cout << "Size: " << size << '\n';
@@ -95,15 +103,16 @@ int main() {
break;
case sol::type::string:
std::cout << "Value: " << iohelper::read<std::string>(is) << '\n';
wrapper->table[name] = iohelper::read<std::string>(is);
break;
case sol::type::number:
std::cout << "Value: " << std::dec << iohelper::read<int32_t>(is) << '\n';
// @todo Handle floats
wrapper->table[name] = iohelper::read<float>(is);
break;
case sol::type::boolean:
std::cout << "Value: " << std::dec << iohelper::read<bool>(is) << '\n';
wrapper->table[name] = iohelper::read<bool>(is);
break;
default:
@@ -120,7 +129,7 @@ int main() {
return manager;
});
lua.script_file("test.lua");
lua.safe_script_file("test.lua");
std::cout << "Update position\n";
manager.view<Position, Velocity>().for_each([](ecs::Entity*, Position* pos, Velocity* vel) {
@@ -186,6 +195,7 @@ int main() {
// This is needed to avoid id conflicts between clients
std::ofstream ids2("ids", std::ios::out | std::ios::trunc);
ecs::serial::serialize_ids(ids2);
ids2.close();
}
{
@@ -218,5 +228,11 @@ int main() {
std::cout << "x: " << pos->x << '\n';
std::cout << "y: " << pos->y << '\n';
});
lua.set_function("get_manager", [&manager] () -> ecs::Manager& {
return manager;
});
lua.safe_script_file("test2.lua");
}
}