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
ARGS = ARGS or {}
local dropitem = ARGS.item or {}
local dropitem = ARGS
if SAVED_DATA.item then
dropitem.id = item.index(SAVED_DATA.item)
dropitem.count = SAVED_DATA.count

View File

@ -26,7 +26,7 @@ function on_grounded()
block.set(ix, iy, iz, block.index(blockid))
else
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))
end
entity:despawn()

View File

@ -17,7 +17,7 @@ function on_hud_open()
local pvel = {player.get_vel(pid)}
local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0})
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,
count=1
}})

View File

@ -1,6 +1,7 @@
local function update(x, y, z)
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)
end
end

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;

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

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

View File

@ -95,7 +95,7 @@ static void initialize_body(
entityid_t Entities::spawn(
EntityDef& def,
glm::vec3 position,
dynamic::Value args,
dynamic::Map_sptr args,
dynamic::Map_sptr saved,
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");
}
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) {

View File

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