{"id":2521,"date":"2012-07-13T11:10:13","date_gmt":"2012-07-13T11:10:13","guid":{"rendered":"http:\/\/www.drmop.com\/?p=2521"},"modified":"2012-07-13T11:10:13","modified_gmt":"2012-07-13T11:10:13","slug":"iwgame-engine-using-templates-tutorial","status":"publish","type":"post","link":"http:\/\/www.drmop.com\/index.php\/2012\/07\/13\/iwgame-engine-using-templates-tutorial\/","title":{"rendered":"IwGame Engine &#8211; Using Templates Tutorial"},"content":{"rendered":"<p>New here? What\u2019s IwGame? IwGame is an open source free to use cross platform game engine for iPhone, iPad, Android, Bada, Playbook, Symbian, Windows Mobile, LG-TV, Windows and Mac, built\u00a0on top of the\u00a0Marmalade SDK.<\/p>\n<p>A forum poster asked a question recently that made me realise that XOML Templates can quite easily be overlooked as a means for creating re-usable actors and such, so I thought to help make Templates more visible that I would create a very quick tutorial showing how to use them.<\/p>\n<p>To begin with, lets take a look at what a template actually is and does. A template is a XOML tag that allows you to define generic XOML that is not instantiated immediately, yuo can think of a Template as a blue print for something that you will later instantiate into your game or app world. A template also takes any number of parameters that can be passed when you instantiate the template.<\/p>\n<p>When creating items inside a Template, template parameters are defined using a template parameter name that is enclosed inside double dollar signs ($$), for example $position$. When you later instantiate the items within the template the parameters within the template will be replaced by values that are passed to the template.<\/p>\n<p>Lets take a quick look at creating an actor \/ child actor with a template<\/p>\n<p>[sourcecode language=&#8221;xml&#8221;]<br \/>\n    &lt;Template Name=&quot;TankTemp&quot;&gt;<br \/>\n        &lt;TankActor Name=&quot;$name$&quot; Style=&quot;TankActorStyle&quot; Position=&quot;$pos$\u2033 &gt;<br \/>\n            &lt;TankActor Name=&quot;$name$_sel&quot; Style=&quot;TankActorSelectedStyle&quot; Position=&quot;0, 0\u2033 \/&gt;<br \/>\n        &lt;\/TankActor&gt;<br \/>\n    &lt;\/Template&gt;<br \/>\n[\/sourcecode]<\/p>\n<p>Here we create a template called TankTemp that defined an actor with a name of $name$ and a position of $pos$. Note that because these two parameters are enclosed in $$ they are classed as template parameters.<\/p>\n<p>Now to instantiate this elements within this template in XOML we use the following:<\/p>\n<p>[sourcecode language=&#8221;xml&#8221;]<br \/>\n        &lt;FromTemplate Template=&quot;TankTemp&quot; name=&quot;Tank&quot; pos=&quot;310, -120&quot; \/&gt;<br \/>\n[\/sourcecode]<\/p>\n<p>The above code will instantiate the following code:<\/p>\n<p>[sourcecode language=&#8221;xml&#8221;]<br \/>\n    &lt;TankActor Name=&quot;Tank&quot; Style=&quot;TankActorStyle&quot; Position=&quot;310, -120\u2033 &gt;<br \/>\n        &lt;TankActor Name=&quot;Tank_sel&quot; Style=&quot;TankActorSelectedStyle&quot; Position=&quot;0, 0\u2033 \/&gt;<br \/>\n    &lt;\/TankActor&gt;<br \/>\n[\/sourcecode]<\/p>\n<p>To instantiate a template from C++, we firstly need to find the template, build the parameters then instantiate the template passing in the parameters:<\/p>\n<p>[sourcecode language=&#8221;cpp&#8221;]<br \/>\n\/\/ Find the tank template<br \/>\nCIwGameTemplate* temp = (CIwGameTemplate*)scene-&gt;getResourceManager()-&gt;findResource(&quot;TankTemp&quot;, &quot;template&quot;);<br \/>\nif (temp != NULL)<br \/>\n{<br \/>\n\t\/\/ Create a set of XML attributes that will replace the template parameters<br \/>\n\tCIwGameXmlNode* replacements = new CIwGameXmlNode();<br \/>\n\treplacements-&gt;Managed = false;<br \/>\n\tCIwGameXmlAttribute* attrib;<\/p>\n<p>\t\/\/ Set name template parameter<br \/>\n\tattrib = new CIwGameXmlAttribute();<br \/>\n\tattrib-&gt;Managed = false;<br \/>\n\tattrib-&gt;setName(&quot;name&quot;);<br \/>\n\tattrib-&gt;setValue(&quot;Tank&quot;);<br \/>\n\treplacements-&gt;AddAttribute(attrib);<\/p>\n<p>\t\/\/ Set position template parameter<br \/>\n\tattrib = new CIwGameXmlAttribute();<br \/>\n\tattrib-&gt;Managed = false;<br \/>\n\tattrib-&gt;setName(&quot;pos&quot;);<br \/>\n\tattrib-&gt;setValue(&quot;310, -120&quot;);<br \/>\n\treplacements-&gt;AddAttribute(attrib);<\/p>\n<p>\t\/\/ Instantiate the Tank template<br \/>\n\ttemp-&gt;Instantiate(scene, replacements);<\/p>\n<p>\t\/\/ Clean up replacement attributes<br \/>\n\tdelete replacements;<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>New here? What\u2019s IwGame? IwGame is an open source free to use cross platform game engine for iPhone, iPad, Android, Bada, Playbook, Symbian, Windows Mobile, LG-TV, Windows and Mac, built\u00a0on top of the\u00a0Marmalade SDK. A forum poster asked a question recently that made me realise that XOML Templates can quite easily be overlooked as a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,445,43,118,44,42,181,45],"tags":[746,159,453,452,451,46,739],"class_list":["post-2521","post","type-post","status-publish","format-standard","hentry","category-android-app-development","category-appeasy","category-blackberry-playbook-app-development","category-c-programming","category-game-and-app-development","category-ios-app-development","category-iwgame-engine","category-samsung-bada-development","tag-appeasy","tag-iwgame","tag-markup","tag-parameters-2","tag-template","tag-tutorial","tag-xoml"],"_links":{"self":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/2521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/comments?post=2521"}],"version-history":[{"count":4,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/2521\/revisions"}],"predecessor-version":[{"id":2525,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/2521\/revisions\/2525"}],"wp:attachment":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/media?parent=2521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/categories?post=2521"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/tags?post=2521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}