Was the best way to implent this? For example it will only say "It's nice I know." If you first ask "hat" and then answer "yes".
This is how I'm thinking:
Topic = 0
local messages = {
{"name", "I'm Bob, the owner of this little shop."},
{"job", "I sell stuff."},
{"hat|head", "You like my hat?", Topic = 1},
{Topic == 1, "yes", "It's nice I know."}}
for i, a in ipairs(messages) do
if isMsg(a[1], msg) then
makeSay(a[2])
return true
end
end
Functions
function isMsg(messages, msg)
local keys = split(messages, "|")
if getmsg(keys, msg) then
return true
end
return false
end
function table.contains(table, element)
for _, value in ipairs(table) do
if value == element then
return true
end
end
return false
end
function getmsg(t, msg)
for v, a in ipairs(t) do
if msgcontains(msg, a) then
return true
end
end
return false
end
function keyContains(t, msg)
for v, t in ipairs(t) do
if t == msg then
return true
end
end
return false
end
function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end