<li><aclass="backlink"title="Click to do a full-text search for this title"href="./ScriptSyntax?action=fullsearch&value=linkto%3A%22ScriptSyntax%22&context=180">ScriptSyntax</a></li>
<spanclass="anchor"id="line-2"></span><spanclass="anchor"id="line-3"></span><pclass="line862">Script files are written in a very specific way. There are rules to follow for creating the various parts of the file, and for controlling the logic. This rule set is called a <em>syntax</em>. <spanclass="anchor"id="line-4"></span><spanclass="anchor"id="line-5"></span><pclass="line862">(i) If you are familiar with most programming languages, you won't find any big changes here. The script syntax is very similar to C / C++. You might want to check out the <ahref="./ScriptVariable.html">section on variables</a> to see the changes there. <spanclass="anchor"id="line-6"></span><spanclass="anchor"id="line-7"></span><pclass="line867">
<spanclass="line"><spanclass="LineNumber"> 5 </span><spanclass="Comment">//assignment here. We place the values on the right into the variable on the left. </span></span>
<spanclass="line"><spanclass="LineNumber"> 7 </span><spanclass="ID">b</span><spanclass="SPChar">=</span><spanclass="ID">a</span><spanclass="SPChar">;</span><spanclass="Comment">//now b is also equal to 2.</span></span>
<spanclass="line"><spanclass="LineNumber"> 10 </span><spanclass="ID">a</span><spanclass="SPChar">=</span><spanclass="Number">1</span><spanclass="Number">0</span><spanclass="SPChar">*</span><spanclass="ID">b</span><spanclass="SPChar">;</span><spanclass="Comment">//a is now 20.</span></span>
<spanclass="line"><spanclass="LineNumber"> 13 </span><spanclass="ID">a</span><spanclass="SPChar">=</span><spanclass="ID">b</span><spanclass="SPChar">/</span><spanclass="Number">5</span><spanclass="SPChar">;</span><spanclass="Comment">//now a is 4-- following along?</span></span>
<spanclass="line"><spanclass="LineNumber"> 22 </span><spanclass="ID">a</span><spanclass="SPChar">+</span><spanclass="SPChar">+</span><spanclass="SPChar">;</span><spanclass="Comment">//++ raises a number's value by 1.</span></span>
<spanclass="line"><spanclass="LineNumber"> 25 </span><spanclass="ID">a</span><spanclass="SPChar">-</span><spanclass="SPChar">-</span><spanclass="SPChar">;</span><spanclass="Comment">//and -- does the opposite.</span></span>
<spanclass="line"><spanclass="LineNumber"> 27 </span><spanclass="Comment">//look further in the page for the example of 'if'.</span></span>
<spanclass="line"><spanclass="LineNumber"> 28 </span><spanclass="Comment">//but know here we're comparing a and b. We are not saying that a equals b, that would be =.</span></span>
<spanclass="line"><spanclass="LineNumber"> 30 </span><spanclass="ID">sys</span><spanclass="SPChar">.</span><spanclass="ID">println</span><spanclass="SPChar">(</span><spanclass="String">"a and b are the same"</span><spanclass="SPChar">)</span><spanclass="SPChar">;</span></span>
<spanclass="line"><spanclass="LineNumber"> 32 </span><spanclass="ID">sys</span><spanclass="SPChar">.</span><spanclass="ID">println</span><spanclass="SPChar">(</span><spanclass="String">"a and b are different"</span><spanclass="SPChar">)</span><spanclass="SPChar">;</span></span>
<spanclass="anchor"id="line-63"></span><spanclass="anchor"id="line-64"></span><pclass="line874">A function is collection of script commands designed around a specific purpose. Usually they're built to do one thing that takes a bunch of smaller steps. You might write a function to activate all the monsters in a room, start up some machinery, or perform any small task. <spanclass="anchor"id="line-65"></span><spanclass="anchor"id="line-66"></span><pclass="line867"><strong><ttclass="backtick">[return type] [function name] ( [parameter list ]) {</tt></strong><spanclass="anchor"id="line-67"></span><spanclass="anchor"id="line-68"></span><pclass="line867"><strong><ttclass="backtick">\\function stuff goes here</tt><spanclass="anchor"id="line-69"></span><spanclass="anchor"id="line-70"></span><pclass="line867"></strong><ttclass="backtick">}</tt><strong><spanclass="anchor"id="line-71"></span><spanclass="anchor"id="line-72"></span><pclass="line862">The <ttclass="backtick">[return type]</tt> is one of the <ahref="./ScriptVariable.html">variable types</a> the script system uses. <spanclass="anchor"id="line-73"></span><spanclass="anchor"id="line-74"></span><pclass="line862">The <ttclass="backtick">[function name]</tt> can be any name you like, however it cannot contain any spaces. <spanclass="anchor"id="line-75"></span><spanclass="anchor"id="line-76"></span><pclass="line862">The <ttclass="backtick">[parameter list]</tt> is a collection of <ahref="./ScriptVariable.html">variables</a> you want to send into the function. If there are none, leave that section blank. <spanclass="anchor"id="line-77"></span><spanclass="anchor"id="line-78"></span><pclass="line867"></strong>Example:<strong><spanclass="anchor"id="line-79"></span><spanclass="anchor"id="line-80"></span><pclass="line867"><spanclass="anchor"id="line-81"></span><spanclass="anchor"id="line-82"></span><spanclass="anchor"id="line-83"></span><spanclass="anchor"id="line-84"></span><spanclass="anchor"id="line-85"></span><spanclass="anchor"id="line-86"></span><spanclass="anchor"id="line-87"></span><spanclass="anchor"id="line-88"></span><spanclass="anchor"id="line-89"></span><spanclass="anchor"id="line-90"></span><spanclass="anchor"id="line-91"></span><spanclass="anchor"id="line-92"></span><spanclass="anchor"id="line-93"></span><spanclass="anchor"id="line-94"></span><spanclass="anchor"id="line-95"></span><spanclass="anchor"id="line-96"></span><spanclass="anchor"id="line-97"></span><spanclass="anchor"id="line-98"></span><spanclass="anchor"id="line-99"></span><spanclass="anchor"id="line-100"></span><spanclass="anchor"id="line-101"></span><spanclass="anchor"id="line-102"></span><spanclass="anchor"id="line-103"></span><spanclass="anchor"id="line-104"></span><divclass="codearea"dir="ltr"lang="en">
<spanclass="line"><spanclass="LineNumber"> 6 </span><spanclass="ID">sys</span><spanclass="SPChar">.</span><spanclass="ID">println</span><spanclass="SPChar">(</span><spanclass="String">"You sent in "</span><spanclass="SPChar">+</span><spanclass="ID">f</span><spanclass="SPChar">+</span><spanclass="String">", which when doubled is "</span><spanclass="SPChar">+</span><spanclass="ID">f</span><spanclass="SPChar">;</span></span>
<spanclass="anchor"id="line-107"></span><spanclass="anchor"id="line-108"></span><pclass="line862">Functions can be executed, or called, from the map or from within the script. To call functions from a map, use an entity that supports the call keypair and assign it a function. Typically, triggers are what support the call function, though there are other cases. To call the <ttclass="backtick">simpleFunction</tt> example above, you could enable a <ahref="./Entity_TriggerOnce.html">trigger_once</a> with the following keypair: <spanclass="anchor"id="line-109"></span><spanclass="anchor"id="line-110"></span><pclass="line867"><ttclass="backtick">call simpleFunction</tt><spanclass="anchor"id="line-111"></span><spanclass="anchor"id="line-112"></span><pclass="line874">Triggers that fire more than once will call their functions each time they fire. Be wary of this when you set up your triggers. <spanclass="anchor"id="line-113"></span><spanclass="anchor"id="line-114"></span><pclass="line862">To have a function called from within script, see the <ahref="./AdvancedScriptTutorial.html">AdvancedScriptTutorial</a>. <spanclass="anchor"id="line-115"></span><spanclass="anchor"id="line-116"></span><pclass="line867">
<spanclass="anchor"id="line-117"></span><spanclass="anchor"id="line-118"></span><pclass="line874">There are some command words in the script syntax that are used to perform various logic functions. Looping, decision making, running different function threads, and so on. <spanclass="anchor"id="line-119"></span><spanclass="anchor"id="line-120"></span><pclass="line867">
<spanclass="anchor"id="line-121"></span><spanclass="anchor"id="line-122"></span><pclass="line874">A for command, or for loop, creates a loop that executes a set number of times based on a condition <spanclass="anchor"id="line-123"></span><spanclass="anchor"id="line-124"></span><pclass="line867"></strong><ttclass="backtick">for ( [starting condition]; [condition]; [iterator] ) {</tt><strong><spanclass="anchor"id="line-125"></span><spanclass="anchor"id="line-126"></span><pclass="line867"></strong><ttclass="backtick">}</tt><strong><spanclass="anchor"id="line-127"></span><spanclass="anchor"id="line-128"></span><pclass="line874">The starting condition is what happens when the for loop is first reached, it only happens once. The condition is what must be true for the loop to continue. the iterator is what happens at the end of each loop. Check out the example below: <spanclass="anchor"id="line-129"></span><spanclass="anchor"id="line-130"></span><pclass="line867"><spanclass="anchor"id="line-131"></span><spanclass="anchor"id="line-132"></span><spanclass="anchor"id="line-133"></span><spanclass="anchor"id="line-134"></span><spanclass="anchor"id="line-135"></span><spanclass="anchor"id="line-136"></span><spanclass="anchor"id="line-137"></span><spanclass="anchor"id="line-138"></span><spanclass="anchor"id="line-139"></span><spanclass="anchor"id="line-140"></span><spanclass="anchor"id="line-141"></span><spanclass="anchor"id="line-142"></span><spanclass="anchor"id="line-143"></span><spanclass="anchor"id="line-144"></span><spanclass="anchor"id="line-145"></span><spanclass="anchor"id="line-146"></span><spanclass="anchor"id="line-147"></span><divclass="codearea"dir="ltr"lang="en">
<spanclass="line"><spanclass="LineNumber"> 13 </span><spanclass="Comment">//the loop ends here, so the iterator will execute now. In our case,</span></span>
<spanclass="line"><spanclass="LineNumber"> 14 </span><spanclass="Comment">//doorCounter will be incremented by 1.</span></span>
</strong></pre></div><spanclass="anchor"id="line-148"></span><spanclass="anchor"id="line-149"></span><pclass="line862">In that example, doors 100 through 105 will open. When <ttclass="backtick">doorCounter</tt> becomes 106, the loop will end because the condition has turned false. <spanclass="anchor"id="line-150"></span><spanclass="anchor"id="line-151"></span><pclass="line867">
<spanclass="anchor"id="line-152"></span><spanclass="anchor"id="line-153"></span><pclass="line862">The <em>if</em> statement creates the opportunity for decision making. <spanclass="anchor"id="line-154"></span><spanclass="anchor"id="line-155"></span><pclass="line867"><ttclass="backtick">if ( [condition] ) {</tt><spanclass="anchor"id="line-156"></span><ul><listyle="list-style-type:none"><pclass="line891"><ttclass="backtick">[executed if condition is true]</tt><spanclass="anchor"id="line-157"></span></li></ul><pclass="line867"><ttclass="backtick">} else {</tt><spanclass="anchor"id="line-158"></span><ul><listyle="list-style-type:none"><pclass="line891"><ttclass="backtick">[executed if condition is false]</tt><spanclass="anchor"id="line-159"></span></li></ul><pclass="line867"><ttclass="backtick">}</tt><spanclass="anchor"id="line-160"></span><spanclass="anchor"id="line-161"></span><pclass="line874">Here is an example. <spanclass="anchor"id="line-162"></span><spanclass="anchor"id="line-163"></span><pclass="line867"><spanclass="anchor"id="line-164"></span><spanclass="anchor"id="line-165"></span><spanclass="anchor"id="line-166"></span><spanclass="anchor"id="line-167"></span><spanclass="anchor"id="line-168"></span><spanclass="anchor"id="line-169"></span><spanclass="anchor"id="line-170"></span><spanclass="anchor"id="line-171"></span><spanclass="anchor"id="line-172"></span><spanclass="anchor"id="line-173"></span><spanclass="anchor"id="line-174"></span><spanclass="anchor"id="line-175"></span><divclass="codearea"dir="ltr"lang="en">
</pre></div><spanclass="anchor"id="line-176"></span><spanclass="anchor"id="line-177"></span><pclass="line862">Note that we use the <ttclass="backtick">==</tt> comparison operator here, and not the <ttclass="backtick">=</tt> assignment operator. <spanclass="anchor"id="line-178"></span><spanclass="anchor"id="line-179"></span><pclass="line867">
<spanclass="anchor"id="line-180"></span><spanclass="anchor"id="line-181"></span><pclass="line862">The <em>while</em> is another loop, similar to the <em>for</em> loop but a bit simpler. <spanclass="anchor"id="line-182"></span><spanclass="anchor"id="line-183"></span><pclass="line867"></strong><ttclass="backtick">while( [condition] ) {</tt><strong><spanclass="anchor"id="line-184"></span><br/>
<spanclass="anchor"id="line-185"></span><br/>
<spanclass="anchor"id="line-186"></span></strong><ttclass="backtick">}</tt><strong><spanclass="anchor"id="line-187"></span><spanclass="anchor"id="line-188"></span><pclass="line862">While the <ttclass="backtick">[condition]</tt> is true, the loop will continue. <spanclass="anchor"id="line-189"></span><spanclass="anchor"id="line-190"></span><pclass="line867"><spanclass="anchor"id="line-191"></span><spanclass="anchor"id="line-192"></span><spanclass="anchor"id="line-193"></span><spanclass="anchor"id="line-194"></span><spanclass="anchor"id="line-195"></span><spanclass="anchor"id="line-196"></span><spanclass="anchor"id="line-197"></span><spanclass="anchor"id="line-198"></span><divclass="codearea"dir="ltr"lang="en">
<li><ahref="http://moinmoin.wikiwikiweb.de/">MoinMoin Powered</a></li><li><ahref="http://www.python.org/">Python Powered</a></li><li><ahref="http://validator.w3.org/check?uri=referer">Valid HTML 4.01</a></li>