lua:sharedtable
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
lua:sharedtable [2015/09/28 04:33] – vodur | lua:sharedtable [2015/09/28 16:36] (current) – vodur | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== ' | ====== ' | ||
- | Any prog/script runs inside a script environment. See [[: | + | Any prog/script runs inside a script environment. See [[: |
- | The ' | + | The ' |
Using the ' | Using the ' | ||
- | <lua> | + | <code lua> |
+ | shared.message=" | ||
+ | </code> | ||
- | < | + | One can clear/reset the shared table by simply setting its value as an empty table: |
- | shared.message="Hello world!" | + | <code lua> |
+ | 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: | ||
+ | |||
+ | ===== Shared functions ===== | ||
+ | |||
+ | Just like any other lua table, ' | ||
+ | |||
+ | <code lua> | ||
+ | -- FUNCTION FOR DETERMINING GUILD AFFILIATION | ||
+ | -- send ch.class | ||
+ | function shared.find_guild(class) | ||
+ | if class == "warrior" | ||
+ | or class == " | ||
+ | or class == " | ||
+ | or class == " | ||
+ | return " | ||
+ | elseif class == " | ||
+ | or class == " | ||
+ | or class == " | ||
+ | return " | ||
+ | elseif class == " | ||
+ | or class == " | ||
+ | or class == " | ||
+ | return " | ||
+ | elseif class == " | ||
+ | or class == " | ||
+ | or class == " | ||
+ | or class == " | ||
+ | return " | ||
+ | elseif class == " | ||
+ | return " | ||
+ | elseif class == " | ||
+ | return " | ||
+ | end | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | ===== Tables in ' | ||
+ | |||
+ | We can also put tables inside ' | ||
+ | |||
+ | <code lua> | ||
+ | shared.somevalues = { | ||
+ | bluecount=3, | ||
+ | yellowcount=5, | ||
+ | orangecount=1 | ||
+ | } | ||
+ | |||
+ | shared.module1={} | ||
+ | local good_vnums={1234, | ||
+ | function shared.module1.check_vnum(vnum) | ||
+ | for k,v in pairs(good_vnums) do | ||
+ | if v==vnum then return true | ||
+ | end | ||
+ | return false | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | ===== Loading shared code ===== | ||
+ | |||
+ | When using shared table to store shared code such as helper functions, a typical way to do this is to put all the functions in a single prog and call this prog from any prog that uses the helper functions. Additionally, | ||
+ | |||
+ | Example using a loaded flag: | ||
+ | |||
+ | <code lua> | ||
+ | -- mprog 1234 | ||
+ | if shared.helpers_loaded then return | ||
+ | |||
+ | function shared.helper1() | ||
+ | echo(" | ||
+ | end | ||
+ | |||
+ | function shared.helper2() | ||
+ | echo(" | ||
+ | end | ||
+ | |||
+ | shared.helpers_loaded=true | ||
+ | </ | ||
+ | |||
+ | <code lua> | ||
+ | -- mprog 1235 | ||
+ | loadprog(1234) | ||
+ | shared.helper1() | ||
+ | </ | ||
+ | |||
+ | <code lua> | ||
+ | -- mprog 1236 | ||
+ | loadprog(1234) | ||
+ | shared.helper2() | ||
+ | </ | ||
+ | |||
+ | Similar example using a module style: | ||
+ | |||
+ | <code lua> | ||
+ | -- mprog 1234 | ||
+ | if shared.module1 then return | ||
+ | |||
+ | local module1={} | ||
+ | |||
+ | module1.helper1 = function() | ||
+ | echo(" | ||
+ | end | ||
+ | |||
+ | module1.helper2 = function() | ||
+ | echo(" | ||
+ | end | ||
+ | |||
+ | shared.module1 = module1 | ||
+ | </ | ||
+ | |||
+ | ==== Sharing with different prog types ==== | ||
+ | |||
+ | All prog types have access to ' | ||
+ | |||
+ | One strategy to overcome this limitation is to keep all the shared code in an aprog and load it from the different prog types accordingly: | ||
+ | |||
+ | <code lua> | ||
+ | -- aprog 1234, shared helper functions for all prog types | ||
+ | if shared.helpers_loaded then return | ||
+ | |||
+ | function shared.helper1() | ||
+ | echo(" | ||
+ | end | ||
+ | |||
+ | function shared.helper2() | ||
+ | echo(" | ||
+ | end | ||
+ | |||
+ | shared.helpers_loaded=true | ||
+ | </ | ||
+ | |||
+ | <code lua> | ||
+ | -- aprog 1235 | ||
+ | loadprog(1234) | ||
+ | shared.helper1() | ||
+ | shared.helper2() | ||
+ | </ | ||
+ | |||
+ | <code lua> | ||
+ | -- mprog 1234 | ||
+ | mob.proto.area: | ||
+ | shared.helper1() | ||
+ | shared.helper2() | ||
+ | </ | ||
+ | |||
+ | <code lua> | ||
+ | -- oprog 1234 | ||
+ | obj.proto.area: | ||
+ | shared.helper1() | ||
+ | shared.helper2() | ||
+ | </ | ||
+ | |||
+ | <code lua> | ||
+ | -- rprog 1234 | ||
+ | room.area: | ||
+ | shared.helper1() | ||
+ | shared.helper2() | ||
+ | </ | ||
+ | |||
+ | Using ' | ||
+ | |||
+ | <code lua> | ||
+ | -- helpers_example.lua, | ||
+ | if shared.helpers_loaded then return | ||
+ | |||
+ | function shared.helper1() | ||
+ | echo(" | ||
+ | end | ||
+ | |||
+ | function shared.helper2() | ||
+ | echo(" | ||
+ | end | ||
+ | |||
+ | shared.helpers_loaded=true | ||
+ | </ | ||
+ | |||
+ | <code lua> | ||
+ | -- aprog 1235 | ||
+ | loadscript(' | ||
+ | shared.helper1() | ||
+ | shared.helper2() | ||
+ | </ | ||
+ | |||
+ | <code lua> | ||
+ | -- mprog 1234 | ||
+ | loadscript(' | ||
+ | shared.helper1() | ||
+ | shared.helper2() | ||
+ | </ | ||
+ | |||
+ | <code lua> | ||
+ | -- oprog 1234 | ||
+ | loadscript(' | ||
+ | shared.helper1() | ||
+ | shared.helper2() | ||
+ | </ | ||
+ | |||
+ | <code lua> | ||
+ | -- rprog 1234 | ||
+ | loadscript(' | ||
+ | shared.helper1() | ||
+ | shared.helper2() | ||
</ | </ | ||
lua/sharedtable.1443414823.txt.gz · Last modified: 2015/09/28 04:33 by vodur