diff --git a/fs_edit_general.lua b/fs_edit_general.lua index dd41af7..9c70656 100644 --- a/fs_edit_general.lua +++ b/fs_edit_general.lua @@ -1011,6 +1011,22 @@ yl_speak_up.input_fs_edit_option_related = function(player, formname, fields, }) return end + -- allow to delete unused variables + elseif(fields.delete_unused_variable) then + -- try to delete the variable + local res = yl_speak_up.del_quest_variable(pname, data.variable_name) + local text = "Deleted successfully." + if(not(res)) then + text = "Error: Variable could not be deleted." + end + yl_speak_up.show_fs(player, "msg", { + input_to = "yl_speak_up:"..formspec_input_to, + formspec = "size[8,2]".. + "label[0.2,0.5;Trying to delete variable \"".. + minetest.formspec_escape(tostring(data.variable_name)).. + "\":\n"..text.."]".. + "button[1.5,1.5;2,0.9;back_from_error_msg;Back]"}) + return end -- the player wants to change/edit a precondition or effect diff --git a/quest_api.lua b/quest_api.lua index 4f49293..092bc47 100644 --- a/quest_api.lua +++ b/quest_api.lua @@ -74,13 +74,15 @@ end -- accidentally created or no longer needed variables need to be deleted somehow yl_speak_up.del_quest_variable = function(owner_name, variable_name) - local k = "$ "..tostring(owner_name).." "..tostring(variable_name) + local k = yl_speak_up.restore_complete_var_name(variable_name, owner_name) if(not(owner_name) or not(variable_name)) then return false end + -- actually delete the variable + yl_speak_up.player_vars[ k ] = nil -- a variable was deleted - that deserves a forced save yl_speak_up.save_quest_variables(true) - yl_speak_up.player_vars[ k ] = nil + return true end @@ -250,6 +252,18 @@ yl_speak_up.strip_pname_from_varlist = function(var_list, pname) end +-- (partly) the opposite of the function above - add the name of the player to a variable +-- name again if needed +yl_speak_up.restore_complete_var_name = function(var_name, pname) + local vparts = string.split(var_name or "", " ") + -- has the player name been stripped from the variable name for better readability? + if(vparts and #vparts > 0 and vparts[1] ~= "$") then + return "$ "..tostring(pname).." "..table.concat(vparts, " ") + end + return var_name +end + + -- helper function for saving NPC data; -- this only works if *someone* is currently talking to that NPC yl_speak_up.get_pname_for_n_id = function(n_id) @@ -370,6 +384,7 @@ end -- which NPC do use this variable? yl_speak_up.get_npc_users_of_variable = function(var_name) + var_name = yl_speak_up.restore_complete_var_name(var_name, pname) -- no variable, or nothing stored? then it's not used by any NPC either if(not(var_name) or not(yl_speak_up.player_vars[ var_name ]) @@ -413,6 +428,7 @@ end -- find out where this variable is used in NPCs yl_speak_up.get_list_of_usage_of_variable = function(var_name, pname, check_preconditions, back_button_name, back_button_text) + var_name = yl_speak_up.restore_complete_var_name(var_name, pname) -- which NPC (might be several) is using this variable? local npc_list = yl_speak_up.get_npc_users_of_variable(var_name) -- list of all relevant preconditions, actions and effects