User Tools

Site Tools


lua:scripting:examples:sharinginfo

Note that as of today (9/27/2015), while this example is a valid example of savetbl/loadtbl, use of newly added 'shared' table will be more appropriate for many applications that required savetbl/loadtbl previously.

We know how to make 1 mob remember a player by saving their name in a variable or table, but what if we wanted that information available to other mobs or even objects and area scripts? For this we can use 'savetbl' and 'loadtbl'.

Angels in the gauntlet at Angel's Heaven seem pretty indifferent to their fellow angels getting killed. Let's make it so they will all aggro the most recent person who has killed any of them.

A few things we need before we start: angel vnum ( 10709 ) 2 mprog vnums to use ( we'll use 10703 and 10704)

Let's start off simple and just make a death trigger and make sure it's working.

mpedit create 10703
MobProgram Code Created.

medit 10709

addmp 10703 death 100
Mprog Added.

MOBPrograms for [10709]:
 Number Vnum Trigger Phrase
 ------ ---- ------- ------
[    0] 10703   DEATH 100

And just a simple prog.

Vnum:       [10703]
Lua:        True
Security:   9
Code:
say("You killed me!")
Your pierce does INCONCEIVABLE things to An angel!
An angel is DEAD!!
You have an evil thought.
An angel exclaims 'You killed me!'
You hear An angel's death cry.
You get 1701 silver coins and 114 gold coins from the corpse of An angel.

Now for the secret sauce. Note that 'savetbl' takes a table as the 2nd argument, so even if we only want to save just 1 thing we need to put it in a table, but it's no big deal.

say("You killed me!")
say("You will regret it soon!")
local tbl={}
tbl.killer=ch.name
savetbl("angelkill", tbl) -- save the table in a file called angelkill
Your pierce does INCONCEIVABLE things to An angel!
An angel is DEAD!!
You have an evil thought.
An angel exclaims 'You killed me!'
An angel exclaims 'You will regret it soon!'
You hear An angel's death cry.
You get 1368 silver coins and 99 gold coins from the corpse of An angel.

Ok, it LOOKS like it's working, but how do we know if the info got saved? If we did it right, there is a file called “angelkill” saved in the Angel's heaven area directory that has a table with my name in it. Let's confirm.

We'll set up our 2nd trigger and prog now. We'll have a greet trigger that fires 100% of the time.

mpedit create 10704
MobProgram Code Created.

medit 10709

addmp 10704 greet 100
Mprog Added.

MOBPrograms for [10709]:
 Number Vnum Trigger Phrase
 ------ ---- ------- ------
[    0] 10704   GREET 100
[    1] 10703   DEATH 100
local tbl=loadtbl("angelkill")
-- what if the file doesn't exist or is empty or somehow doesn't have a killer?
if tbl==nil or tbl.killer==nil then
  return -- exit the script
end
 
tprint(tbl)
south

The south western corner [Room 10780 inside]
  On the wall here there is a parchment.  It reads "To comfort and to
bless, to find a balm for woe, to tend the lone and featherless, is an
angel's work below".  This text explains what angels are sent to do when
they visit the mortal realm.

[Exits: north east]
An angel is here training in case of a war.
An angel is here training in case of a war.
An angel is here training in case of a war.
An angel says '"killer"="Ranglor"
'
An angel says '"killer"="Ranglor"
'
An angel says '"killer"="Ranglor"
'

Looks like we're good to go! Now to have them attack.

Vnum:       [10704]
Lua:        True
Security:   9
Code:
local tbl=loadtbl("angelkill")
-- what if the file doesn't exist or is empty or somehow doesn't have a killer?
if tbl==nil or tbl.killer==nil then
  return -- exit the script
end
 
say(ch.name..", you murderer!")
kill(ch.name)

And an update to 10703

Vnum:       [10703]
Lua:        True
Security:   9
Code:
say("You killed me!")
say("You will regret it soon!")
local tbl={}
tbl.killer=ch.name
savetbl("angelkill", tbl) -- save the table in a file called angelkill
 
-- Any angels in the room attack!
for _,char in pairs(mob.room.people) do
  if char.vnum==mob.vnum then
    char:say(ch.name..", you murderer!")
    char:kill(ch.name)
  end
end

Final notes: savetbl and loadtbl will always save/load in area specific directory (angel's heaven dir in this case) according to HOME area, not current area. It's the same directory whether called from mob, object, or area so it means you can share info between any type of prog, but only in the same area.

lua/scripting/examples/sharinginfo.txt · Last modified: 2015/09/28 04:22 by vodur