update entities.spawn(...) semantics to the docs state

This commit is contained in:
MihailRis
2024-07-17 10:17:20 +03:00
parent 51a57d3d1f
commit 1b99a48849
9 changed files with 23 additions and 13 deletions
+5 -2
View File
@@ -20,9 +20,12 @@ static int l_spawn(lua::State* L) {
auto defname = lua::tostring(L, 1);
auto& def = content->entities.require(defname);
auto pos = lua::tovec3(L, 2);
dynamic::Value args = dynamic::NONE;
dynamic::Map_sptr args = nullptr;
if (lua::gettop(L) > 2) {
args = lua::tovalue(L, 3);
auto value = lua::tovalue(L, 3);
if (auto map = std::get_if<dynamic::Map_sptr>(&value)) {
args = *map;
}
}
level->entities->spawn(def, pos, args);
return 1;
+9 -3
View File
@@ -301,7 +301,7 @@ void scripting::on_entity_spawn(
const EntityDef&,
entityid_t eid,
const std::vector<std::unique_ptr<UserComponent>>& components,
dynamic::Value args,
dynamic::Map_sptr args,
dynamic::Map_sptr saved
) {
auto L = lua::get_main_thread();
@@ -321,8 +321,14 @@ void scripting::on_entity_spawn(
lua::get_from(L, lua::CHUNKS_TABLE, component->name, true);
lua::pushenv(L, *compenv);
lua::pushvalue(L, args);
lua::setfield(L, "ARGS");
if (args != nullptr) {
std::string compfieldname = component->name;
util::replaceAll(compfieldname, ":", "__");
if (auto datamap = args->map(compfieldname)) {
lua::pushvalue(L, datamap);
lua::setfield(L, "ARGS");
}
}
if (saved == nullptr) {
lua::createtable(L, 0, 0);
+1 -1
View File
@@ -82,7 +82,7 @@ namespace scripting {
const EntityDef& def,
entityid_t eid,
const std::vector<std::unique_ptr<UserComponent>>& components,
dynamic::Value args,
dynamic::Map_sptr args,
dynamic::Map_sptr saved
);
void on_entity_despawn(const Entity& entity);