From a877fe64cb8e9599a11d3dda9576d373add381a9 Mon Sep 17 00:00:00 2001 From: zorman2000 Date: Tue, 29 Nov 2016 14:44:27 -0500 Subject: [PATCH] Added yes/no dialogue box using normal formspecs. Added handler for formspecs --- chat.lua | 45 ++++++++++++++++++++++++++++++++++++++------- npc.lua | 8 ++++---- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/chat.lua b/chat.lua index c4f386a..a743295 100644 --- a/chat.lua +++ b/chat.lua @@ -28,8 +28,13 @@ local options = {"Question 1","Question 2","Question 3","Question 4"} npc.dialogue = {} -npc.dialogue.YES_GIFT_ANSWER_LABEL = "Yes, give wielded item" -npc.dialogue.NO_GIFT_ANSWER_LAEBL = "Nevermind" +npc.dialogue.YES_GIFT_ANSWER_LABEL_PREFIX = "Yes, give " +npc.dialogue.NEGATIVE_ANSWER_LABEL = "Nevermind" + +-- This table contains the answers of dialogue boxes +npc.dialogue.dialogue_results = { + yes_no_dialogue = {} +} --------------------------------------------------------------------- -- Creates a formspec for dialog @@ -50,15 +55,21 @@ local function create_formspec(options, close_option) end -- New function for getting dialogue formspec -function npc.dialogue.show_yes_no_dialogue(prompt, player_name) - - +function npc.dialogue.show_yes_no_dialogue(prompt, + positive_answer_label, + negative_answer_label, + player_name) -- Send prompt message to player minetest.chat_send_player(player_name, prompt) - dialogue_form:show(player_name) + local formspec = "size[7, 2.4]".. + "button[0.5, 0.7; 6, 0.5; yes_option; "..positive_answer_label.." ]".. + "button_exit[0.5, 1.4; 6, 0.5; no_option; "..negative_answer_label.." ]" - return dialogue_result + -- Create entry into responses table + npc.dialogue.dialogue_results[player_name] = nil + + minetest.show_formspec(player_name, "advanced_npc:yes_no", formspec) end --------------------------------------------------------------------- @@ -131,3 +142,23 @@ local function show_chat_option(npc_name, self, player_name, chat_options, close self.order = "follow" end + +-- Handler for chat formspec +minetest.register_on_player_receive_fields(function (player, formname, fields) + -- Additional checks for other forms should be handled here + if formname ~= "advanced_npc:yes_no" then + return false + end + minetest.log(player:get_player_name().." current response: "..dump(npc.dialogue.dialogue_results[player:get_player_name()])) + + if fields then + minetest.log(dump(fields)) + if fields.yes_option then + npc.dialogue.dialogue_results[player:get_player_name()] = true + elseif fields.no_option then + npc.dialogue.dialogue_results[player:get_player_name()] = false + end + minetest.log(player:get_player_name().." after response: "..dump(npc.dialogue.dialogue_results[player:get_player_name()])) + end + +end) diff --git a/npc.lua b/npc.lua index f30c70f..a82d44f 100755 --- a/npc.lua +++ b/npc.lua @@ -602,10 +602,10 @@ mobs:register_mob("advanced_npc:npc", { -- Receive gift or start chat if self.can_have_relationship then -- Show dialogue to confirm that player is giving item as gift - -- local result = npc.dialogue.show_yes_no_dialogue( - -- "Do you want to give your currently wielded item to "..self.nametag.."?", - -- name) - -- minetest.log("Dialogue outcome: "..dump(result)) + npc.dialogue.show_yes_no_dialogue("Do you want to give this item to NPC?", + "Yes, I want to give it!", + npc.dialogue.NEGATIVE_ANSWER_LABEL, + name) if receive_gift(self, clicker) == false then start_chat(self, clicker) end