<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ShiVa 3D</title>
	<atom:link href="http://www.stonetrip.com/developer/feed" rel="self" type="application/rss+xml" />
	<link>http://www.stonetrip.com/developer</link>
	<description>ShiVa developers will find here all the materials needed to create amazing games and web3D applications...</description>
	<lastBuildDate>Wed, 21 Sep 2011 10:08:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introduction To The LUA Metatables: Default values in the declaration</title>
		<link>http://www.stonetrip.com/developer/1754-introduction-to-the-lua-metatables-default-values-in-the-declaration</link>
		<comments>http://www.stonetrip.com/developer/1754-introduction-to-the-lua-metatables-default-values-in-the-declaration#comments</comments>
		<pubDate>Wed, 21 Sep 2011 09:35:10 +0000</pubDate>
		<dc:creator>ST Team</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.stonetrip.com/developer/?p=1754</guid>
		<description><![CDATA[This tutorial is a small extension to the previous tutorial about Lua metatables. It show you another way to declare a metatable, how to add default values in your metatable.]]></description>
			<content:encoded><![CDATA[<p>The previous tutorial was about how to build an URL with post parameters using a metatable. It can be read <a href="http://www.stonetrip.com/developer/1749-introduction-to-the-lua-metatables-the-url-example">here</a>.</p>
<p><b>Warning:</b> This tutorial is only viable for games using the Lua script, it doesn’t work for games exported in native code, because it uses LUA elements</p>
<h2>Introduction</h2>
<p>You can add default values in a metatable when declaring it.<br />
The syntax is this one:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">local</span> myMetatable <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #888800;">&quot;value0&quot;</span>, <span style="color: #cc66cc;">32</span>, hObject<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>All the elements between the { } are the value, and the key is set automatically using an index, starting at 0.<br />
Here, myMetatable[0] is &#8220;value0&#8243;, myMetatable[1] is 32 and myMetatable[2] is hObject</p>
<p>It&#8217;s exactly the same result than the code was:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">local</span> myMetatable <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
myMetatable<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #888800;">&quot;value0&quot;</span>
myMetatable<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">32</span>
myMetatable<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> hObject</pre></div></div>

<h2>Sample</h2>
<p>Below, you can find a small function using this kind of declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">function</span> MyGame.getMonthName <span style="color: #66cc66;">&#40;</span> nMonth <span style="color: #66cc66;">&#41;</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
&nbsp;
    <span style="color: #0000ff;">local</span> month_of_year <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #888800;">&quot;January&quot;</span>, <span style="color: #888800;">&quot;February&quot;</span>, <span style="color: #888800;">&quot;March&quot;</span>, <span style="color: #888800;">&quot;April&quot;</span>, <span style="color: #888800;">&quot;May&quot;</span>, <span style="color: #888800;">&quot;June&quot;</span>, <span style="color: #888800;">&quot;July&quot;</span>, <span style="color: #888800;">&quot;August&quot;</span>, <span style="color: #888800;">&quot;September&quot;</span>, <span style="color: #888800;">&quot;October&quot;</span>, <span style="color: #888800;">&quot;November&quot;</span>, <span style="color: #888800;">&quot;December&quot;</span><span style="color: #66cc66;">&#125;</span>
    <span style="color: #0000ff;">return</span> month_of_year<span style="color: #66cc66;">&#91;</span>nMonth<span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">end</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span></pre></div></div>

<p>Call it with a month index, and you&#8217;ll get the month name.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stonetrip.com/developer/1754-introduction-to-the-lua-metatables-default-values-in-the-declaration/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Introduction to the LUA metatables: The URL example.</title>
		<link>http://www.stonetrip.com/developer/1749-introduction-to-the-lua-metatables-the-url-example</link>
		<comments>http://www.stonetrip.com/developer/1749-introduction-to-the-lua-metatables-the-url-example#comments</comments>
		<pubDate>Wed, 21 Sep 2011 09:19:12 +0000</pubDate>
		<dc:creator>ST Team</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.stonetrip.com/developer/?p=1749</guid>
		<description><![CDATA[This tutorial teach you an easy way to manage your URL parameters in your application by using LUA metatables.]]></description>
			<content:encoded><![CDATA[<p><b>Warning:</b> This tutorial is only viable for games using the Lua script, it doesn&#8217;t work for games exported in native code, because it uses LUA elements</p>
<h2>Introduction</h2>
<p>Metatables in LUA are a kind of hashtable. You can associate a key and a value. Here, the key can be anything (number, string, object&#8230;)<br />
The URL post parameters is a list of &#8220;parameter_key=parameter_value&#8221;, separated by &#8220;&#038;&#8221;.<br />
We will use the parameter_keys as key of a metatable, and parameter_values as value.</p>
<h2>Steps</h2>
<p>1. In your game, where you want to create an url, declare the metatable like that:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">local</span> params <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>2. Now, add all the parameters you want for your URL by adding each key and value of the parameter in the metatable, using this syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">params<span style="color: #66cc66;">&#91;</span><span style="color: #888800;">&quot;page&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">=</span><span style="color: #888800;">&quot;&quot;</span>..<span style="color: #0000ff;">this</span>.nCurrentPage <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>
params<span style="color: #66cc66;">&#91;</span><span style="color: #888800;">&quot;category&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">=</span><span style="color: #0000ff;">this</span>.sCurrentCategory <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>3. Create a function buildPost ( params ), taking in argument your metatable containing the URL parameters.<br />
4. Start by declaring the result variable:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">local</span> sRes <span style="color: #66cc66;">=</span> <span style="color: #888800;">&quot;&quot;</span></pre></div></div>

<p>5. We will now create a loop for the metatable. For this kind of table, you mustn&#8217;t use the &#8220;for i&#8221; loop, but the &#8220;for each&#8221; one.<br />
The syntax of this loop is:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">for</span> vKey <span style="color: #0000ff;">in</span> params
<span style="color: #0000ff;">do</span>
    <span style="color: #0000ff;">local</span> vValue <span style="color: #66cc66;">=</span> params<span style="color: #66cc66;">&#91;</span>vKey<span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #0000ff;">end</span></pre></div></div>

<p>6. Concat the key and the value, by adding a &#8220;=&#8221; between them. Do not forget to encode the value. Then add the param to the sRes variable.</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">local</span> sParam <span style="color: #66cc66;">=</span> vKey..<span style="color: #888800;">&quot;=&quot;</span>..<span style="color: #0000ff;">string</span>.encoreURL <span style="color: #66cc66;">&#40;</span> vValue <span style="color: #66cc66;">&#41;</span>
sRes <span style="color: #66cc66;">=</span> sRes..sParam..<span style="color: #888800;">&quot;&amp;&quot;</span></pre></div></div>

<p>7. At the bottom of the function, do not forget to remove the last &#8220;&#038;&#8221;, and return the final string:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">sRes <span style="color: #66cc66;">=</span> <span style="color: #0000ff;">string</span>.getSubString <span style="color: #66cc66;">&#40;</span> sRes, <span style="color: #cc66cc;">0</span>, <span style="color: #0000ff;">string</span>.getLength <span style="color: #66cc66;">&#40;</span> sRes <span style="color: #66cc66;">&#41;</span> - <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">return</span> sRes</pre></div></div>

<p>The whole function should now looks like that:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">function</span> Facebook.buildPost <span style="color: #66cc66;">&#40;</span> params <span style="color: #66cc66;">&#41;</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
&nbsp;
    <span style="color: #0000ff;">local</span> sRes <span style="color: #66cc66;">=</span> <span style="color: #888800;">&quot;&quot;</span>
    <span style="color: #0000ff;">for</span> vKey <span style="color: #0000ff;">in</span> params
    <span style="color: #0000ff;">do</span>
        <span style="color: #0000ff;">local</span> vValue <span style="color: #66cc66;">=</span> params<span style="color: #66cc66;">&#91;</span>vKey<span style="color: #66cc66;">&#93;</span>
        <span style="color: #0000ff;">local</span> sParam <span style="color: #66cc66;">=</span> vKey..<span style="color: #888800;">&quot;=&quot;</span>..<span style="color: #0000ff;">string</span>.encoreURL <span style="color: #66cc66;">&#40;</span> vValue <span style="color: #66cc66;">&#41;</span>
&nbsp;
        sRes <span style="color: #66cc66;">=</span> sRes..sParam..<span style="color: #888800;">&quot;&amp;&quot;</span>
    <span style="color: #0000ff;">end</span>
&nbsp;
    sRes <span style="color: #66cc66;">=</span> <span style="color: #0000ff;">string</span>.getSubString <span style="color: #66cc66;">&#40;</span> sRes, <span style="color: #cc66cc;">0</span>, <span style="color: #0000ff;">string</span>.getLength <span style="color: #66cc66;">&#40;</span> sRes <span style="color: #66cc66;">&#41;</span> - <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">return</span> sRes
&nbsp;
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">end</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span></pre></div></div>

<p>8. Get back to the place where you have declared the metatable, and add this line:<br />
    local sUrl = this.sTheServerUrl ( )..&#8221;/search?&#8221;..this.buildPost ( params )</p>
<p>You can now use the url</p>
<h2>Conclusion</h2>
<p>By using this way, you can now easily build URLs in your game, just by creating a metatable, adding the post parameters in it and calling your buildPost function.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stonetrip.com/developer/1749-introduction-to-the-lua-metatables-the-url-example/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fake the Editor to make it believe it is a device</title>
		<link>http://www.stonetrip.com/developer/1735-fake-the-editor-to-make-it-believe-it-is-a-device</link>
		<comments>http://www.stonetrip.com/developer/1735-fake-the-editor-to-make-it-believe-it-is-a-device#comments</comments>
		<pubDate>Tue, 13 Sep 2011 16:23:21 +0000</pubDate>
		<dc:creator>ST Team</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.stonetrip.com/developer/?p=1735</guid>
		<description><![CDATA[Sometimes, it is boring to test your script if your game targets several devices, because you have some "if ( android ) then elseif ( iphone )..." in your code for instance.
There is an easy trick to make your script thinking that it is running on a specific device.]]></description>
			<content:encoded><![CDATA[<p>Warning: This tutorial is only viable for games using the Lua script, it doesn&#8217;t work for games exported in native code, because it plays with Lua pointers.</p>
<h2>Steps</h2>
<p>The idea is to make some StoneScript functions pointing to custom Lua functions.</p>
<p>1. Create an AIModel &#8220;DeviceSimulator&#8221;</p>
<p>2. Create a function named getOSType, in which you return the constant of the OS type you want to simulate:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">return</span> system.kOSTypeAndroid</pre></div></div>

<p>3. Create a function named getClientType that return the constant of the client type you want to simulate:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">return</span> system.kClientTypeEmbedded</pre></div></div>

<p>4. Create a onInit handler in the AI, and override the lua pointers for both system.getOSType and system.getClientType functions by your own ones:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> system.getClientType <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> system.kClientTypeEditor <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">then</span>  
    system.getOSType <span style="color: #66cc66;">=</span> <span style="color: #0000ff;">this</span>.getOSType
    system.getClientType <span style="color: #66cc66;">=</span> <span style="color: #0000ff;">this</span>.getClientType
<span style="color: #0000ff;">end</span></pre></div></div>

<p>5. Compile your AIModel. You will get this error:<br />
    &#8220;malformed expression, expected &#8216;(&#8216;&#8221;<br />
You can ignore this error, the script will work.</p>
<p>6. Place the AIModel in your User Main AIs of your game, and be sure to place it on the very top (for the onInit of the AIModel to be executed before anything else)</p>
<h2>Conclusion</h2>
<p>Now, anywhere in your code, when you will call system.getOSType ( ) or system.getClientType ( ), it will call your own functions, returning the constant of your choice.<br />
For instance, in that case, when calling system.getOSType ( ), you will get system.kOSTypeAndroid, even if you are in ShiVa Editor.</p>
<p>This is a very useful usage of the Lua Pointers.<br />
Many powerful things can be done by going deeper than only using the &#8220;basic&#8221; Lua scripting.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stonetrip.com/developer/1735-fake-the-editor-to-make-it-believe-it-is-a-device/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Buttons normal/pressed textures managment</title>
		<link>http://www.stonetrip.com/developer/1727-buttons-normalpressed-textures-managment</link>
		<comments>http://www.stonetrip.com/developer/1727-buttons-normalpressed-textures-managment#comments</comments>
		<pubDate>Wed, 07 Sep 2011 14:56:29 +0000</pubDate>
		<dc:creator>ST Team</dc:creator>
				<category><![CDATA[HUD]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://www.stonetrip.com/developer/?p=1727</guid>
		<description><![CDATA[Having buttons in your HUD is fine for your application to work, but it's better when the user can have a feedback of his actions. That's the goal of this tutorial, changing the texture of a button when the user clic it with his mouse, or tap it with his finger.]]></description>
			<content:encoded><![CDATA[<h2>Steps</h2>
<p>1.	Create an AIModel named « ButtonsManager », and add it to your game User Main AIs.</p>
<p>2.	Create 2 hashtables to store the textures of your buttons, one for the state « normal » and one for the state « pressed ».</p>
<p>3.	Create a string variable named « sUnderMouseButton » to store the button that has been clicked.</p>
<p>4.	Create a function to simplify the setup of the buttons. The function take in parameter the tag of the button, and its textures for both states. Then, the function adds the textures in our 2 hashtables, using the component tag as key. The function should looks like that :</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">function</span> ButtonsManager.addButton <span style="color: #66cc66;">&#40;</span> sComponent, sTextureNormal, sTexturePressed <span style="color: #66cc66;">&#41;</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> sTextureNormal <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">then</span>
        hashtable.add <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.htButtonsNormal <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>, sComponent, sTextureNormal <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">end</span>
    <span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> sTexturePressed <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">then</span>
        hashtable.add <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.htButtonsPressed <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>, sComponent, sTexturePressed <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">end</span>
&nbsp;
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">end</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span></pre></div></div>

<p>5.	Create the onInit handler, and add the button you want to interact with, by using the function you have created above :</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">function</span> ButtonsManager.onInit <span style="color: #66cc66;">&#40;</span>  <span style="color: #66cc66;">&#41;</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
&nbsp;
	<span style="color: #0000ff;">this</span>.addButton <span style="color: #66cc66;">&#40;</span> <span style="color: #888800;">&quot;Menu.PlayButton&quot;</span>, <span style="color: #888800;">&quot;play_button_normal&quot;</span>, <span style="color: #888800;">&quot;play_button_pressed&quot;</span> <span style="color: #66cc66;">&#41;</span>
	<span style="color: #0000ff;">this</span>.addButton <span style="color: #66cc66;">&#40;</span> <span style="color: #888800;">&quot;Menu.OptionsButton&quot;</span>, <span style="color: #888800;">&quot;options_button_normal&quot;</span>, <span style="color: #888800;">&quot;options_button_pressed&quot;</span> <span style="color: #66cc66;">&#41;</span>
	<span style="color: #0000ff;">this</span>.addButton <span style="color: #66cc66;">&#40;</span> <span style="color: #888800;">&quot;Menu.CreditsButton&quot;</span>, <span style="color: #888800;">&quot;credits_button_normal&quot;</span>, <span style="color: #888800;">&quot;credits_button_pressed&quot;</span> <span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">end</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span></pre></div></div>

<p>6.	Create a new function to update the state of a button. The function takes the button tag and the button state as parameters. Create 2 local variables for the 2 possible states the button can have. These variables will be used as constants. The button state in parameter must match one of the constants.</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">function</span> ButtonsManager.setButtonTexture <span style="color: #66cc66;">&#40;</span> sButtonTag, kButtonState <span style="color: #66cc66;">&#41;</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
&nbsp;
    <span style="color: #0000ff;">local</span> hButton <span style="color: #66cc66;">=</span> <span style="color: #0000ff;">hud</span>.getComponent <span style="color: #66cc66;">&#40;</span> application.getCurrentUser <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>, sButtonTag <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> hButton <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">then</span>
        <span style="color: #0000ff;">local</span> kButtonStateNormal <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
        <span style="color: #0000ff;">local</span> kButtonStatePressed <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span></pre></div></div>

<p>Then, get the texture name corresponding to the wanted button state, and update the button texture :</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">        <span style="color: #0000ff;">local</span> sTexture
        <span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> kButtonState <span style="color: #66cc66;">==</span> kButtonStateNormal <span style="color: #66cc66;">&#41;</span>
        <span style="color: #0000ff;">then</span>
            sTexture <span style="color: #66cc66;">=</span> hashtable.get <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.htButtonsNormal <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>, sButtonTag <span style="color: #66cc66;">&#41;</span>
        <span style="color: #0000ff;">elseif</span> <span style="color: #66cc66;">&#40;</span> kButtonState <span style="color: #66cc66;">==</span> kButtonStatePressed <span style="color: #66cc66;">&#41;</span>
        <span style="color: #0000ff;">then</span>
            sTexture <span style="color: #66cc66;">=</span> hashtable.get <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.htButtonsPressed <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>, sButtonTag <span style="color: #66cc66;">&#41;</span>
        <span style="color: #0000ff;">end</span>
        <span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> sTexture <span style="color: #66cc66;">&#41;</span>
        <span style="color: #0000ff;">then</span>
            <span style="color: #0000ff;">hud</span>.setComponentBackgroundImage <span style="color: #66cc66;">&#40;</span> hButton, sTexture <span style="color: #66cc66;">&#41;</span>
        <span style="color: #0000ff;">end</span>
    <span style="color: #0000ff;">end</span>
&nbsp;
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">end</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span></pre></div></div>

<p>7.	Add the onMouseButtonDown handler to your AI. When this handler is being called, if a button is under the cursor, you have to change its texture (from normal to pressed). Start by checking which component is under the mouse cursor, and if this component is a button. If it’s the case, store the button tag in the sUnderMouseButton variable, to be able to restore its previous texture when the mouse button will be released.</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">function</span> ButtonsManager.onMouseButtonDown <span style="color: #66cc66;">&#40;</span> nButton, nPointX, nPointY, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ <span style="color: #66cc66;">&#41;</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
&nbsp;
    <span style="color: #0000ff;">local</span> hComponent <span style="color: #66cc66;">=</span> <span style="color: #0000ff;">hud</span>.getUnderCursorComponent <span style="color: #66cc66;">&#40;</span> application.getCurrentUser <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> hComponent <span style="color: #0000ff;">and</span> <span style="color: #0000ff;">hud</span>.getComponentType <span style="color: #66cc66;">&#40;</span> hComponent <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">==</span> <span style="color: #0000ff;">hud</span>.kComponentTypeButton <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">then</span>
        <span style="color: #0000ff;">local</span> sButtonTag <span style="color: #66cc66;">=</span> <span style="color: #0000ff;">hud</span>.getComponentTag <span style="color: #66cc66;">&#40;</span> hComponent <span style="color: #66cc66;">&#41;</span>
        <span style="color: #0000ff;">this</span>.sUnderMouseButton <span style="color: #66cc66;">&#40;</span> sButtonTag <span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>And then, update the component texture by calling the setButtonTexture function you have created a few minutes ago, using the right button state contant :</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;">        <span style="color: #0000ff;">local</span> kButtonStateNormal <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
        <span style="color: #0000ff;">local</span> kButtonStatePressed <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
&nbsp;
        <span style="color: #0000ff;">this</span>.setButtonTexture <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.sUnderMouseButton <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>, kButtonStatePressed <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">end</span>
&nbsp;
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">end</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span></pre></div></div>

<p>8.	Finally, create the onMouseButtonUp handler and restaure the button normal texture :</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">function</span> ButtonsManager.onMouseButtonUp <span style="color: #66cc66;">&#40;</span> nButton, nPointX, nPointY, nRayPntX, nRayPntY, nRayPntZ, nRayDirX, nRayDirY, nRayDirZ <span style="color: #66cc66;">&#41;</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">not</span> <span style="color: #0000ff;">string</span>.isEmpty <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.sUnderMouseButton <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">then</span>
        <span style="color: #0000ff;">local</span> kButtonStateNormal <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
        <span style="color: #0000ff;">local</span> kButtonStatePressed <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
&nbsp;
        <span style="color: #0000ff;">this</span>.setButtonTexture <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.sUnderMouseButton <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>, kButtonStateNormal <span style="color: #66cc66;">&#41;</span>
        <span style="color: #0000ff;">this</span>.sUnderMouseButton <span style="color: #66cc66;">&#40;</span> <span style="color: #888800;">&quot;&quot;</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">end</span>
&nbsp;
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">end</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span></pre></div></div>

<h2>Conclusion</h2>
<p>You can use this AIModel in all of your games, you only have to change the onInit content to match the buttons of your games.<br />
Do not forget to reference all your buttons textures in your game (Game Editor &gt; Resources tag).<br />
This sample also works for a multitouch application.</p>
<h2>Go further</h2>
<p>You can add more features to this AIModel, in order to be able to change the button text color for instance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stonetrip.com/developer/1727-buttons-normalpressed-textures-managment/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mix mouse and multitouch</title>
		<link>http://www.stonetrip.com/developer/1720-mix-mouse-and-multitouch</link>
		<comments>http://www.stonetrip.com/developer/1720-mix-mouse-and-multitouch#comments</comments>
		<pubDate>Wed, 16 Mar 2011 15:34:42 +0000</pubDate>
		<dc:creator>ST Team</dc:creator>
				<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://www.stonetrip.com/developer/?p=1720</guid>
		<description><![CDATA[When creating a game for a mobile device with multitouch capabilities, it's difficult to test the code inside ShiVa Editor on a computer with no multitouch. This tutorial explains you how to simulate the multitouch using the mouse.]]></description>
			<content:encoded><![CDATA[<h2>Script</h2>
<h3>Setup</h3>
<p>To mix multitouch and mouse events, we will redirect the mouse events to multitouch handlers when the multitouch is off.<br />
Create the mouse handlers (onMouseButtonDown, onMouseMove and onMouseButtonUp) and the multitouch handlers (onTouchSequenceBegin, onTouchSequenceChange and onTouchSequenceEnd ).</p>
<p>To know if the multitouch is enabled, add a variable bMultitouchEnabled in your AIModel and in a onInit handler, write the line:<br />
<strong>this.bMultitouchEnabled ( input.enableMultiTouch ( this.getUser ( ), true ) )</strong></p>
<p>You also have to add a variable bMouseDown to know if a mouse button is pressed.</p>
<h2>Mouse events redirection</h2>
<p>Now, you have to redirect mouse events to multitouch events when the multitouch is disabled.</p>
<h4>onMouseButtonDown</h4>
<p>In the onMouseButtonDown handler, set the bMouseDown variable to true and send an event to the onTouchSequenceBegin handler. Of course, this code has to be executed only if multitouch is disabled:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">not</span> <span style="color: #0000ff;">this</span>.bMultitouchEnabled <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">then</span>
    <span style="color: #0000ff;">this</span>.bMouseDown <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">true</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">this</span>.sendEvent <span style="color: #66cc66;">&#40;</span> <span style="color: #888800;">&quot;onTouchSequenceBegin&quot;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">end</span></pre></div></div>

<h4>onMouseButtonUp</h4>
<p>The code is pretty the same for the onMouseButtonUp handler, but the event has to be sent to the onTouchSequenceEnd handler, and you have to set the mouse bMouseDown variable to false.</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">not</span> <span style="color: #0000ff;">this</span>.bMultitouchEnabled <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">then</span>
    <span style="color: #0000ff;">this</span>.bMouseDown <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">false</span> <span style="color: #66cc66;">&#41;</span>
    <span style="color: #0000ff;">this</span>.sendEvent <span style="color: #66cc66;">&#40;</span> <span style="color: #888800;">&quot;onTouchSequenceEnd&quot;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">end</span></pre></div></div>

<h4>onMouseMove</h4>
<p>This handler will works like the 2 others, but have another restriction: the bMouseDown has to be true because on a multitouch device, it is not possible to interact with the screen without touching it, pressing the mouse button simulate the finger which is touching the screen.<br />
You also have to pass the nPointX and nPointY parameters of the onMouseMove handler to the onTouchSequenceChange handler.</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">not</span> <span style="color: #0000ff;">this</span>.bMultitouchEnabled <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">and</span> <span style="color: #0000ff;">this</span>.bMouseDown <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">then</span>
    <span style="color: #0000ff;">this</span>.sendEvent <span style="color: #66cc66;">&#40;</span> “onTouchSequenceChange”, <span style="color: #cc66cc;">1</span>, nPointX, nPointY, -<span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, -<span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">end</span></pre></div></div>

<p>The value “1” mean the touch n°0 is running. The two “-1” values mean the touch n°1 and n°2 are not running (refer the the onTouchSequenceChange to know all the parameters).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stonetrip.com/developer/1720-mix-mouse-and-multitouch/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Get path, filename and extension</title>
		<link>http://www.stonetrip.com/developer/1715-get-path-filename-and-extension-from-an-url</link>
		<comments>http://www.stonetrip.com/developer/1715-get-path-filename-and-extension-from-an-url#comments</comments>
		<pubDate>Wed, 16 Mar 2011 15:17:11 +0000</pubDate>
		<dc:creator>ST Team</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[filename]]></category>
		<category><![CDATA[path]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://www.stonetrip.com/developer/?p=1715</guid>
		<description><![CDATA[It is often interesting when you have to download an image to put into cache to get the name and the extension of the file in the URL.]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>For this tutorial, you need to declare a table in your AIModel variables, I named it tTable.<br />
create a function named getFile and add the variable sFileURL as parameter.</p>
<h2>Get the path</h2>
<p>Start the script by clearing the table data:<br />
<strong>table.empty ( this.tTable ( ) )</strong></p>
<p>Use now the string.explode. The delimiter will be the “/” character. Use the sFileURL variable plus “/” as first parameter, the table as second parameter, and “/” as delimiter (third parameter).<br />
<strong>string.explode ( sFileURL..&#8221;/&#8221;, this.tTable ( ), &#8220;/&#8221; )</strong></p>
<p>The last item of the table is the filename with its extension:</p>
<h2>Get the filename and the extension</h2>
<p><strong>local sFilenameAndExtension = table.getLast ( this.tTable ( ) )</strong></p>
<p>Logically, the path of the file is the URL without the file and the extension:<br />
<strong>local sPath = string.getSubString ( sFileURL, 0, string.getLength ( sFileURL ) &#8211; string.getLength ( sFilenameAndExtension ) )</strong></p>
<p>Now you have to separe the filename to its extension, you must find the “.” symbol:<br />
<strong>local nDotIndex = string.findFirst ( sFilenameAndExtension, &#8220;.&#8221;, 0 )</strong></p>
<p>The extension is the part at the right of the string:<br />
<strong>local sExtension = string.getSubString ( sFilenameAndExtension, nDotIndex, string.getLength ( sFilenameAndExtension ) &#8211; nDotIndex )</strong></p>
<p>And the filename is the other part:<br />
<strong>local sFilename = string.getSubString ( sFilenameAndExtension, 0, string.getLength ( sFilenameAndExtension ) &#8211; string.getLength ( sExtension ) )</strong></p>
<p>It is finished, you can return the results:<br />
<strong>return sPath, sFilename, sExtension</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.stonetrip.com/developer/1715-get-path-filename-and-extension-from-an-url/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rotate the viewport</title>
		<link>http://www.stonetrip.com/developer/1704-rotate-the-viewport-according-to-the-device-rotation</link>
		<comments>http://www.stonetrip.com/developer/1704-rotate-the-viewport-according-to-the-device-rotation#comments</comments>
		<pubDate>Wed, 16 Mar 2011 12:15:47 +0000</pubDate>
		<dc:creator>ST Team</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[System]]></category>
		<category><![CDATA[accelerometer]]></category>
		<category><![CDATA[device]]></category>
		<category><![CDATA[rotation]]></category>
		<category><![CDATA[viewport]]></category>

		<guid isPermaLink="false">http://www.stonetrip.com/developer/?p=1704</guid>
		<description><![CDATA[Learn how to rotate the viewport according to the device rotation using the accelerometer]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>An accelerometer track the forces applied on the device. When the device don’t move, the only force recorded is the gravity.</p>
<p>You have to consider that your device defines the center of a (x,y) space.<br />
When the viewport is set to 0, the basis is in this orientation:</p>
<p>If you rotate the device, the basis rotates too, in the same direction.</p>
<div style="background-color: #ffffff; border: 1px solid #BBBBBB;"><img class="alignnone size-full wp-image-1696" style="background-color: transparent; border: 0px;" title="image02" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image021.jpg" alt="" width="291" height="276" /><img class="alignnone size-full wp-image-1703" style="background-color: transparent; border: 0px;" title="image09" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image091.jpg" alt="" width="394" height="167" /></div>
<p>In fact, the basis follow the iPhone rotation. When you will change the viewport rotation, the basis orientation will get an additional rotation (here +90°):<br />
<img class="alignnone size-full wp-image-1694" title="image00" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image001.jpg" alt="" width="386" height="151" /></p>
<h2>Script</h2>
<p>Create a new AIModel named ViewportRotation, and create a variable named nOptionValue, type number. This variable will be used to store the current viewport rotation value. Add a onInit handler in which your set this variable to 0 and the viewport rotation to the same value:<br />
<a rel="prettyPhoto[tuto]" href="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image081.jpg"><img class="alignnone size-large wp-image-1702" title="image08" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image081-600x188.jpg" alt="" width="600" height="188" /></a></p>
<p>Create now a onJoypadMove handler. The parameters we will use are nAxisX and nAxisY, corresponding to the position of the accelerometer on the x, y axis of the basis (don’t forget the accelerometer follow the gravity).</p>
<p>You can consider the device is enough rotated and the viewport has to changewhen the value of an axis is greater than 1.5 or lesser than -1.5,. In all the cases, you will add a value to the current rotation, and in all the cases, you have to modulate the result by 4, because the viewport rotation value is 0, 1, 2 or 3 (corresponding to the default rotation (0°) plus 1*90°, 2*90° or 3*90°).</p>
<p>When the device is the default position, the gravity force is on the Y axis only, in the negative side, but in this case, no need to change the viewport rotation because it is already the current one.</p>
<h2>Rotation 180</h2>
<p>When you rotate the device backward, the gravity is on the Y axis, in the positive side. At this moment, the needed viewport rotation is the opposite of the current one. To know the new viewport rotation, you have add 2 rotations of 90° (+2) on the current value (modulus 4).</p>
<div style="background-color: #ffffff; border: 1px solid #BBBBBB;"><img class="alignnone size-full wp-image-1696" style="background-color: transparent; border: 0px;" title="image02" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image021.jpg" alt="" width="291" height="276" /><img class="alignnone size-full wp-image-1705" style="background-color: transparent; border: 0px;" title="image03" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image032.jpg" alt="" width="304" height="271" /><img class="alignnone size-full wp-image-1699" style="background-color: transparent; border: 0px;" title="image05" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image051.jpg" alt="" width="288" height="273" /></div>
<h2>Rotation 90</h2>
<p>When you rotate the device on the right, the value of the gravity on the X axis become greater. When it is greater than 1.5, the viewport rotation has to be changed by the current value plus 1 rotation of 90° (+1) (modulus 4).</p>
<div style="background-color: #ffffff; border: 1px solid #BBBBBB;"><img class="alignnone size-full wp-image-1696" style="background-color: transparent; border: 0px;" title="image02" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image021.jpg" alt="" width="291" height="276" /><img class="alignnone size-full wp-image-1701" style="background-color: transparent; border: 0px;" title="image07" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image071.jpg" alt="" width="387" height="180" /><img class="alignnone size-full wp-image-1694" style="background-color: transparent; border: 0px;" title="image00" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image001.jpg" alt="" width="386" height="151" /></div>
<h2>Rotation 270</h2>
<p>When you rotate the device on the left, the viewport has to be rotated 3 times: the new value is the current value plus 3 rotation of 90° (+3) (modulus 4).</p>
<div style="background-color: #ffffff; border: 1px solid #BBBBBB;"><img class="alignnone size-full wp-image-1696" style="background-color: transparent; border: 0px;" title="image02" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image021.jpg" alt="" width="291" height="276" /><img class="alignnone size-full wp-image-1698" style="background-color: transparent; border: 0px;" title="image04" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image041.jpg" alt="" width="383" height="185" /><img class="alignnone size-full wp-image-1695" style="background-color: transparent; border: 0px;" title="image01" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image011.jpg" alt="" width="384" height="149" /></div>
<p>Of course, don’t forget execute the modulus and to apply the new viewport rotation at the end of the script.</p>
<p>You can see the full code here:<br />
<a rel="prettyPhoto[tuto]" href="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image061.jpg"><img class="alignnone size-large wp-image-1700" title="image06" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image061-600x299.jpg" alt="" width="600" height="299" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.stonetrip.com/developer/1704-rotate-the-viewport-according-to-the-device-rotation/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Add/Set a hashtable value easily</title>
		<link>http://www.stonetrip.com/developer/1686-addset-a-hashtable-value-easily</link>
		<comments>http://www.stonetrip.com/developer/1686-addset-a-hashtable-value-easily#comments</comments>
		<pubDate>Wed, 16 Mar 2011 11:46:17 +0000</pubDate>
		<dc:creator>ST Team</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[add]]></category>
		<category><![CDATA[hastable]]></category>
		<category><![CDATA[key]]></category>

		<guid isPermaLink="false">http://www.stonetrip.com/developer/?p=1686</guid>
		<description><![CDATA[When you want to set a key/value in a hashtable, you have to check if the key is already existing before doing a hashtable.set (), because if the key doesn’t exists, the function won’t do anything.]]></description>
			<content:encoded><![CDATA[<h2>The solution</h2>
<p>To solve this problem without making your script unreadable, I suggest you to create a function “addToHashtable” you will call, which will do automatically the check and add the key and in the hashtable if not existing, or only set the value in the other case.</p>
<p>The function take 3 parameters: the hashtable, the key and the value.</p>
<p>The code of the function is this one:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">function</span> MyGame.addToHashtable <span style="color: #66cc66;">&#40;</span> htHashtable, sKey, vValue <span style="color: #66cc66;">&#41;</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
&nbsp;
   <span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> hashtable.contains <span style="color: #66cc66;">&#40;</span> htHashtable, sKey <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
   <span style="color: #0000ff;">then</span>
       <span style="color: #008000; font-style: italic;">-- If the key is in the hashtable, update the value</span>
       hashtable.set <span style="color: #66cc66;">&#40;</span> htHashtable, sKey, vValue <span style="color: #66cc66;">&#41;</span>
   <span style="color: #0000ff;">else</span>
       <span style="color: #008000; font-style: italic;">-- If the key is not in the hashtable, create a new entry in the hashtable, with sKey and vValue as key/value for this entry.</span>
       hashtable.add <span style="color: #66cc66;">&#40;</span> htHashtable, sKey, vValue <span style="color: #66cc66;">&#41;</span>
   <span style="color: #0000ff;">end</span>
&nbsp;
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">end</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span></pre></div></div>

<p>You can call the function like that:</p>
<p>this.addToHashtable ( this.myHashtable ( ), “myKey”, 3 )</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stonetrip.com/developer/1686-addset-a-hashtable-value-easily/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Create a popup window</title>
		<link>http://www.stonetrip.com/developer/1660-create-a-popup-window</link>
		<comments>http://www.stonetrip.com/developer/1660-create-a-popup-window#comments</comments>
		<pubDate>Wed, 16 Mar 2011 11:09:46 +0000</pubDate>
		<dc:creator>ST Team</dc:creator>
				<category><![CDATA[HUD]]></category>

		<guid isPermaLink="false">http://www.stonetrip.com/developer/?p=1660</guid>
		<description><![CDATA[Learn how to create an iPhone like notification message using HUD components and actions]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>A popup is composed by a background and windows containing a title, a description and a button to close the popup.</p>
<p>The popup will be composed by 2 main HUD components: one for the background and a container to place and scale the popup window.<br />
These 2 components will have a big Z-Order to appear in front of other HUDs of your game, and the background as to appear behind the window, their Z-Order will be 249 for the background and 250 for the window.</p>
<h2>Platform setup</h2>
<p>In ShiVa Editor, setup your desktop to have the HUD Editor and the scene viewer at the same time.<br />
Define the resolution of the viewport as for an iPhone (in the Scene Viewer, click on Display&gt;Size&gt;iPhone&gt;320&#215;480:<br />
<img class="alignnone size-large wp-image-1677" title="image14" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image14-600x595.jpg" alt="" width="600" height="595" /></p>
<h2>Game setup</h2>
<p>For this tutorial, you will need to have a game with a Main AIModel. In this AIModel, create a handler “onDestroyTemplateInstance”.<br />
In this handler, add the parameter sTemplate, and write the script: hud.destroyTemplateInstance ( this.getUser ( ), sTemplate )<br />
<img class="alignnone size-large wp-image-1671" title="image08" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image08-600x164.jpg" alt="" width="600" height="164" /></p>
<h2>The Background</h2>
<p>In the HUD Editor, create a new HUD template and create a new component, named Background, full size (100%x100%) and set the ZOrder to 249.<br />
In the appearance section, set the background color to black and not at full opacity (ex: 0, 0, 0, 200). No border color (alpha: 0).<br />
<img class="alignnone size-full wp-image-1679" title="image16" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image16.jpg" alt="" width="120" height="37" /></p>
<p>Create now 2 actions named appearBackground and disappearBackground:<br />
<img class="alignnone size-full wp-image-1678" title="image15" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image15.jpg" alt="" width="483" height="38" /></p>
<p>In each action, add a command stopAction to stop the other one. (When the disappearBackground action will be called, it will stop the appearBackground action).</p>
<p>In the appearBackground action, add a command to interpolate the opacity to 255 on the Background component, in a linear way, during 250 ms. Add the same command in the disappearBackground action, but with an opacity of 0.<br />
<img class="alignnone size-full wp-image-1672" title="image09" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image09.jpg" alt="" width="501" height="112" /></p>
<h2>The window</h2>
<p>Create now the container for the popup window. It has a full size (100%x100%) and a ZOrder at 250. In the appearance section, set the alpha of the background color and the border color to 0. Press OK to close the component edition.</p>
<p>Add now a child to this container as follow:<br />
<img class="alignnone size-full wp-image-1665" title="image02" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image02.jpg" alt="" width="309" height="201" /></p>
<p>This component is the main window of the popup. Name it Window. It will contain the title of the window, the description and the exit button, so set its type to Container. Make it aspect ratio independant and size it to 50&#215;31. Add a background image on it (the background of the popup) and check the background color, it must be 127, 127, 127, 255.</p>
<h3>Title Label</h3>
<p>Add the title, a label component, placed at the top of its container (Origin: top, position: 50-100), with a size of 100&#215;25.<br />
As it is a component used for writing text, It has no background color and no border color (alpha = 0) but a foreground color set to 127, 127, 127, 255 to have white text.<br />
In the Label section, set a text to “This will be the title” for instance (it is just for text, it will be overwritten in another part of the tutorial) and the Text Height to 75.<br />
You also have to choose a Font. To finish, set the Text Alignment to Center / Center</p>
<h3>Description Label</h3>
<p>Create another component in the Window container, type label, placed at 50-53 (origin Center) and with a size of 90&#215;47.<br />
It is a text component too, so set the alpha of the background and border color to 0 and set the foreground color to 127, 127, 127, 255.<br />
In the Label section, add the text “There will be my description in this label”, choose the Font set the Text Height to 33 to have a text in 3 lines (100 / 33 &gt;= 3).<br />
Set the Text Alignment to Left / Center.</p>
<h3>Close button</h3>
<p>On the same container, add now a button component which will be used to close the popup.<br />
Place it at the bottom (Origin Bottom, Position 50-6) and size it to 80&#215;21.<br />
The button will have a background image (BackColor to 127, 127, 127, 255) and a text (ForeColor to 127, 127, 127, 255).<br />
No border color (alpha=0). In the Button section, add the text “OK”, set the Text Height to 65, the Text Alignment to Center / Center and choose a Font.</p>
<p>If you have done all the previous steps right, you should have this structure:<br />
<img class="alignnone size-full wp-image-1663" title="image00" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image00.jpg" alt="" width="148" height="127" /></p>
<p>And the appearance like that:<br />
<img class="alignnone size-full wp-image-1670" title="image07" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image07.jpg" alt="" width="375" height="534" /></p>
<h2>Animation</h2>
<p>Create now 2 actions named appear and disappear:<br />
<img class="alignnone size-full wp-image-1667" title="image04" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image04.jpg" alt="" width="483" height="36" /></p>
<p>As for appearBackground and disappearBackground, add a stopAction in each action to stop the other one.<br />
<img class="alignnone size-full wp-image-1680" title="image17" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image17.jpg" alt="" width="193" height="129" /></p>
<h3>Appear</h3>
<p>Edit the Container component and set its Visible state to false.</p>
<p>In the appear action, add a command SetSize on the Container component, to 0, 0, and then, another command to make it visible (SetVisible with true as parameter).</p>
<p>Add a CallAction command to call the appearBackground action, and set the Wait parameter to false.<br />
Add now the final command, type InterpolateSize on the Container component, to the size 100&#215;100, with a Spring2 effect during 1500 ms.</p>
<p>Note Wait parameter set to false for the CallAction to appearBackground will make both interpolations (InterpolateSize of the Container and InterpolateOpacity of the Background) to run at the same time.<br />
<img class="alignnone size-full wp-image-1676" title="image13" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image13.jpg" alt="" width="362" height="107" /></p>
<p>Set now this action (appear) as the Initial Action.<br />
<img class="alignnone size-full wp-image-1673" title="image10" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image10.jpg" alt="" width="337" height="76" /></p>
<p><img class="alignnone size-full wp-image-1668" title="image05" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image05.jpg" alt="" width="287" height="130" /></p>
<p><img class="alignnone size-full wp-image-1675" title="image12" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image12.jpg" alt="" width="169" height="34" /></p>
<h3>Disappear</h3>
<p>In the disappear action, call the disappearBackground action (add a CallAction command), with a Wait parameter at false and then, add a command to interpolate the size of the Container component (InterpolateSize command), to 0&#215;0, with a linear animation, and during 200 ms.</p>
<p>Finally, add a command SendEventToUser, to the handler created at the beginning of this tutorial, onDestroyTemplateInstance, and set the first parameter type to String, and the value to “Popup”.</p>
<p>The action should be like that:<br />
<img class="alignnone size-full wp-image-1674" title="image11" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image11.jpg" alt="" width="501" height="89" /></p>
<p>Congratulations, it is finish for the HUD Editor part, the HUD template should looks like that:<br />
<img class="alignnone size-full wp-image-1669" title="image06" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image06.jpg" alt="" width="535" height="559" /></p>
<h2>How to make it works?</h2>
<p>In your game script, when you want to make your popup appearing, you have to create the template instance, and define the text of its title and description, for example:<br />
<a rel="prettyPhoto[tuto]" href="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image01.jpg"><img class="alignnone size-large wp-image-1664" title="image01" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image01-600x34.jpg" alt="" width="600" height="34" /></a></p>
<p>As result, you have a beautiful window appearing in a very pleasant manner:<br />
<img class="alignnone size-full wp-image-1666" title="image03" src="http://www.stonetrip.com/developer/wp-content/uploads/2011/03/image03.jpg" alt="" width="516" height="534" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.stonetrip.com/developer/1660-create-a-popup-window/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Get an Xml without using states</title>
		<link>http://www.stonetrip.com/developer/1656-get-an-xml-without-using-states</link>
		<comments>http://www.stonetrip.com/developer/1656-get-an-xml-without-using-states#comments</comments>
		<pubDate>Wed, 16 Mar 2011 10:31:12 +0000</pubDate>
		<dc:creator>ST Team</dc:creator>
				<category><![CDATA[Basics]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[handler]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[state]]></category>
		<category><![CDATA[status]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.stonetrip.com/developer/?p=1656</guid>
		<description><![CDATA[Here is a very easy to use sample of script to get an xml. Instead of using states, I recommend using handlers...]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>States suffice when you only have one xml to get, but when you want to load more than one file, or when you want to use states for another usage, it becomes difficult to use them for the xml.<br />
With handlers, you only have to create a handler and an AI variable per xml to get.</p>
<h2>Setup</h2>
<p>Firstly, create a variable in your AIModel, named myXml1 in my case, type xml. Then created a handler (onGetMyXml1 for instance).<br />
The handler will be called once when you will decide to get the xml file, and then, the handler will (down)load it and call itself until the xml is not received.</p>
<h2>Test the status</h2>
<p>At the beginning of the script, get the status of the xml variable:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>em<span style="color: #66cc66;">&gt;</span>local status <span style="color: #66cc66;">=</span> xml.getReceiveStatus <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.myXml1 <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&lt;/</span>em<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>If the status is 1, it means the xml has been received and you can parse it.</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> status <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">then</span>
     <span style="color: #0000ff;">local</span> root <span style="color: #66cc66;">=</span> xml.getRootElement <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.myXml1 <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
     <span style="color: #008000; font-style: italic;">--To do</span>
<span style="color: #0000ff;">else</span></pre></div></div>

<p>I have written the first step of the parse process (get the root element), you now have replace the “&#8211;To do” by your own parse process, according to your xml structure.</p>
<p>In the other case, the xml hasn&#8217;t been received yet, and as I said previously, the handler will call itself while receiving the xml, so in the &#8220;else&#8221;, you have to put:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>em<span style="color: #66cc66;">&gt;</span>this.postEvent <span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span>, <span style="color: #888800;">&quot;onGetMyXml1&quot;</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&lt;/</span>em<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>Using the postEvent function with 0 as time parameter mean that the event will be executed at the next frame.<br />
In that case, the status can be < 0 or 0<=status<1. If the status is lesser than 0, it means that the xml hasn’t been started or the receive process has failed, so you have to start the receive process:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> status <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">then</span>
   <span style="color: #0000ff;">local</span> sUrl <span style="color: #66cc66;">=</span> <span style="color: #888800;">&quot;http://www.myWebsite.com/myXml.xml&quot;</span>
   xml.receive <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.myXml1 <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>, sUrl <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">end</span></pre></div></div>

<p>Of course, you have to replace the string sURL by your own URL.</p>
<h2>Conclusion</h2>
<p>Congratulations, you have finished this tutorial. Note there is no dedicated script when the status is &gt;= 0 and &lt; 1, because you just have to wait until the xml receive process ends, so the script will do nothing except calling the handler again at next frame.</p>
<p>If you have another xml, you just have to create a new xml variable, copy/paste your handler and change the variable name and the handler name in the postEvent function.<br />
Don’t forget, you have to call each handler only one time on a onInit for example or when you want to receive the xml file.</p>
<p>You can see the fully code there:</p>

<div class="wp_syntax"><div class="code"><pre class="lua" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">function</span> myGame.onGetMyXml1 <span style="color: #66cc66;">&#40;</span>  <span style="color: #66cc66;">&#41;</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
&nbsp;
<span style="color: #0000ff;">local</span> status <span style="color: #66cc66;">=</span> xml.getReceiveStatus <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.myXml1 <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> status <span style="color: #66cc66;">==</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">then</span>
  <span style="color: #0000ff;">local</span> root <span style="color: #66cc66;">=</span> xml.getRootElement <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.myXml1 <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
   <span style="color: #008000; font-style: italic;">--To do</span>
<span style="color: #0000ff;">else</span>
      <span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span> status <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>
      <span style="color: #0000ff;">then</span>
           <span style="color: #0000ff;">local</span> sUrl <span style="color: #66cc66;">=</span> <span style="color: #888800;">&quot;http://www.myWebsite.com/myXml.xml&quot;</span>
           xml.receive <span style="color: #66cc66;">&#40;</span> <span style="color: #0000ff;">this</span>.myXml1 <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>, sUrl <span style="color: #66cc66;">&#41;</span>
      <span style="color: #0000ff;">end</span>
      <span style="color: #0000ff;">this</span>.postEvent <span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span>, <span style="color: #888800;">&quot;onGetMyXml1&quot;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #0000ff;">end</span>
&nbsp;
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span>
<span style="color: #0000ff;">end</span>
<span style="color: #008000; font-style: italic;">--------------------------------------------------------------------------------</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.stonetrip.com/developer/1656-get-an-xml-without-using-states/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

