I don&#39;t know that &quot;local&quot; works that way, see: <a href="http://www.lua.org/pil/4.2.html">http://www.lua.org/pil/4.2.html</a><br><br><div>I find that page of documentation kind of confusing. :P</div><div><br><div>
<div class="gmail_quote">On Tue, Jun 29, 2010 at 10:41 AM, Andrew Church <span dir="ltr">&lt;<a href="mailto:achurch%2Baquaria@achurch.org">achurch+aquaria@achurch.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
That sounds pretty similar to my approach, only using an explicit table<br>
instead of creating a new environment table from the C++ side.  But if<br>
we&#39;re going to update the scripts anyway, wouldn&#39;t it be simpler to just<br>
use Lua&#39;s built-in local variable functionality?  E.g., instead of<br>
<br>
n = 0<br>
function init(me)<br>
        n = getNaija()<br>
end<br>
<br>
you make the first line &quot;local n = 0&quot;, and if my understanding of Lua&#39;s<br>
scoping rules is correct, &quot;n&quot; is then local to that specific script.  I<br>
think I read that local variables are also faster than globals, though<br>
I can&#39;t recall the reference at the moment.<br>
<br>
The one potential issue I&#39;ve seen so far with this is that Lua doesn&#39;t<br>
seem to have any facility for loading include files in the current scope,<br>
like &quot;require&quot; in Perl.  I haven&#39;t looked at all the Lua include files to<br>
see whether it&#39;s actually a problem, but if any of them reference global<br>
variables, those would have to be adjusted to take function parameters<br>
instead.<br>
<div class="im"><br>
  --Andrew Church<br>
    <a href="mailto:achurch@achurch.org">achurch@achurch.org</a><br>
    <a href="http://achurch.org/" target="_blank">http://achurch.org/</a><br>
<br>
</div><div class="im">&gt;So, one way to change the scripts to use local variables... Based on what a<br>
&gt;friend told me, and the engine he&#39;s working on...<br>
&gt;<br>
&gt;Instead of creating a lua_State for each script, we create a table. Then any<br>
&gt;script can access it&#39;s table... something like<br>
&gt;<br>
&gt;v = getVars();<br>
&gt;<br>
&gt;then any local variable just has to have &quot;v.&quot; added before it.<br>
&gt;<br>
&gt;This doesn&#39;t sound like a super hard change to make, although it would<br>
&gt;involve changing all the scripts.<br>
&gt;<br>
&gt;On Thu, Jun 17, 2010 at 11:05 AM, Andrew Church<br>
</div>&gt;&lt;<a href="mailto:achurch%2Baquaria@achurch.org">achurch+aquaria@achurch.org</a>&lt;<a href="mailto:achurch%252Baquaria@achurch.org">achurch%2Baquaria@achurch.org</a>&gt;<br>
<div><div></div><div class="h5">&gt;&gt; wrote:<br>
&gt;<br>
&gt;&gt; I&#39;ve put together a patch that allows the game&#39;s Lua scripts to run as<br>
&gt;&gt; threads within a single Lua state.  The patch is admittedly a bit<br>
&gt;&gt; hackish, in that it creates a nested environment for each thread so the<br>
&gt;&gt; global variables defined in each script don&#39;t interfere with those used<br>
&gt;&gt; by other scripts, but IMHO the memory savings -- typically around 80% of<br>
&gt;&gt; Lua&#39;s current memory usage, for example ~16MB in Home Waters -- make it<br>
&gt;&gt; worth the change.  The nested environment hack could of course be<br>
&gt;&gt; removed in the future if the scripts are updated to use local variables.<br>
&gt;&gt; I haven&#39;t tested the patch extensively, but in theory it should be safe,<br>
&gt;&gt; as the only effective change is merging the common function set into a<br>
&gt;&gt; single read-only parent environment.<br>
&gt;&gt;<br>
&gt;&gt; The patch is attached below, as a compressed file due to its length.<br>
&gt;&gt; (Ryan, I hope that&#39;s not a problem for either you or the mailing list<br>
&gt;&gt; software!)  The actual changes aren&#39;t too major; the bulk of the patch<br>
&gt;&gt; is due to s/*L/baseState/ in ScriptInterface::createBaseLuaVM()<br>
&gt;&gt; (formerly initLuaVM(), which is now used only for threads).<br>
&gt;&gt;<br>
&gt;&gt; The patch is also in my repository (<a href="http://achurch.org/cgi-bin/hg/aquaria/" target="_blank">http://achurch.org/cgi-bin/hg/aquaria/</a><br>
&gt;&gt; )<br>
&gt;&gt; on branch &quot;single-lua-state&quot;.  It&#39;s actually committed as two patches<br>
&gt;&gt; there, since part of the change is identical to one of my PSP patches,<br>
&gt;&gt; but I&#39;ve merged them here for (what I hope is) convenience.<br>
&gt;&gt;<br>
&gt;&gt;  --Andrew Church<br>
&gt;&gt;    <a href="mailto:achurch@achurch.org">achurch@achurch.org</a><br>
&gt;&gt;    <a href="http://achurch.org/" target="_blank">http://achurch.org/</a><br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; aquaria mailing list<br>
&gt;&gt; <a href="mailto:aquaria@icculus.org">aquaria@icculus.org</a><br>
&gt;&gt; <a href="http://icculus.org/mailman/listinfo/aquaria" target="_blank">http://icculus.org/mailman/listinfo/aquaria</a><br>
&gt;&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;--<br>
&gt;Alec Holowka<br>
&gt;<a href="http://www.infiniteammo.ca" target="_blank">www.infiniteammo.ca</a><br>
&gt;<a href="http://www.bit-blot.com" target="_blank">www.bit-blot.com</a><br>
&gt;<br>
</div></div>&gt;--00c09f99e54989e40c048a24bf46<br>
&gt;Content-Type: text/html; charset=ISO-8859-1<br>
&gt;Content-Transfer-Encoding: quoted-printable<br>
<div class="im">&gt;<br>
&gt;So, one way to change the scripts to use local variables... Based on what a=<br>
</div>&gt; friend told me, and the engine he&amp;#39;s working on...&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;I=<br>
<div class="im">&gt;nstead of creating a lua_State for each script, we create a table. Then any=<br>
</div>&gt; script can access it&amp;#39;s table... something like&lt;/div&gt;<br>
&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;v =3D getVars();&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;then any loca=<br>
&gt;l variable just has to have &amp;quot;v.&amp;quot; added before it.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;=<br>
&gt;/div&gt;&lt;div&gt;This doesn&amp;#39;t sound like a super hard change to make, although=<br>
&gt; it would involve changing all the scripts.&lt;br&gt;<br>
&gt;&lt;br&gt;&lt;div class=3D&quot;gmail_quote&quot;&gt;On Thu, Jun 17, 2010 at 11:05 AM, Andrew Chu=<br>
&gt;rch &lt;span dir=3D&quot;ltr&quot;&gt;&amp;lt;&lt;a href=3D&quot;mailto:<a href="mailto:achurch%252Baquaria@achurch.org">achurch%2Baquaria@achurch.org</a>&quot;&gt;=<br>
&gt;<a href="mailto:achurch%2Baquaria@achurch.org">achurch+aquaria@achurch.org</a>&lt;/a&gt;&amp;gt;&lt;/span&gt; wrote:&lt;br&gt;&lt;blockquote class=3D&quot;g=<br>
&gt;mail_quote&quot; style=3D&quot;margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-l=<br>
&gt;eft:1ex;&quot;&gt;<br>
&gt;I&amp;#39;ve put together a patch that allows the game&amp;#39;s Lua scripts to run=<br>
&gt; as&lt;br&gt;<br>
&gt;threads within a single Lua state. =A0The patch is admittedly a bit&lt;br&gt;<br>
&gt;hackish, in that it creates a nested environment for each thread so the&lt;br&gt;<br>
&gt;global variables defined in each script don&amp;#39;t interfere with those used=<br>
&gt;&lt;br&gt;<br>
&gt;by other scripts, but IMHO the memory savings -- typically around 80% of&lt;br=<br>
&gt;&gt;<br>
&gt;Lua&amp;#39;s current memory usage, for example ~16MB in Home Waters -- make it=<br>
&gt;&lt;br&gt;<br>
&gt;worth the change. =A0The nested environment hack could of course be&lt;br&gt;<br>
&gt;removed in the future if the scripts are updated to use local variables.&lt;br=<br>
&gt;&gt;<br>
&gt;I haven&amp;#39;t tested the patch extensively, but in theory it should be safe=<br>
&gt;,&lt;br&gt;<br>
&gt;as the only effective change is merging the common function set into a&lt;br&gt;<br>
&gt;single read-only parent environment.&lt;br&gt;<br>
&gt;&lt;br&gt;<br>
&gt;The patch is attached below, as a compressed file due to its length.&lt;br&gt;<br>
&gt;(Ryan, I hope that&amp;#39;s not a problem for either you or the mailing list&lt;b=<br>
&gt;r&gt;<br>
&gt;software!) =A0The actual changes aren&amp;#39;t too major; the bulk of the patc=<br>
&gt;h&lt;br&gt;<br>
&gt;is due to s/*L/baseState/ in ScriptInterface::createBaseLuaVM()&lt;br&gt;<br>
&gt;(formerly initLuaVM(), which is now used only for threads).&lt;br&gt;<br>
&gt;&lt;br&gt;<br>
&gt;The patch is also in my repository (&lt;a href=3D&quot;<a href="http://achurch.org/cgi-bin/h=" target="_blank">http://achurch.org/cgi-bin/h=</a><br>
&gt;g/aquaria/&quot; target=3D&quot;_blank&quot;&gt;<a href="http://achurch.org/cgi-bin/hg/aquaria/" target="_blank">http://achurch.org/cgi-bin/hg/aquaria/</a>&lt;/a&gt;)&lt;b=<br>
&gt;r&gt;<br>
&gt;on branch &amp;quot;single-lua-state&amp;quot;. =A0It&amp;#39;s actually committed as t=<br>
&gt;wo patches&lt;br&gt;<br>
&gt;there, since part of the change is identical to one of my PSP patches,&lt;br&gt;<br>
&gt;but I&amp;#39;ve merged them here for (what I hope is) convenience.&lt;br&gt;<br>
&gt;&lt;br&gt;<br>
&gt; =A0--Andrew Church&lt;br&gt;<br>
&gt; =A0 =A0&lt;a href=3D&quot;mailto:<a href="mailto:achurch@achurch.org">achurch@achurch.org</a>&quot;&gt;<a href="mailto:achurch@achurch.org">achurch@achurch.org</a>&lt;/a&gt;&lt;br&gt;<br>
&gt; =A0 =A0&lt;a href=3D&quot;<a href="http://achurch.org/" target="_blank">http://achurch.org/</a>&quot; target=3D&quot;_blank&quot;&gt;<a href="http://achurch.or" target="_blank">http://achurch.or</a>=<br>
&gt;g/&lt;/a&gt;&lt;br&gt;<br>
&gt;&lt;br&gt;<br>
&gt;_______________________________________________&lt;br&gt;<br>
&gt;aquaria mailing list&lt;br&gt;<br>
&gt;&lt;a href=3D&quot;mailto:<a href="mailto:aquaria@icculus.org">aquaria@icculus.org</a>&quot;&gt;<a href="mailto:aquaria@icculus.org">aquaria@icculus.org</a>&lt;/a&gt;&lt;br&gt;<br>
&gt;&lt;a href=3D&quot;<a href="http://icculus.org/mailman/listinfo/aquaria" target="_blank">http://icculus.org/mailman/listinfo/aquaria</a>&quot; target=3D&quot;_blank&quot;&gt;h=<br>
&gt;ttp://<a href="http://icculus.org/mailman/listinfo/aquaria" target="_blank">icculus.org/mailman/listinfo/aquaria</a>&lt;/a&gt;&lt;br&gt;<br>
&gt;&lt;/blockquote&gt;&lt;/div&gt;&lt;br&gt;&lt;br clear=3D&quot;all&quot;&gt;&lt;br&gt;-- &lt;br&gt;Alec Holowka&lt;br&gt;&lt;a href=<br>
&gt;=3D&quot;<a href="http://www.infiniteammo.ca" target="_blank">http://www.infiniteammo.ca</a>&quot;&gt;<a href="http://www.infiniteammo.ca" target="_blank">www.infiniteammo.ca</a>&lt;/a&gt;&lt;br&gt;&lt;a href=3D&quot;http:=<br>

&gt;//<a href="http://www.bit-blot.com" target="_blank">www.bit-blot.com</a>&quot;&gt;<a href="http://www.bit-blot.com" target="_blank">www.bit-blot.com</a>&lt;/a&gt;&lt;br&gt;<br>
&gt;&lt;/div&gt;<br>
&gt;<br>
&gt;--00c09f99e54989e40c048a24bf46--<br>
&gt;<br>
&gt;--===============4162574011203577186==<br>
&gt;Content-Type: text/plain; charset=&quot;us-ascii&quot;<br>
&gt;MIME-Version: 1.0<br>
&gt;Content-Transfer-Encoding: 7bit<br>
&gt;Content-Disposition: inline<br>
<div class="im">&gt;<br>
&gt;_______________________________________________<br>
&gt;aquaria mailing list<br>
&gt;<a href="mailto:aquaria@icculus.org">aquaria@icculus.org</a><br>
&gt;<a href="http://icculus.org/mailman/listinfo/aquaria" target="_blank">http://icculus.org/mailman/listinfo/aquaria</a><br>
&gt;<br>
</div>&gt;--===============4162574011203577186==--<br>
<div><div></div><div class="h5">_______________________________________________<br>
aquaria mailing list<br>
<a href="mailto:aquaria@icculus.org">aquaria@icculus.org</a><br>
<a href="http://icculus.org/mailman/listinfo/aquaria" target="_blank">http://icculus.org/mailman/listinfo/aquaria</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>Alec Holowka<br><a href="http://www.infiniteammo.ca">www.infiniteammo.ca</a><br><a href="http://www.bit-blot.com">www.bit-blot.com</a><br>
</div></div>