guiismiti
Well-Known Member
- Joined
- May 19, 2014
- Messages
- 315
- Solutions
- 3
- Reaction score
- 68
Hello,
I'm having a problem with an NPC.
The original code is too long, so, I cut most of the generalConfig array and the entire addonItemsIds array.
The code is working, but the value of config gets mixed when two players talk to the NPC.
The problem (example):
- Player #1 says hi, addon, brotherhood
- This will define config as generalConfig["brotherhood"]
- If Player #2 says hi, addon, barbarian, it will not only set a value for config for Player #2, it will also change the value of config of Player #1 to generalConfig["barbarian"]
Can anybody help me?
EDITED: I tried placing "local config" inside the main function, but its value gets lost. I've also tried to use config as non local, but it also mixes the values between players.
I'm having a problem with an NPC.
The original code is too long, so, I cut most of the generalConfig array and the entire addonItemsIds array.
The code is working, but the value of config gets mixed when two players talk to the NPC.
Code:
local generalConfig = {
["citizen"] = {
id = {male = 128, female = 136},
items = {
{addonItemsIds.minotaurLeather, 100},
{addonItemsIds.chickenFeather, 100},
{addonItemsIds.honeyComb, 50},
{addonItemsIds.legionHelmet, 1}
}
},
["hunter"] = {
id = {male = 129, female = 137},
items = {
{addonItemsIds.lizardLeather, 100},
{addonItemsIds.redDragonLeather, 100},
{addonItemsIds.enchantedChickenWing, 5},
{addonItemsIds.pieceOfRoyalSteel, 1},
{addonItemsIds.pieceOfDraconianSteel, 1},
{addonItemsIds.pieceOfHellSteel, 1},
{addonItemsIds.sniperGloves, 1}
}
}
}
local config
local function creatureSayCallback(cid, type, msg)
if not npcHandler:isFocused(cid) then
return false
end
local player = Player(cid)
local answer
msg = msg:lower()
if msg == "valdisnei" then
npcHandler:say("Valdisnei sells items for addons. He is at the Yalahar Palace.", cid)
npcHandler.topic[cid] = 0
elseif ((msg == "addon") or (msg == "addons")) then
answer = "And which addons would you like? I can offer you "
for outfitName, configs in pairs(generalConfig) do
answer = answer .. "{" .. outfitName .. "}"
if outfitName ~= "assassin" then
answer = answer .. ", "
else
answer = answer .. "."
end
end
npcHandler:say(answer, cid)
npcHandler.topic[cid] = 1
elseif npcHandler.topic[cid] == 1 then
-- player has chosen an outfit
config = generalConfig[msg]
-- change mage for summoner and summoner for mage if player is female
if ((msg == "mage") and (player:getSex() == 1)) then
config = generalConfig["summoner"]
elseif ((msg == "summoner") and (player:getSex() == 1)) then
config = generalConfig["mage"]
end
if config then
answer = "For this addon, I need "
for i = 1, #config.items do
local itemType = ItemType(config.items[i][1])
answer = answer .. (i ~= 1 and (i == #config.items and " and " or ", ") or "") .. (config.items[i][2] > 1 and config.items[i][2] or itemType:getArticle()) .. " " .. (config.items[i][2] > 1 and itemType:getPluralName() or itemType:getName())
end
answer = answer .. ". Do you have it?"
npcHandler:say(answer, cid)
npcHandler.topic[cid] = 2
else
npcHandler:say("That is not a valid outfit.", cid)
npcHandler.topic[cid] = 0
end
elseif npcHandler.topic[cid] == 2 then
if msg == "yes" then
-- check if player already has outfit
if not player:hasOutfit(config.id.male, 3) then
-- check if player has items
local hasItems = true
for i = 1, #config.items do
if player:getItemCount(config.items[i][1]) < config.items[i][2] then
hasItems = false
end
end
if hasItems then
-- remove items
for i = 1, #config.items do
player:removeItem(config.items[i][1], config.items[i][2])
end
-- give addon
player:addOutfitAddon(config.id.male, 3)
player:addOutfitAddon(config.id.female, 3)
npcHandler:say("Congratulation! Enjoy your new addon.", cid)
else
npcHandler:say("You do not have the items with you.", cid)
end
npcHandler.topic[cid] = 0
else
npcHandler:say("You already have this addon.", cid)
npcHandler.topic[cid] = 0
end
else
npcHandler:say("Okay then.", cid)
npcHandler.topic[cid] = 0
end
end
return true
end
The problem (example):
- Player #1 says hi, addon, brotherhood
- This will define config as generalConfig["brotherhood"]
- If Player #2 says hi, addon, barbarian, it will not only set a value for config for Player #2, it will also change the value of config of Player #1 to generalConfig["barbarian"]
Can anybody help me?
EDITED: I tried placing "local config" inside the main function, but its value gets lost. I've also tried to use config as non local, but it also mixes the values between players.