<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Maratis forum - addFunction issue]]></title>
		<link>http://forum.maratis3d.com/viewtopic.php?id=417</link>
		<description><![CDATA[The most recent posts in addFunction issue.]]></description>
		<lastBuildDate>Tue, 28 Aug 2012 14:23:07 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2434#p2434</link>
			<description><![CDATA[<p>Hi,<br />I committed a fix, tested on mac with a custom plugin.</p>]]></description>
			<author><![CDATA[dummy@example.com (anael)]]></author>
			<pubDate>Tue, 28 Aug 2012 14:23:07 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2434#p2434</guid>
		</item>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2381#p2381</link>
			<description><![CDATA[<p>Ok, I will submit that in a bit. I&#039;m just getting a segfault and I&#039;m trying to figure out where it is. It&#039;s only when I call a function I specified... But only after it&#039;s already returned to lua... So yeah, no idea what&#039;s going on with that right now</p>]]></description>
			<author><![CDATA[dummy@example.com (Nistur)]]></author>
			<pubDate>Sun, 12 Aug 2012 17:28:22 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2381#p2381</guid>
		</item>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2380#p2380</link>
			<description><![CDATA[<p>I think I probably badly tried to optimize the runScript function by adding :</p><p>if(m_isRunning)<br />{<br />&nbsp; &nbsp; clear();<br />&nbsp; &nbsp; init();<br />}</p><p>it&#039;s a mistake as m_isRunning is false the first time it should just be :<br />clear();<br />init();</p><p>and accessory init() don&#039;t need to be called in MScript constructor,<br />just like you said.</p>]]></description>
			<author><![CDATA[dummy@example.com (anael)]]></author>
			<pubDate>Sun, 12 Aug 2012 17:00:40 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2380#p2380</guid>
		</item>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2378#p2378</link>
			<description><![CDATA[<p>I&#039;m trying to look at it this evening,<br />I used the system in a work production, so I guess it&#039;s more a bug that I added after or something that I moved who broke it.<br />I&#039;ll go back to you, sorry, I&#039;m still not home but in another part of France.</p>]]></description>
			<author><![CDATA[dummy@example.com (anael)]]></author>
			<pubDate>Sun, 12 Aug 2012 16:29:53 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2378#p2378</guid>
		</item>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2374#p2374</link>
			<description><![CDATA[<p>you still need to register MScript::function to respond to the calls from lua (otherwise the symbols are all nil). This is only ever done in MScript::init()</p><p>Either MScript::init() needs to be called the first time we run the script (rather than when MScript is initialised), or we manually link MScript::function to the function name in MScript::addFunction. As long as we always add to the map, either way should work, if MScript gets restarted, MScript::init() will then re-link any previously added functions. It&#039;s just really a case of whether there was a particular reason MScript::init() was being called on construction, or if it&#039;s ok to leave it until we run the script.</p>]]></description>
			<author><![CDATA[dummy@example.com (Nistur)]]></author>
			<pubDate>Sun, 12 Aug 2012 09:57:23 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2374#p2374</guid>
		</item>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2373#p2373</link>
			<description><![CDATA[<p>I didn&#039;t had time to look at it yet,<br />the thing is the custom function are in fact not added to the lua state,<br />so it don&#039;t need to be called before init normally, but maybe a bug is there.</p><p>If I remember (but I need to check to be sure), custom functions just feed a map used by a special lua function<br />that itself run the custom function from the map.</p>]]></description>
			<author><![CDATA[dummy@example.com (anael)]]></author>
			<pubDate>Sun, 12 Aug 2012 08:02:22 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2373#p2373</guid>
		</item>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2371#p2371</link>
			<description><![CDATA[<p>I have just confirmed, for my use case, this definitely fixes the problem. Is it ok to submit this?</p>]]></description>
			<author><![CDATA[dummy@example.com (Nistur)]]></author>
			<pubDate>Sun, 12 Aug 2012 06:52:46 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2371#p2371</guid>
		</item>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2370#p2370</link>
			<description><![CDATA[<p>It finally occurred to me what&#039;s happening.</p><p>MScript calls init() on construction, to add all the functions to the lua state. It doesn&#039;t call init again unless runScript is called when the script is already running. Therefore, adding functions manually, like I suggested, won&#039;t work because it&#039;s post-init but pre-run. m_isRunning will be false. The choice then, I think, is either to always call init() on runScript (and not on construction) or to blindly add the function to the lua_State, regardless of whether the script has been initialised, running or whatever. Personally I think the former is best, but I don&#039;t know if there was a reason this wasn&#039;t done before</p>]]></description>
			<author><![CDATA[dummy@example.com (Nistur)]]></author>
			<pubDate>Sun, 12 Aug 2012 06:28:09 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2370#p2370</guid>
		</item>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2368#p2368</link>
			<description><![CDATA[<p>Other people go out on a Friday night, I sit on a train, coding... or trying to...</p><p>Anyway, I&#039;ve investigated this further. I&#039;ve finally set up my netbook to easily switch between 3 builds of Maratis (stock distribution, svn dev build and MIngEd dev build) so I can now finally build Maratis and not have to manually copy files over...</p><p>Not important. What I have found interesting is that init() is called LONG before the game plugin is loaded... so the reason my functions aren&#039;t being found is because it&#039;s just being added to the map, but not to the lua_State.</p><p>my suggested fix is to change this:</p><div class="quotebox"><blockquote><p>void MScript::addFunction(const char* name, int (*function)(void)){<br />&nbsp; &nbsp; m_functions[name] = function;<br />}</p></blockquote></div><p>to:</p><div class="quotebox"><blockquote><p>void MScript::addFunction(const char* name, int (*function)(void)){<br />&nbsp; &nbsp; m_functions[name] = function;<br />&nbsp; &nbsp; if(m_IsRunning)<br />&nbsp; &nbsp; &nbsp; &nbsp; lua_register(m_state, name, ::function);<br />}</p></blockquote></div><p>I think that should probably fix it. Unfortunately, my train journey is almost over, so I don&#039;t think I&#039;ll have time...</p>]]></description>
			<author><![CDATA[dummy@example.com (Nistur)]]></author>
			<pubDate>Fri, 10 Aug 2012 20:05:26 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2368#p2368</guid>
		</item>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2367#p2367</link>
			<description><![CDATA[<p>Nope, that didn&#039;t work. It seems to be that at all points where I can realistically put addFunction, it&#039;s before the script is initialised</p><div class="quotebox"><blockquote><p>Registering script functions<br />Starting script<br />First script update (getBehavior: nil)</p></blockquote></div><p>The first line is from within the register function that I&#039;ve been trying to call from various places, the second is from within the script, outside any functions, the last is the first update of the script.</p><p>I have no idea why this might be happening, as the MScript setup seems to be that it should add the functions on init(). This is particularly difficult to debug, as I have to build both Maratis, and the game together... I&#039;ll see what I can find out</p>]]></description>
			<author><![CDATA[dummy@example.com (Nistur)]]></author>
			<pubDate>Fri, 10 Aug 2012 13:03:45 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2367#p2367</guid>
		</item>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2366#p2366</link>
			<description><![CDATA[<p>Behaviours get added automatically with static objects when the plugin is loaded currently, but I&#039;ll try adding it to startPlugin later, see if that makes any difference. Thanks</p>]]></description>
			<author><![CDATA[dummy@example.com (Nistur)]]></author>
			<pubDate>Fri, 10 Aug 2012 08:45:28 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2366#p2366</guid>
		</item>
		<item>
			<title><![CDATA[Re: addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2365#p2365</link>
			<description><![CDATA[<p>you should call addFunction in the start plugin call, at the same place you add behaviors.<br />It&#039;s maybe that.</p>]]></description>
			<author><![CDATA[dummy@example.com (anael)]]></author>
			<pubDate>Fri, 10 Aug 2012 08:35:55 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2365#p2365</guid>
		</item>
		<item>
			<title><![CDATA[addFunction issue]]></title>
			<link>http://forum.maratis3d.com/viewtopic.php?pid=2361#p2361</link>
			<description><![CDATA[<p>I have no idea whether this is a bug, or something silly I&#039;m doing.</p><p>I have a collection of functions I&#039;m trying to add to lua. As far as I know, I&#039;m registering them correctly:</p><div class="quotebox"><blockquote><p>MEngine* engine = MEngine::getInstance();<br />MScriptContext* script = engine-&gt;getScriptContext();<br />if(script)<br />&nbsp; &nbsp; script-&gt;addFunction(&quot;broadcastMessage&quot;, ScriptBroadcastMessage);</p></blockquote></div><div class="quotebox"><blockquote><p>int ScriptBroadcastMessage()<br />{<br />&nbsp; &nbsp; //...<br />}</p></blockquote></div><p>I&#039;ve tried registering the functions on MGame::MGame(), as a static object when the plugin is loaded, and on the first update of MGame. I&#039;ve checked the value of broadcastMessage from lua on the first update, and on 10th update, and it&#039;s always nil.</p><p>As I have said, this is probably my fault, but I can&#039;t see what I&#039;m doing wrong...</p>]]></description>
			<author><![CDATA[dummy@example.com (Nistur)]]></author>
			<pubDate>Thu, 09 Aug 2012 21:39:53 +0000</pubDate>
			<guid>http://forum.maratis3d.com/viewtopic.php?pid=2361#p2361</guid>
		</item>
	</channel>
</rss>
