diff --git a/functions.lua b/functions.lua index 7c0e164..2072b1d 100644 --- a/functions.lua +++ b/functions.lua @@ -1756,54 +1756,54 @@ yl_speak_up.edit_mode_apply_changes = function(pname, fields) -- changes to options are not possible if there are none if(dialog.n_dialogs[ d_id ].d_options) then - -- detect changes to text_option_: text for option - for k, v in pairs(dialog.n_dialogs[ d_id ].d_options) do - if( fields[ "text_option_"..k ] - and fields[ "text_option_"..k ] ~= v.o_text_when_prerequisites_met ) then - -- TODO: delete option if text is empty - --minetest.chat_send_player("singleplayer", "CHANGED: Text for option "..tostring(k).." was changed\n from: "..tostring(v.o_text_when_prerequisites_met).."\n to: "..tostring(fields[ "text_option_"..k])) - -- store that there have been changes to this npc - table.insert(yl_speak_up.npc_was_changed[ yl_speak_up.edit_mode[pname] ], - "Dialog "..d_id..": The text for option "..tostring(k).." was changed from \""..tostring(v.o_text_when_prerequisites_met).."\" to \""..tostring(fields[ "text_option_"..k]).."\".") - -- actually change the text of the option - dialog.n_dialogs[ d_id ].d_options[ k ].o_text_when_prerequisites_met = fields[ "text_option_"..k ] + -- detect changes to text_option_: text for option + for k, v in pairs(dialog.n_dialogs[ d_id ].d_options) do + if( fields[ "text_option_"..k ] + and fields[ "text_option_"..k ] ~= v.o_text_when_prerequisites_met ) then + -- TODO: delete option if text is empty + --minetest.chat_send_player("singleplayer", "CHANGED: Text for option "..tostring(k).." was changed\n from: "..tostring(v.o_text_when_prerequisites_met).."\n to: "..tostring(fields[ "text_option_"..k])) + -- store that there have been changes to this npc + table.insert(yl_speak_up.npc_was_changed[ yl_speak_up.edit_mode[pname] ], + "Dialog "..d_id..": The text for option "..tostring(k).." was changed from \""..tostring(v.o_text_when_prerequisites_met).."\" to \""..tostring(fields[ "text_option_"..k]).."\".") + -- actually change the text of the option + dialog.n_dialogs[ d_id ].d_options[ k ].o_text_when_prerequisites_met = fields[ "text_option_"..k ] + end end - end - -- detect changes to d_id_: target dialog for option - -- TODO: this may also point to a new dialog - for k, v in pairs(dialog.n_dialogs[ d_id ].d_options) do - if( fields[ "d_id_"..k ] and v.o_results) then - for kr, vr in pairs(v.o_results) do - if( vr.r_type == "dialog" and vr.r_value and vr.r_value ~= fields[ "d_id_"..k ]) then - --minetest.chat_send_player("singleplayer", "CHANGED: Target dialog for option "..tostring(k).." was changed\n from: "..tostring(vr.r_value).."\n to: "..tostring(fields[ "d_id_"..k])) - -- store that there have been changes to this npc - table.insert(yl_speak_up.npc_was_changed[ yl_speak_up.edit_mode[pname] ], - "Dialog "..d_id..": The target dialog for option "..tostring(k).." was changed from "..tostring(vr.r_value).." to "..tostring(fields[ "d_id_"..k])..".") - -- actually change the target dialog - vr.r_value = fields[ "d_id_"..k ] + -- detect changes to d_id_: target dialog for option + -- TODO: this may also point to a new dialog + for k, v in pairs(dialog.n_dialogs[ d_id ].d_options) do + if( fields[ "d_id_"..k ] and v.o_results) then + for kr, vr in pairs(v.o_results) do + if( vr.r_type == "dialog" and vr.r_value and vr.r_value ~= fields[ "d_id_"..k ]) then + --minetest.chat_send_player("singleplayer", "CHANGED: Target dialog for option "..tostring(k).." was changed\n from: "..tostring(vr.r_value).."\n to: "..tostring(fields[ "d_id_"..k])) + -- store that there have been changes to this npc + table.insert(yl_speak_up.npc_was_changed[ yl_speak_up.edit_mode[pname] ], + "Dialog "..d_id..": The target dialog for option "..tostring(k).." was changed from "..tostring(vr.r_value).." to "..tostring(fields[ "d_id_"..k])..".") + -- actually change the target dialog + vr.r_value = fields[ "d_id_"..k ] + end end - end - -- this might be the first option - elseif( fields["d_id_"..k ]) then - --minetest.chat_send_player("singleplayer", "CHANGED: Target dialog for option "..tostring(k).." was added\n as: "..tostring(fields[ "d_id_"..k])) - -- store that a new option has been added to this dialog - table.insert(yl_speak_up.npc_was_changed[ yl_speak_up.edit_mode[pname] ], - "Dialog "..d_id..": The target dialog for option "..tostring(k).." was changed from -default- to "..tostring(fields[ "d_id_"..k])..".") + -- this might be the first option + elseif( fields["d_id_"..k ]) then + --minetest.chat_send_player("singleplayer", "CHANGED: Target dialog for option "..tostring(k).." was added\n as: "..tostring(fields[ "d_id_"..k])) + -- store that a new option has been added to this dialog + table.insert(yl_speak_up.npc_was_changed[ yl_speak_up.edit_mode[pname] ], + "Dialog "..d_id..": The target dialog for option "..tostring(k).." was changed from -default- to "..tostring(fields[ "d_id_"..k])..".") - -- create a new result (first the id, then the actual result) - local future_r_id = "r_" .. find_next_id(dialog.n_dialogs[d_id].d_options[k].o_results) - if future_r_id == "r_1" then - dialog.n_dialogs[d_id].d_options[k].o_results = {} + -- create a new result (first the id, then the actual result) + local future_r_id = "r_" .. find_next_id(dialog.n_dialogs[d_id].d_options[k].o_results) + if future_r_id == "r_1" then + dialog.n_dialogs[d_id].d_options[k].o_results = {} + end + -- actually store the new result + dialog.n_dialogs[d_id].d_options[k].o_results[future_r_id] = { + r_id = future_r_id, + r_type = "dialog", + r_value = fields[ "d_id_"..k ]} end - -- actually store the new result - dialog.n_dialogs[d_id].d_options[k].o_results[future_r_id] = { - r_id = future_r_id, - r_type = "dialog", - r_value = fields[ "d_id_"..k ]} end end - end -- add a new dialog; either via "+" button or "New dialog" in dialog dropdown menu -- this has to be done after all the other changes because those (text changes etc.) still