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 12:53] – [Loading shared code] vodur | lua:sharedtable [2015/09/28 16:36] (current) – vodur | ||
|---|---|---|---|
| Line 126: | Line 126: | ||
| </ | </ | ||
| - | <lua> | + | ==== Sharing with different prog types ==== |
| - | < | + | All prog types have access to ' |
| - | for k,v in pairs(whatev) do say('hi') end | + | |
| + | 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: | ||
| + | |||
| + | < | ||
| + | -- 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.1443444834.txt.gz · Last modified: 2015/09/28 12:53 by vodur