forked from Mirrorlandia_minetest/minetest
Add LuaEntity on_death callback (#6177)
Add LuaEntity on_death callback This fixes #5474
This commit is contained in:
parent
ba959ce27f
commit
550c0404a8
@ -3864,6 +3864,9 @@ Registered entities
|
||||
* `tool_capabilities`: capability table of used tool (can be `nil`)
|
||||
* `dir`: unit vector of direction of punch. Always defined. Points from
|
||||
the puncher to the punched.
|
||||
`on_death(self, killer)`
|
||||
* Called when the object dies.
|
||||
* `killer`: an `ObjectRef` (can be `nil`)
|
||||
* `on_rightclick(self, clicker)`
|
||||
* `get_staticdata(self)`
|
||||
* Should return a string that will be passed to `on_activate` when
|
||||
|
@ -317,6 +317,9 @@ minetest.register_entity("experimental:testentity", {
|
||||
self.object:remove()
|
||||
hitter:add_to_inventory('craft testobject1 1')
|
||||
end,
|
||||
on_death = function(self, killer)
|
||||
print("testentity.on_death")
|
||||
end
|
||||
})
|
||||
|
||||
--
|
||||
|
@ -582,8 +582,10 @@ int LuaEntitySAO::punch(v3f dir,
|
||||
}
|
||||
}
|
||||
|
||||
if (getHP() == 0)
|
||||
if (getHP() == 0) {
|
||||
m_removed = true;
|
||||
m_env->getScriptIface()->luaentity_on_death(m_id, puncher);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -262,6 +262,34 @@ bool ScriptApiEntity::luaentity_Punch(u16 id,
|
||||
return retval;
|
||||
}
|
||||
|
||||
bool ScriptApiEntity::luaentity_on_death(u16 id, ServerActiveObject *killer)
|
||||
{
|
||||
SCRIPTAPI_PRECHECKHEADER
|
||||
|
||||
int error_handler = PUSH_ERROR_HANDLER(L);
|
||||
|
||||
// Get core.luaentities[id]
|
||||
luaentity_get(L, id);
|
||||
int object = lua_gettop(L);
|
||||
// State: object is at top of stack
|
||||
// Get function
|
||||
lua_getfield(L, -1, "on_death");
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 2); // Pop on_death and entity
|
||||
return false;
|
||||
}
|
||||
luaL_checktype(L, -1, LUA_TFUNCTION);
|
||||
lua_pushvalue(L, object); // self
|
||||
objectrefGetOrCreate(L, killer); // killer reference
|
||||
|
||||
setOriginFromTable(object);
|
||||
PCALL_RES(lua_pcall(L, 6, 1, error_handler));
|
||||
|
||||
bool retval = lua_toboolean(L, -1);
|
||||
lua_pop(L, 2); // Pop object and error handler
|
||||
return retval;
|
||||
}
|
||||
|
||||
// Calls entity:on_rightclick(ObjectRef clicker)
|
||||
void ScriptApiEntity::luaentity_Rightclick(u16 id,
|
||||
ServerActiveObject *clicker)
|
||||
|
@ -41,6 +41,7 @@ public:
|
||||
bool luaentity_Punch(u16 id,
|
||||
ServerActiveObject *puncher, float time_from_last_punch,
|
||||
const ToolCapabilities *toolcap, v3f dir, s16 damage);
|
||||
bool luaentity_on_death(u16 id, ServerActiveObject *killer);
|
||||
void luaentity_Rightclick(u16 id,
|
||||
ServerActiveObject *clicker);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user