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");
}
}

View File

@@ -3,6 +3,7 @@ local ecs = require "ecs"
local Position = require "ecs.Position"
local Velocity = require "ecs.Velocity"
local Meta = require "ecs.Meta"
local Wrapper = require 'ecs.Wrapper'
manager = get_manager()
ent = manager:create_entity()
@@ -10,12 +11,13 @@ ent:add_component(Position.new(1.9, 9.7))
ent:add_component(Velocity.new(0.2, 0.3))
ent:add_component(Meta.new("Soldier"))
random = {
data = {
speed = 10,
something = "Hello, World!",
alive = true
}
ent:add_component(ecs.Wrapper.new("Random", random))
ent:add_component(Wrapper.new("LuaData", data))
ent:add_component(Wrapper.new("TestThing", { test = 1.23563 }))
-- @todo Make this happen...
-- stats = {
@@ -60,20 +62,26 @@ end)
-- print "TEST"
-- end
manager:view("Wrapper"):for_each(function(ent)
wrapped = ent:get_component("Wrapper").table
manager:view("LuaData"):for_each(function(ent)
d = ent:get_component("LuaData")
print(wrapped.speed)
wrapped.speed = 11
print(wrapped.speed)
print(random.speed)
random.speed = 20
print(wrapped.speed)
print(random.speed)
print(d.speed)
d.speed = 11
print(d.speed)
print(data.speed)
data.speed = 20
print(d.speed)
print(data.speed)
print(wrapped.alive)
wrapped.alive = false
print(wrapped.alive)
print(d.alive)
d.alive = false
print(d.alive)
print(wrapped.something)
print(d.something)
end)
manager:view("TestThing"):for_each(function(ent)
t = ent:get_component("TestThing")
print(t.test)
end)

21
test/test2.lua Normal file
View File

@@ -0,0 +1,21 @@
manager = get_manager()
manager:view("LuaData"):for_each(function(ent)
-- @todo It would be nice if this could somehow be passed in as function arg
data = ent:get_component("LuaData")
print("speed: " .. data.speed)
print("something: " .. data.something)
-- print("alive: " .. data.alive)
end)
manager:view("TestThing"):for_each(function(ent)
data = ent:get_component("TestThing")
print("test: " .. data.test)
end)
-- @todo Allow this
-- for i, v in pairs(manager:view("TestThing")) do
-- print(i, v)
-- end
print(manager:has_entity("6d58fdb5-6d8c-4e6f-89d4-f7d7b184f463"))