• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

[Lua] How do I get the answer that I gave to the NPC?

Jack Parsons

Member
Joined
Mar 8, 2016
Messages
32
Reaction score
12
Location
São Paulo State, Brazil
I'm having difficulties finding how to access the answer that I gave to the NPC. Let us use this script as an example:

Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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 tell(cid, message, keywords, parameters, node)
    local npcHandler = parameters.npcHandler
    if npcHandler == nil then
        error("tell called without any npcHandler instance.")
    end
    if not npcHandler:isFocused(cid) then
        return false
    end
    npcHandler:say(parameters.text, cid)
    npcHandler:resetNpc(cid)
    return true
end

local node = keywordHandler:addKeyword({'creature'}, tell, {npcHandler = npcHandler, onlyFocus = true, text = 'Tell me the name of a {creature}, and I'll repeat it to you...'})
node:addChildKeyword({ANSWER_THAT_I_GAVE}, someFunc, {})
npcHandler:addModule(FocusModule:new())

Pretty straightfoward. As you can see, I say "creature" to the NPC and that triggers the function tell, and then he just asks me to say the name of some creature. The problem is, I want to use the name that I gave him as an answer to be the trigger-word for the next function. How can I access the answer that I gave to the NPC through the node that is returned by the addKeyword()?
 
I'm having difficulties finding how to access the answer that I gave to the NPC. Let us use this script as an example:

Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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 tell(cid, message, keywords, parameters, node)
    local npcHandler = parameters.npcHandler
    if npcHandler == nil then
        error("tell called without any npcHandler instance.")
    end
    if not npcHandler:isFocused(cid) then
        return false
    end
    npcHandler:say(parameters.text, cid)
    npcHandler:resetNpc(cid)
    return true
end

local node = keywordHandler:addKeyword({'creature'}, tell, {npcHandler = npcHandler, onlyFocus = true, text = 'Tell me the name of a {creature}, and I'll repeat it to you...'})
node:addChildKeyword({ANSWER_THAT_I_GAVE}, someFunc, {})
npcHandler:addModule(FocusModule:new())

Pretty straightfoward. As you can see, I say "creature" to the NPC and that triggers the function tell, and then he just asks me to say the name of some creature. The problem is, I want to use the name that I gave him as an answer to be the trigger-word for the next function. How can I access the answer that I gave to the NPC through the node that is returned by the addKeyword()?
You might be able to trick the keywordhandler with this:

Code:
node:addChildKeyword({"."}, someFunc, {})
 
I've had people help me with this in the past, and I've used it a couple of times the same way.
I'm not certain if there is a better way or not. There probably is, but I've found it works for me.

Use a table to hold the value 'msg', and have the table updated somewhere in the script.
https://otland.net/threads/xikinis-...e-0-3-7-0-3-6-0-4.234306/page-14#post-2330703

I eventually solved it doing exactly that (Parsing / filtering the message). I've posted the NPC I was creating already: https://otland.net/threads/tfs-1-0-summon-any-creature-in-the-game-for-a-bloody-price.241608/

I've used a little split function:

Code:
function split(inputstr, sep)
    if sep == nil then
        sep = "%s"
    end
    local t={} ; i=1
    for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
        t[i] = str
        i = i + 1
    end
    return t
end

In that way, I've just passed the message as an argument and got a table (And then, it was easy to get what I've said accessing the indexes).
 
Code:
function Answer(cid, message, keywords, parameters, node)
    local npcHandler = parameters.npcHandler
    if npcHandler == nil then
        error("Answer called without any npcHandler instance.")
    end

    if not npcHandler:isFocused(cid) then
        return false
    end

    npcHandler:say("You said " .. message, cid)
    return true
end

local node = keywordHandler:addKeyword({'keyword'}, tell, {npcHandler = npcHandler, onlyFocus = true, text = 'Say something..'})
    node:addChildKeyword({''}, Answer, {npcHandler = npcHandler})
 
Back
Top