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

View File

@ -7,7 +7,7 @@ ready = false
target = -1 target = -1
ARGS = ARGS or {} ARGS = ARGS or {}
local dropitem = ARGS.item or {} local dropitem = ARGS
if SAVED_DATA.item then if SAVED_DATA.item then
dropitem.id = item.index(SAVED_DATA.item) dropitem.id = item.index(SAVED_DATA.item)
dropitem.count = SAVED_DATA.count dropitem.count = SAVED_DATA.count

View File

@ -26,7 +26,7 @@ function on_grounded()
block.set(ix, iy, iz, block.index(blockid)) block.set(ix, iy, iz, block.index(blockid))
else else
local picking_item = block.get_picking_item(block.index(blockid)) local picking_item = block.get_picking_item(block.index(blockid))
local drop = entities.spawn("base:drop", pos, {item={id=picking_item, count=1}}) local drop = entities.spawn("base:drop", pos, {base__drop={id=picking_item, count=1}})
drop.rigidbody:set_vel(vec3.spherical_rand(5.0)) drop.rigidbody:set_vel(vec3.spherical_rand(5.0))
end end
entity:despawn() entity:despawn()

View File

@ -17,7 +17,7 @@ function on_hud_open()
local pvel = {player.get_vel(pid)} local pvel = {player.get_vel(pid)}
local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0}) local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0})
local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE) local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE)
local drop = entities.spawn("base:drop", ppos, {item={ local drop = entities.spawn("base:drop", ppos, {base__drop={
id=itemid, id=itemid,
count=1 count=1
}}) }})

View File

@ -1,6 +1,7 @@
local function update(x, y, z) local function update(x, y, z)
if block.is_replaceable_at(x, y-1, z) then if block.is_replaceable_at(x, y-1, z) then
entities.spawn("base:falling_block", {x+0.5, y+0.5, z+0.5}, {block='base:sand'}) entities.spawn("base:falling_block", {x+0.5, y+0.5, z+0.5},
{base__falling_block={block='base:sand'}})
block.set(x, y, z, 0) block.set(x, y, z, 0)
end end
end end

View File

@ -20,9 +20,12 @@ static int l_spawn(lua::State* L) {
auto defname = lua::tostring(L, 1); auto defname = lua::tostring(L, 1);
auto& def = content->entities.require(defname); auto& def = content->entities.require(defname);
auto pos = lua::tovec3(L, 2); auto pos = lua::tovec3(L, 2);
dynamic::Value args = dynamic::NONE; dynamic::Map_sptr args = nullptr;
if (lua::gettop(L) > 2) { 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); level->entities->spawn(def, pos, args);
return 1; return 1;

View File

@ -301,7 +301,7 @@ void scripting::on_entity_spawn(
const EntityDef&, const EntityDef&,
entityid_t eid, entityid_t eid,
const std::vector<std::unique_ptr<UserComponent>>& components, const std::vector<std::unique_ptr<UserComponent>>& components,
dynamic::Value args, dynamic::Map_sptr args,
dynamic::Map_sptr saved dynamic::Map_sptr saved
) { ) {
auto L = lua::get_main_thread(); 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::get_from(L, lua::CHUNKS_TABLE, component->name, true);
lua::pushenv(L, *compenv); lua::pushenv(L, *compenv);
lua::pushvalue(L, args); if (args != nullptr) {
lua::setfield(L, "ARGS"); 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) { if (saved == nullptr) {
lua::createtable(L, 0, 0); lua::createtable(L, 0, 0);

View File

@ -82,7 +82,7 @@ namespace scripting {
const EntityDef& def, const EntityDef& def,
entityid_t eid, entityid_t eid,
const std::vector<std::unique_ptr<UserComponent>>& components, const std::vector<std::unique_ptr<UserComponent>>& components,
dynamic::Value args, dynamic::Map_sptr args,
dynamic::Map_sptr saved dynamic::Map_sptr saved
); );
void on_entity_despawn(const Entity& entity); void on_entity_despawn(const Entity& entity);

View File

@ -95,7 +95,7 @@ static void initialize_body(
entityid_t Entities::spawn( entityid_t Entities::spawn(
EntityDef& def, EntityDef& def,
glm::vec3 position, glm::vec3 position,
dynamic::Value args, dynamic::Map_sptr args,
dynamic::Map_sptr saved, dynamic::Map_sptr saved,
entityid_t uid) entityid_t uid)
{ {
@ -179,7 +179,7 @@ void Entities::loadEntity(const dynamic::Map_sptr& map) {
throw std::runtime_error("could not read entity - invalid UID"); throw std::runtime_error("could not read entity - invalid UID");
} }
auto& def = level->content->entities.require(defname); auto& def = level->content->entities.require(defname);
spawn(def, {}, dynamic::NONE, map, uid); spawn(def, {}, nullptr, map, uid);
} }
void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) { void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) {

View File

@ -182,7 +182,7 @@ public:
entityid_t spawn( entityid_t spawn(
EntityDef& def, EntityDef& def,
glm::vec3 position, glm::vec3 position,
dynamic::Value args=dynamic::NONE, dynamic::Map_sptr args=nullptr,
dynamic::Map_sptr saved=nullptr, dynamic::Map_sptr saved=nullptr,
entityid_t uid=0); entityid_t uid=0);