Hmm yeah. So far it seems like making our own local variable system is the way to go...<div><br></div><div>It&#39;d be good  to test it out with a handful of scripts and see what it does... if it&#39;s a big improvement, then we could apply it to everything else. It&#39;d just take some grunt work. :)<br>
<br><div class="gmail_quote">On Tue, Jul 6, 2010 at 9:46 PM, 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;">
Okay, never mind all that.  It looks like Lua resolves global variable<br>
references in functions when the function is compiled, not when it&#39;s<br>
called, so you can&#39;t set up a nested thread environment and have the<br>
functions use that.  (Well, you can, but it requires even more hacks.)<br>
It worked in my original version only because I was always loading the<br>
script after creating the thread.<br>
<br>
Best laid plans, as they say...<br>
<div><div></div><div class="h5"><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>
&gt;Well, the way Lua threads work (as I understand it, anyway) is that each<br>
&gt;thread is sort of a &quot;lightweight&quot; lua_State, in the sense that when you<br>
&gt;create it, it shares the global table of the original lua_State you<br>
&gt;created it from.  But it&#39;s still a separate state block, so you can set<br>
&gt;a new environment table and the thread will then use that instead of the<br>
&gt;original.<br>
&gt;<br>
&gt;Since Lua also lets you link tables together using the &quot;index&quot;<br>
&gt;metamethod (see under <a href="http://www.lua.org/manual/5.1/manual.html#2.8" target="_blank">http://www.lua.org/manual/5.1/manual.html#2.8</a>),<br>
&gt;you can nest environments by setting the thread environment&#39;s index<br>
&gt;metamethod to the original global table -- that way the thread can read<br>
&gt;from both its own environment and the shared globals, but it can&#39;t write<br>
&gt;to the shared globals so it won&#39;t interfere with any other instances of<br>
&gt;the same script.<br>
&gt;<br>
&gt;It looked convenient to me, anyway. (:<br>
&gt;<br>
&gt;  --Andrew Church<br>
&gt;    <a href="mailto:achurch@achurch.org">achurch@achurch.org</a><br>
&gt;    <a href="http://achurch.org/" target="_blank">http://achurch.org/</a><br>
&gt;<br>
&gt;&gt;I suppose I don&#39;t understand the thread method very well, so that may be<br>
&gt;&gt;hampering my ability to see the advantages/disadvantages to either. :)<br>
&gt;&gt;<br>
&gt;&gt;On Tue, Jul 6, 2010 at 6:59 PM, Andrew Church<br>
&gt;&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>
&gt;&gt;&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt;&gt; Just that it feels really roundabout to me, especially with all the &quot;v.&quot;<br>
&gt;&gt;&gt; prefixes added to the variables.  As long as we have to set something,<br>
&gt;&gt;&gt; why not just set it once when the thread is created?  Then you don&#39;t have<br>
&gt;&gt;&gt; to worry about updating it later, plus you can use &quot;global&quot; variables in<br>
&gt;&gt;&gt; the scripts without worrying about forgetting a &quot;v.&quot; somewhere.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;  --Andrew Church<br>
&gt;&gt;&gt;    <a href="mailto:achurch@achurch.org">achurch@achurch.org</a><br>
&gt;&gt;&gt;    <a href="http://achurch.org/" target="_blank">http://achurch.org/</a><br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;What&#39;s wrong with setting a global variable before each script function is<br>
&gt;&gt;&gt; &gt;called?<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt;On Tue, Jul 6, 2010 at 6:41 PM, Andrew Church<br>
&gt;&gt;&gt; &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;&lt;<br>
&gt;&gt;&gt; <a href="mailto:achurch%252Baquaria@achurch.org">achurch%2Baquaria@achurch.org</a> &lt;<a href="mailto:achurch%25252Baquaria@achurch.org">achurch%252Baquaria@achurch.org</a>&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; wrote:<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt;&gt; Okay, so I&#39;ve run into a little problem with this.  I&#39;m not sure what<br>
&gt;&gt;&gt; &gt;&gt; approach your friend took, but what I&#39;ve done so far is put a<br>
&gt;&gt;&gt; &gt;&gt; &quot;v = getVars()&quot; at the top of each script, and put &quot;v.&quot; in front of all<br>
&gt;&gt;&gt; &gt;&gt; local variables.  The problem is that the functions in each script all<br>
&gt;&gt;&gt; &gt;&gt; see only a single version of &quot;v&quot;, because the getVars() is only executed<br>
&gt;&gt;&gt; &gt;&gt; once per script instance.<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; I guess one way around this would be to add a getVars() call to every<br>
&gt;&gt;&gt; &gt;&gt; function, but that feels a bit fragile to me -- too easy to miss one<br>
&gt;&gt;&gt; &gt;&gt; somewhere, and you can&#39;t even trace the problem easily because it&#39;ll<br>
&gt;&gt;&gt; &gt;&gt; fall back to the global &quot;v&quot; definition (which you need for initializing<br>
&gt;&gt;&gt; &gt;&gt; stuff at the top of the script).<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; What I did for now was to update &quot;v&quot; with the current instance&#39;s<br>
&gt;&gt;&gt; variable<br>
&gt;&gt;&gt; &gt;&gt; table right before each call into Lua, but that feels like a hack to me;<br>
&gt;&gt;&gt; &gt;&gt; if we have to mess with the Lua environment from C, we might as well go<br>
&gt;&gt;&gt; &gt;&gt; back to the nested environments I was using before, because at least<br>
&gt;&gt;&gt; with<br>
&gt;&gt;&gt; &gt;&gt; those you don&#39;t have to to a lua_setglobal() on every Lua function call.<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; I don&#39;t know Lua too well, so maybe I&#39;m missing something obvious --<br>
&gt;&gt;&gt; &gt;&gt; any other suggestions?<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;  --Andrew Church<br>
&gt;&gt;&gt; &gt;&gt;    <a href="mailto:achurch@achurch.org">achurch@achurch.org</a><br>
&gt;&gt;&gt; &gt;&gt;    <a href="http://achurch.org/" target="_blank">http://achurch.org/</a><br>
</div></div><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>