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