- Joined
- Nov 17, 2010
- Messages
- 6,830
- Solutions
- 586
- Reaction score
- 5,413
0.3.7
Hey everyone.
Trying to create a task npc.
Instead of writing out each task separately, I thought it would be a good idea to just stuff them into a table, and make it nice and generic, and learn a thing or two in the process.
Well I'm here, because I'm apparently not learning fast enough.
Whenever I call the value x by itself, it works, however if I try to play around with it at all.. well it just screams at me.
This works.
This works.
This does not work, and pulls the error below
--------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------
Can someone point me in the right direction, or show me what I'm doing wrong?
Thanks!
Sorry for the tables, I promise they look better with real tabs
Hey everyone.
Trying to create a task npc.
Instead of writing out each task separately, I thought it would be a good idea to just stuff them into a table, and make it nice and generic, and learn a thing or two in the process.
Well I'm here, because I'm apparently not learning fast enough.
Whenever I call the value x by itself, it works, however if I try to play around with it at all.. well it just screams at me.
This works.
Code:
elseif x then
selfSay("Would like you like to start the task?", cid)
Code:
elseif x and getPlayerStorageValue(cid, x.storage) < 1 then
selfSay("Would like you like to start the task?", cid)
Code:
elseif x and getPlayerStorageValue(cid, x.storage) < 1 then
selfSay("Would like you like to start the ".. x .." task?", cid)
--------------------------------------------------------------------------------------------------------------------------------------------------
Can someone point me in the right direction, or show me what I'm doing wrong?
Thanks!
Sorry for the tables, I promise they look better with real tabs
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
local xmsg = {}
function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
function onThink() npcHandler:onThink() end
function greet(cid)
talkState[cid] = 0
return true
end
function getNpcName()
return getCreatureName(getNpcId())
end
function creatureSayCallback(cid, type, msg)
if(not npcHandler:isFocused(cid)) then
return false
end
local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
local task = {
[1] = {thing = "wasps", storageID = 45003, valueMin = -1, valueMax = 1},
[2] = {thing = "trolls", storageID = 45005, valueMin = -1, valueMax = 1},
[3] = {thing = "goblins", storageID = 45007, valueMin = -1, valueMax = 1},
[4] = {thing = "slimes", storageID = 45009, valueMin = -1, valueMax = 1},
[5] = {thing = "rotworms", storageID = 45011, valueMin = -1, valueMax = 1},
[6] = {thing = "amazons", storageID = 45013, valueMin = -1, valueMax = 1},
[7] = {thing = "valktries", storageID = 45015, valueMin = -1, valueMax = 1},
[8] = {thing = "larva", storageID = 45017, valueMin = -1, valueMax = 1},
[9] = {thing = "orcs", storageID = 45019, valueMin = -1, valueMax = 1},
[10] = {thing = "dwarves", storageID = 45021, valueMin = -1, valueMax = 1},
[11] = {thing = "minotaurs", storageID = 45023, valueMin = -1, valueMax = 1},
[12] = {thing = "ghouls", storageID = 45025, valueMin = -1, valueMax = 1},
[13] = {thing = "scarabs", storageID = 45027, valueMin = -1, valueMax = 1},
[14] = {thing = "ancient scarabs", storageID = 45029, valueMin = -1, valueMax = 1},
[15] = {thing = "demons skeletons", storageID = 45031, valueMin = -1, valueMax = 1},
[16] = {thing = "cyclops", storageID = 45033, valueMin = -1, valueMax = 1},
[17] = {thing = "vampires", storageID = 45035, valueMin = -1, valueMax = 1},
[18] = {thing = "necromancers", storageID = 45037, valueMin = -1, valueMax = 1},
[19] = {thing = "giant spiders", storageID = 45039, valueMin = -1, valueMax = 1},
[20] = {thing = "dragons", storageID = 45041, valueMin = -1, valueMax = 1},
[21] = {thing = "dragon lords", storageID = 45043, valueMin = -1, valueMax = 1},
[22] = {thing = "demons", storageID = 45045, valueMin = -1, valueMax = 1}
}
local config = {
["wasps"] = {storage = 45003, experience = 1000, reward = 2160, count = 1},
["trolls"] = {storage = 45005, experience = 1000, reward = 2160, count = 1},
["goblins"] = {storage = 45007, experience = 1000, reward = 2160, count = 1},
["slimes"] = {storage = 45009, experience = 1000, reward = 2160, count = 1},
["rotworms"] = {storage = 45011, experience = 1000, reward = 2160, count = 1},
["amazons"] = {storage = 45013, experience = 1000, reward = 2160, count = 1},
["valktries"] = {storage = 45015, experience = 1000, reward = 2160, count = 1},
["larva"] = {storage = 45017, experience = 1000, reward = 2160, count = 1},
["orcs"] = {storage = 45019, experience = 1000, reward = 2160, count = 1},
["dwarves"] = {storage = 45021, experience = 1000, reward = 2160, count = 1},
["minotaurs"] = {storage = 45023, experience = 1000, reward = 2160, count = 1},
["ghouls"] = {storage = 45025, experience = 1000, reward = 2160, count = 1},
["scarabs"] = {storage = 45027, experience = 1000, reward = 2160, count = 1},
["ancient scarabs"] = {storage = 45029, experience = 1000, reward = 2160, count = 1},
["demons skeletons"] = {storage = 45031, experience = 1000, reward = 2160, count = 1},
["cyclops"] = {storage = 45033, experience = 1000, reward = 2160, count = 1},
["vampires"] = {storage = 45035, experience = 1000, reward = 2160, count = 1},
["necromancers"] = {storage = 45037, experience = 1000, reward = 2160, count = 1},
["giant spiders"] = {storage = 45039, experience = 1000, reward = 2160, count = 1},
["dragons"] = {storage = 45041, experience = 1000, reward = 2160, count = 1},
["dragon lords"] = {storage = 45043, experience = 1000, reward = 2160, count = 1},
["demons"] = {storage = 45045, experience = 1000, reward = 2160, count = 1}
}
local x = config[msg:lower()]
if msgcontains(msg, "task") then
local npcmsg = ""
for i = 1, #task do
local storage = getPlayerStorageValue(cid, task[i].storageID)
if storage <= task[i].valueMax and storage >= task[i].valueMin then
if npcmsg ~= "" then
npcmsg = npcmsg.. ", "
end
npcmsg = npcmsg..task[i].thing
end
end
selfSay("Tasks available: " ..npcmsg.. ".", cid)
elseif x then
if getPlayerStorageValue(cid, x.storage) < 1 then
selfSay("Would like you like to start the ".. x .." task?", cid)
talkState[talkUser] = 1
xmsg[cid] = msg
elseif getPlayerStorageValue(cid, x.storage) == 1 then
selfSay("Have you completed your task?", cid)
talkState[talkUser] = 2
xmsg[cid] = msg
elseif getPlayerStorageValue(cid, x.storage) > 1 then
selfSay("You have already completed this task. Thank you.", cid)
end
elseif msgcontains(msg, "yes") and talkState[talkUser] == 1 then
local x = config[xmsg[cid]:lower()]
selfSay("Great! Once you have killed 50 ".. x .." come back and see me.", cid)
setPlayerStorageValue(cid, x.storage, 1)
talkState[talkUser] = 0
elseif msgcontains(msg, "yes") and talkState[talkUser] == 2 then
local x = config[xmsg[cid]:lower()]
if getPlayerStorageValue(cid, x.storage + 1) == 30 then
selfSay("Wonderful! Here is your reward.", cid)
setPlayerStorageValue(cid, x.storage, 2)
doPlayerAddExperience(cid, x.experience)
doPlayerAddItem(cid, x.reward, x.count, true)
else
selfSay("You have not killed enough ".. x ..". Come back when you have killed more.", cid)
end
talkState[talkUser] = 0
elseif msgcontains(msg, "reset") then
setPlayerStorageValue(cid, 45003, -1)
setPlayerStorageValue(cid, 45004, -1)
end
return true
end
npcHandler:setCallback(CALLBACK_GREET, greet)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())