Finished yes/no dialogue for dialogues.
Implemented using function callbacks for each option (positive or negative answer) Implemented dialogue for confirming that player wants to give a gift to NPC rather than just losing the item
This commit is contained in:
parent
a877fe64cb
commit
d6fd49b1ae
44
chat.lua
44
chat.lua
@ -57,17 +57,24 @@ end
|
|||||||
-- New function for getting dialogue formspec
|
-- New function for getting dialogue formspec
|
||||||
function npc.dialogue.show_yes_no_dialogue(prompt,
|
function npc.dialogue.show_yes_no_dialogue(prompt,
|
||||||
positive_answer_label,
|
positive_answer_label,
|
||||||
|
positive_callback,
|
||||||
negative_answer_label,
|
negative_answer_label,
|
||||||
|
negative_callback,
|
||||||
player_name)
|
player_name)
|
||||||
-- Send prompt message to player
|
-- Send prompt message to player
|
||||||
minetest.chat_send_player(player_name, prompt)
|
minetest.chat_send_player(player_name, prompt)
|
||||||
|
|
||||||
local formspec = "size[7, 2.4]"..
|
local formspec = "size[7,2.4]"..
|
||||||
"button[0.5, 0.7; 6, 0.5; yes_option; "..positive_answer_label.." ]"..
|
"button_exit[0.5,0.65;6,0.5;yes_option;"..positive_answer_label.."]"..
|
||||||
"button_exit[0.5, 1.4; 6, 0.5; no_option; "..negative_answer_label.." ]"
|
"button_exit[0.5,1.45;6,0.5;no_option;"..negative_answer_label.."]"
|
||||||
|
|
||||||
-- Create entry into responses table
|
-- Create entry into responses table
|
||||||
npc.dialogue.dialogue_results[player_name] = nil
|
npc.dialogue.dialogue_results.yes_no_dialogue[1] = {
|
||||||
|
name = player_name,
|
||||||
|
response = "",
|
||||||
|
yes_callback = positive_callback,
|
||||||
|
no_callback = negative_callback
|
||||||
|
}
|
||||||
|
|
||||||
minetest.show_formspec(player_name, "advanced_npc:yes_no", formspec)
|
minetest.show_formspec(player_name, "advanced_npc:yes_no", formspec)
|
||||||
end
|
end
|
||||||
@ -143,22 +150,35 @@ local function show_chat_option(npc_name, self, player_name, chat_options, close
|
|||||||
self.order = "follow"
|
self.order = "follow"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Function to get response by player name
|
||||||
|
local function get_yes_no_dialogue_response_by_player_name(player_name)
|
||||||
|
for i = 1,#npc.dialogue.dialogue_results.yes_no_dialogue do
|
||||||
|
local current_result = npc.dialogue.dialogue_results.yes_no_dialogue[i]
|
||||||
|
if current_result.name == player_name then
|
||||||
|
return current_result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
-- Handler for chat formspec
|
-- Handler for chat formspec
|
||||||
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
minetest.register_on_player_receive_fields(function (player, formname, fields)
|
||||||
-- Additional checks for other forms should be handled here
|
-- Additional checks for other forms should be handled here
|
||||||
if formname ~= "advanced_npc:yes_no" then
|
if formname == "advanced_npc:yes_no" then
|
||||||
return false
|
local player_name = player:get_player_name()
|
||||||
end
|
|
||||||
minetest.log(player:get_player_name().." current response: "..dump(npc.dialogue.dialogue_results[player:get_player_name()]))
|
|
||||||
|
|
||||||
if fields then
|
if fields then
|
||||||
minetest.log(dump(fields))
|
local player_response = get_yes_no_dialogue_response_by_player_name(player_name)
|
||||||
if fields.yes_option then
|
if fields.yes_option then
|
||||||
npc.dialogue.dialogue_results[player:get_player_name()] = true
|
player_response.response = true
|
||||||
|
player_response.yes_callback()
|
||||||
elseif fields.no_option then
|
elseif fields.no_option then
|
||||||
npc.dialogue.dialogue_results[player:get_player_name()] = false
|
player_response.response = false
|
||||||
|
player_response.no_callback()
|
||||||
|
end
|
||||||
|
minetest.log(player_name.." chose response: "
|
||||||
|
..dump(get_yes_no_dialogue_response_by_player_name(player_name).response))
|
||||||
end
|
end
|
||||||
minetest.log(player:get_player_name().." after response: "..dump(npc.dialogue.dialogue_results[player:get_player_name()]))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
18
npc.lua
18
npc.lua
@ -599,16 +599,24 @@ mobs:register_mob("advanced_npc:npc", {
|
|||||||
|
|
||||||
minetest.log(dump(self))
|
minetest.log(dump(self))
|
||||||
|
|
||||||
-- Receive gift or start chat
|
-- Receive gift or start chat. If player has no item in hand
|
||||||
if self.can_have_relationship then
|
-- then it is going to start chat directly
|
||||||
|
if self.can_have_relationship and item:to_table() ~= nil then
|
||||||
-- Show dialogue to confirm that player is giving item as gift
|
-- Show dialogue to confirm that player is giving item as gift
|
||||||
npc.dialogue.show_yes_no_dialogue("Do you want to give this item to NPC?",
|
npc.dialogue.show_yes_no_dialogue(
|
||||||
|
"Do you want to give this item to "..self.nametag.."?",
|
||||||
"Yes, I want to give it!",
|
"Yes, I want to give it!",
|
||||||
npc.dialogue.NEGATIVE_ANSWER_LABEL,
|
function()
|
||||||
name)
|
|
||||||
if receive_gift(self, clicker) == false then
|
if receive_gift(self, clicker) == false then
|
||||||
start_chat(self, clicker)
|
start_chat(self, clicker)
|
||||||
end
|
end
|
||||||
|
end,
|
||||||
|
npc.dialogue.NEGATIVE_ANSWER_LABEL,
|
||||||
|
function()
|
||||||
|
start_chat(self, clicker)
|
||||||
|
end,
|
||||||
|
name
|
||||||
|
)
|
||||||
else
|
else
|
||||||
start_chat(self, clicker)
|
start_chat(self, clicker)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user