From c231ee4e3e47f75d9456a4657d99fc274a2548a9 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Wed, 5 Jan 2022 11:56:24 +0100 Subject: [PATCH] Assign ID before calling on_activate --- main.lua | 82 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/main.lua b/main.lua index bf1254f..9f41b7b 100644 --- a/main.lua +++ b/main.lua @@ -184,6 +184,47 @@ function register_entity(name, def) obj:set_velocity(vector.multiply(vector.divide(vel, len))) end end + end + local props_staticdata = props.staticdata + if props_staticdata then + local implementation + if type(props_staticdata) == "table" then + implementation = props_staticdata + else + implementation = ({ + json = { + serializer = minetest.write_json, + deserializer = minetest.parse_json + }, + lua = { + serializer = minetest.serialize, + deserializer = minetest.deserialize + } + })[props_staticdata] + end + local serializer = implementation.serializer + local deserializer = implementation.deserializer + local old_on_activate = on_activate + function on_activate(self, staticdata, dtime) + self._ = (staticdata ~= "" and deserializer(staticdata)) or {} + old_on_activate(self, staticdata, dtime) + end + function def.get_staticdata(self) + return serializer(self._) + end + end + if props.id then + assert(props_staticdata) + local old_on_activate = on_activate + function on_activate(self, staticdata, dtime) + if not self._.id then + highest_id = highest_id + 1 + self._.id = highest_id + storage:set_int("highest_id", highest_id) + end + entities_by_id[self._.id] = self + old_on_activate(self, staticdata, dtime) + end end -- TODO consider HACK for #10158 if props.moveresult then @@ -280,47 +321,6 @@ function register_entity(name, def) self._last_velocity = velocity end end - local props_staticdata = props.staticdata - if props_staticdata then - local implementation - if type(props_staticdata) == "table" then - implementation = props_staticdata - else - implementation = ({ - json = { - serializer = minetest.write_json, - deserializer = minetest.parse_json - }, - lua = { - serializer = minetest.serialize, - deserializer = minetest.deserialize - } - })[props_staticdata] - end - local serializer = implementation.serializer - local deserializer = implementation.deserializer - local old_on_activate = on_activate - function on_activate(self, staticdata, dtime) - self._ = (staticdata ~= "" and deserializer(staticdata)) or {} - old_on_activate(self, staticdata, dtime) - end - function def.get_staticdata(self) - return serializer(self._) - end - end - if props.id then - assert(props_staticdata) - local old_on_activate = on_activate - function on_activate(self, staticdata, dtime) - old_on_activate(self, staticdata, dtime) - if not self._.id then - highest_id = highest_id + 1 - self._.id = highest_id - storage:set_int("highest_id", highest_id) - end - entities_by_id[self._.id] = self - end - end def.on_activate = on_activate def.on_step = on_step minetest.register_entity(name, def)