delete npc from used_by_npc if they no longer use a particular variable

This commit is contained in:
Sokomine 2021-07-29 22:00:21 +02:00
parent f0d7bbf6c4
commit 144eb53719

View File

@ -441,8 +441,11 @@ yl_speak_up.get_fs_manage_variables = function(player, param)
if(k_long
and yl_speak_up.player_vars[ k_long ]
and yl_speak_up.player_vars[ k_long ][ "$META$"]) then
for key, v in pairs(yl_speak_up.player_vars[ k_long ][ "$META$"][
"used_by_npc" ] or {}) do
local t = yl_speak_up.player_vars[ k_long ][ "$META$"]["used_by_npc" ]
if(type(t) ~= "table") then
t = {}
end
for key, v in pairs(t or {}) do
if(c1==0) then
list_of_npc_users = minetest.formspec_escape(key)
else
@ -451,8 +454,11 @@ yl_speak_up.get_fs_manage_variables = function(player, param)
end
c1 = c1 + 1
end
for key, v in pairs(yl_speak_up.player_vars[ k_long ][ "$META$" ][
"used_by_node_pos" ] or {}) do
local t = yl_speak_up.player_vars[ k_long ][ "$META$"]["used_by_node_pos" ]
if(type(t) ~= "table") then
t = {}
end
for key, v in pairs(t or {}) do
if(c2==0) then
list_of_node_pos_users = minetest.formspec_escape(key)
else
@ -734,6 +740,18 @@ yl_speak_up.update_stored_npc_data = function(n_id, dialog)
last_modified = os.date(),
}
-- make sure all variables have a metadata field
for k, v in pairs(yl_speak_up.player_vars) do
if( yl_speak_up.player_vars[ k ][ "$META$" ]
and yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc) then
if(type(yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc) ~= "table") then
yl_speak_up.player_vars[ k ][ "$META$" ].used_by_npc = {}
end
-- delete old entries (the correct new ones will be set shortly after this)
yl_speak_up.player_vars[ k ][ "$META$"].used_by_npc[ n_id ] = nil
end
end
-- save in the variables' metadata which NPC uses it
-- (this is what we're mostly after - know which variable is used in which NPC)
for k, v in pairs(variables_p) do