User Tools

Site Tools


lua:sharedtable

This is an old revision of the document!


'shared' table

Any prog/script runs inside a script environment. See script environemnts for more details. This means that even "global" variables or functions declared in a script are only global to its environment. To overcome this limitation we have the 'shared' table to share data and functions between all scripts in an area.

The 'shared' table is a table that is common to all environments associated with game objects in a certain area. This means that each mob/room/object in a given area that runs a script that points to 'shared' will actually be referencing the same table, and the same is true for the area itself.

Using the 'shared' table is as simple as using a typical lua table:

​shared.message="Hello world!"

One can clear/reset the shared table by simply setting its value as an empty table:

shared={}

Note that because players do not belong to a specific area, scripts run in a player environment do not have access to any shared table. This is important to consider when running scripts with luai/mprun or calling CH:loadprog(), CH:loadfunction, or CH:loadscript.

Shared functions

Just like any other lua table, 'shared' table can also contain functions. Thus 'shared' table is an ideal place to store helper functions that would be in different scripts throughout an area.

<code>
-- FUNCTION FOR DETERMINING GUILD AFFILIATION
-- send ch.class
function shared.find_guild(class)
  if class == "warrior"
  or class == "gladiator"
  or class == "paladin"
  or class == "samurai" then
    return "fighter"
  elseif class == "mage"
  or class == "illusionist"
  or class == "necromancer" then
    return "wizard"
  elseif class == "cleric"
  or class == "monk"
  or class == "templar" then
    return "cleric"
  elseif class == "thief"
  or class == "assassin"
  or class == "ninja"
  or class == "bard" then
    return "rogue"
  elseif class == "gunslinger" then
    return "gunslinger"
  elseif class == "ranger" then
    return "ranger"
  end
end
lua/sharedtable.1443415932.txt.gz · Last modified: 2015/09/28 04:52 by vodur