User Tools

Site Tools


lua:sharedtable

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
lua:sharedtable [2015/09/28 12:52] vodurlua:sharedtable [2015/09/28 16:36] (current) vodur
Line 124: Line 124:
  
 shared.module1 = module1 shared.module1 = module1
 +</code>
 +
 +==== Sharing with different prog types ====
 +
 +All prog types have access to 'shared' table (mprog/oprog/aprog/rprog). However, if all your shared code is in mprog 1234 (as above example), only mprogs can easily load this code using [[:lua:ch:loadprog|loadprog]]. Loadprog called from oprog can only load oprogs, and similarly with aprog and rprog.
 +
 +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("helper 1 test test test")
 +end
 +
 +function shared.helper2()
 +    echo("helper 2 test test test")
 +end
 +
 +shared.helpers_loaded=true
 +</code>
 +
 +<code lua>
 +-- aprog 1235
 +loadprog(1234)
 +shared.helper1()
 +shared.helper2()
 +</code>
 +
 +<code lua>
 +-- mprog 1234
 +mob.proto.area:loadprog(1234)
 +shared.helper1()
 +shared.helper2()
 +</code>
 +
 +<code lua>
 +-- oprog 1234
 +obj.proto.area:loadprog(1234)
 +shared.helper1()
 +shared.helper2()
 +</code>
 +
 +<code lua>
 +-- rprog 1234
 +room.area:loadprog(1234)
 +shared.helper1()
 +shared.helper2()
 +</code>
 +
 +Using '**[[:lua:loadscriptfunction|loadscript]]**' is also an option:
 +
 +<code lua>
 +-- helpers_example.lua, shared helper functions for all prog types
 +if shared.helpers_loaded then return
 +
 +function shared.helper1()
 +    echo("helper 1 test test test")
 +end
 +
 +function shared.helper2()
 +    echo("helper 2 test test test")
 +end
 +
 +shared.helpers_loaded=true
 +</code>
 +
 +<code lua>
 +-- aprog 1235
 +loadscript('vodur','helpers_example')
 +shared.helper1()
 +shared.helper2()
 +</code>
 +
 +<code lua>
 +-- mprog 1234
 +loadscript('vodur','helpers_example')
 +shared.helper1()
 +shared.helper2()
 +</code>
 +
 +<code lua>
 +-- oprog 1234
 +loadscript('vodur','helpers_example')
 +shared.helper1()
 +shared.helper2()
 +</code>
 +
 +<code lua>
 +-- rprog 1234
 +loadscript('vodur','helpers_example')
 +shared.helper1()
 +shared.helper2()
 </code> </code>
  
lua/sharedtable.1443444772.txt.gz · Last modified: 2015/09/28 12:52 by vodur