diff --git a/README.md b/README.md index 3de1680..ee3d044 100644 --- a/README.md +++ b/README.md @@ -152,8 +152,9 @@ Additional properties included by the framework. (defaults) on_construct = function(self), on_destruct = function(self, hitter), + on_save = function(npc, to_save), on_update = function(npc), - on_tell = function(npc, sender, message) + on_tell = function(npc, sender, message), on_receive_fields = function(self, fields, sender), register_spawner = true, armor_groups = {immortal=1}, @@ -204,7 +205,15 @@ Note that 'npc' is a NPC object reference, not a LuaEntitySAO reference. The lat npc.object but only when the LuaEntitySAO is loaded. ### on_construct = function(npc) -Called when the NPC object is first created. +Called when the NPC object is first created, or loaded from save. +You should read things from npc. + +### on_save = function(npc, to_save) +Called when the NPC is being saved to file. to_save is the table that is going to be serialized. + +### on_destruct = function(self, hitter) +Called when an NPC is destroyed by punching. Can be used to unload the NPC when defeated by a player. +See the Guard NPC for an example. ### on_update = function(npc) Called every time an NPC object is updated (NPCF_UPDATE_TIME) even when the LuaEntitySAO is not loaded. @@ -217,10 +226,6 @@ however, it does now allow for interaction even when the associated LuaEntitySAO Called when a button is pressed in the NPC's formspec. text fields, dropdown, list and checkbox selections are automatically stored in the metadata table. -### on_destruct = function(self, hitter) -Called when an NPC is destroyed by punching. Can be used to unload the NPC when defeated by a player. -See the Guard NPC for an example. - npcf ---- The global NPC framework namespace. diff --git a/npcf/npcf.lua b/npcf/npcf.lua index 5e38b37..a4391b1 100644 --- a/npcf/npcf.lua +++ b/npcf/npcf.lua @@ -376,6 +376,11 @@ function npcf:save(id) autoload = npc.autoload, } + local def = minetest.registered_entities[ref.name] or {} + if type(def.on_save) == "function" then + def.on_save(npc, ref) + end + local output = io.open(NPCF_DATADIR.."/"..id..".npc", 'w') if output then output:write(minetest.serialize(ref))