User Tools

Site Tools


lua:scripting:examples:speechmenu

Script example using the “trigger” variable to create a menu for players. With this script, the mob will remember any object a player gives to it, be able to list the objects any given player has given, and be able give back specific objects when asked to.

-- Menu test helper code [320]
mt={}
mt.list={}
 
function mt.menu()
  say("Just say the word!")
  say("list        - a list of the objects you gave me")
  say("give <item> - I'll give you the object back")
end
 
function mt.parse(ch,trigger)
  if trigger=="list" then
    mt.showlist(ch)
    return
  end
 
  local s,f=string.find(trigger,"give")
  if s==1 then -- if give is the first word
    mt.give(ch,string.sub(trigger,f+2)) -- only send the rest of the arg
    return
  end
 
  mt.menu()
end
 
function mt.showlist(ch)
  if not mt.list[ch.name] then
    say("You haven't given me anything...")
    return
  end
 
  if table.getn(mt.list[ch.name])<1 then
    say("I don't have anything of yours...")
    return
  end
 
  for _,v in ipairs(mt.list[ch.name]) do
    say(v.sdesc)
  end
end
 
function mt.give(ch,words)
  if not mt.list[ch.name] then
    say("You haven't given me anything...")
    return
  end
 
  for i,v in ipairs(mt.list[ch.name]) do
    if string.find(v.name, words) then
      mt.gave={ name=ch.name, sdesc=v.sdesc, index=i}
      mdo(string.format("give '%s' %s", v.name, ch.name))
      -- mdo(string.format("drop '%s'", v.name)) -- in case ch had a full inventory
      -- table.remove(mt.list[ch.name],i) -- remove it from the table
      return
    end
  end
 
  -- if we're here, didn't find it
  say("Sorry, you never gave me " .. words .. "...")
end
 
function mt.checkgive(trigger)
  if mt.gave==nil then
    return
  end
  if string.find(trigger, string.format("You give %s to", mt.gave.sdesc) ) then
    table.remove(mt.list[mt.gave.name], mt.gave.index)
    say("There ya go.")
  else
    say("Hmmm, I couldn't give it to you...is your inventory full?")
  end
 
  mt.gave=nil
end  
 
function mt.receive(obj,ch)
  if not mt.list[ch.name] then
    mt.list[ch.name]={}
  end
 
  table.insert(mt.list[ch.name],{ name=obj.name, sdesc=obj.shortdescr})
  say("Ok, I'll hold onto " .. obj.shortdescr .. " for you " .. ch.name .. ".")
 
end
 
mt.loaded=true
-- End Menu test helper code
-- Menu test say * [321]
if not mt or not mt.loaded then
  loadprog(320)
end
 
mt.parse(ch,trigger)
-- End Menu test say *
-- Menu test give * [322]
if not mt or not mt.loaded then
  loadprog(320)
end
 
mt.receive(obj1,ch)
-- End Menu test give *
-- Menu test act * [323]
if not mt or not mt.loaded then
  loadprog(320)
end
 
mt.checkgive(trigger)
-- End Menu test act *

You say 'list'
lua test 20 says 'You haven't given me anything...'
You give a pile of coal and rocks to lua test 20.
lua test 20 says 'Ok, I'll hold onto a pile of coal and rocks for you Vodur.'
You give a pile of coal and rocks to lua test 20.
lua test 20 says 'Ok, I'll hold onto a pile of coal and rocks for you Vodur.'
El Ranglor gives Surf and Turf to lua test 20.
lua test 20 says 'Ok, I'll hold onto Surf and Turf for you Ranglor.'
El Ranglor says 'list'
lua test 20 says 'Surf and Turf'
You say 'list'
lua test 20 says 'a pile of coal and rocks'
lua test 20 says 'a pile of coal and rocks'
You give a pile of coal and rocks to El Ranglor.
El Ranglor has his hands full.
El Ranglor says 'give surf'
lua test 20 asks 'Hmmm, I couldn't give it to you...is your inventory full?'
El Ranglor drops a pile of coal and rocks.
El Ranglor says 'surf'
lua test 20 exclaims 'Just say the word!'
lua test 20 says 'list        - a list of the objects you gave me'
lua test 20 says 'give <item> - I'll give you the object back'
El Ranglor says 'give surf'
lua test 20 says 'There ya go.'
Lua test 20 gives Surf and Turf to El Ranglor.
El Ranglor says 'list'
lua test 20 says 'I don't have anything of yours...'
lua/scripting/examples/speechmenu.txt · Last modified: 2014/12/24 05:45 by vodur