<li><aclass="backlink"title="Click to do a full-text search for this title"href="./ScriptVariable?action=fullsearch&value=linkto%3A%22ScriptVariable%22&context=180">ScriptVariable</a></li>
<h1id="head-d1a238cab8b237ce9d4480b8429746b2f4a84325">Variables in Scripting</h1>
<spanclass="anchor"id="line-2"></span><spanclass="anchor"id="line-3"></span><pclass="line874">In your scripts, you will often want to store and modify data. Changing enemy health, adjusting rotation and movement values for objects, and displaying new information to players all involves the use of data. To do this, we will use variables. <spanclass="anchor"id="line-4"></span><spanclass="anchor"id="line-5"></span><pclass="line874">A variable is a method of storing information. A little piece of memory you can fill with whatever you like. Here's an example: <spanclass="anchor"id="line-6"></span><spanclass="anchor"id="line-7"></span><pclass="line867"><spanclass="anchor"id="line-8"></span><spanclass="anchor"id="line-9"></span><spanclass="anchor"id="line-10"></span><spanclass="anchor"id="line-11"></span><spanclass="anchor"id="line-12"></span><spanclass="anchor"id="line-13"></span><spanclass="anchor"id="line-14"></span><spanclass="anchor"id="line-15"></span><spanclass="anchor"id="line-16"></span><spanclass="anchor"id="line-17"></span><spanclass="anchor"id="line-18"></span><divclass="codearea"dir="ltr"lang="en">
<spanclass="line"><spanclass="LineNumber"> 5 </span><spanclass="Comment">//put some information into it</span></span>
<spanclass="line"><spanclass="LineNumber"> 6 </span><spanclass="ID">messageForYou</span><spanclass="SPChar">=</span><spanclass="String">"Hello my people!"</span><spanclass="SPChar">;</span></span>
</pre></div><spanclass="anchor"id="line-19"></span><spanclass="anchor"id="line-20"></span><pclass="line862">To <em>declare</em> a variable, you have to first enter it's type, followed by it's name. <spanclass="anchor"id="line-21"></span><spanclass="anchor"id="line-22"></span><pclass="line867"><ttclass="backtick">float foo;</tt><spanclass="anchor"id="line-23"></span><spanclass="anchor"id="line-24"></span><pclass="line867"><ttclass="backtick">string cheese;</tt><spanclass="anchor"id="line-25"></span><spanclass="anchor"id="line-26"></span><pclass="line867"><ttclass="backtick">vector stroggMissileDirection;</tt><spanclass="anchor"id="line-27"></span><spanclass="anchor"id="line-28"></span><pclass="line874">The name is unimportant, you can use whatever you like. This will allow you to keep a cohesive naming scheme in your scripts. <spanclass="anchor"id="line-29"></span><spanclass="anchor"id="line-30"></span><pclass="line862">There are four different variable types in script, the <strong>String</strong>, <strong>Float</strong>, <strong>Vector</strong> and <strong>Entity</strong> types. Take a look at them below. <spanclass="anchor"id="line-31"></span><spanclass="anchor"id="line-32"></span><pclass="line867"><hr/><pclass="line874"><spanclass="anchor"id="line-33"></span><spanclass="anchor"id="line-34"></span><spanclass="anchor"id="line-35"></span><pclass="line867"><spanclass="anchor"id="string"></span><spanclass="anchor"id="line-36"></span><spanclass="anchor"id="line-37"></span><pclass="line867">
<spanclass="anchor"id="line-38"></span><spanclass="anchor"id="line-39"></span><pclass="line867"><strong>Usage:</strong> Strings are used to hold lines of text. The word "string" comes from the concept of a string of single characters <spanclass="anchor"id="line-40"></span><spanclass="anchor"id="line-41"></span><pre> string dudeName;
<spanclass="anchor"id="line-51"></span><pclass="line867"><strong>Usage:</strong> Floats are used to hold numbers. The word "float" is derived from the concept of the floating point number. Floats can hold decimal numbers as well as whole numbers. <spanclass="anchor"id="line-52"></span><spanclass="anchor"id="line-53"></span><pre>
<spanclass="anchor"id="line-60"></span></pre><spanclass="anchor"id="line-61"></span><spanclass="anchor"id="line-62"></span><ul><li>In cases where you are creating a string, you can use a float in the concatenation and it is converted automatically. For example, <spanclass="anchor"id="line-63"></span><spanclass="anchor"id="line-64"></span></li></ul><pclass="line867"><spanclass="anchor"id="line-65"></span><spanclass="anchor"id="line-66"></span><spanclass="anchor"id="line-67"></span><spanclass="anchor"id="line-68"></span><spanclass="anchor"id="line-69"></span><spanclass="anchor"id="line-70"></span><spanclass="anchor"id="line-71"></span><spanclass="anchor"id="line-72"></span><spanclass="anchor"id="line-73"></span><spanclass="anchor"id="line-74"></span><spanclass="anchor"id="line-75"></span><spanclass="anchor"id="line-76"></span><divclass="codearea"dir="ltr"lang="en">
<spanclass="line"><spanclass="LineNumber"> 7 </span><spanclass="Comment">// here the number is converted to a string for you automatically.</span></span>
<spanclass="line"><spanclass="LineNumber"> 10 </span><spanclass="Comment">// so, if the number was 67, monsterName would be "uncrushable_boss_67"</span></span>
</pre></div><spanclass="anchor"id="line-77"></span><spanclass="anchor"id="line-78"></span><ul><li>However, conversions for comparison do not happen automatically. <spanclass="anchor"id="line-79"></span><spanclass="anchor"id="line-80"></span></li></ul><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><divclass="codearea"dir="ltr"lang="en">
<spanclass="anchor"id="line-91"></span><pclass="line867"><strong>Usage:</strong> A vector is a collection of three floats, organized into an x, y, and z format. They are primarily used for storing locations, or determining directions. <spanclass="anchor"id="line-92"></span><spanclass="anchor"id="line-93"></span><pclass="line867"><imgalt="/!\"height="15"src="wiki/modern/img/alert.png"title="/!\"width="15"/> Access to a vector's three values is gained by using the _x, _y or _z operator. <strong>Not the . dot operator.</strong><spanclass="anchor"id="line-94"></span><spanclass="anchor"id="line-95"></span><pclass="line867"><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">
</pre></div><spanclass="anchor"id="line-105"></span><spanclass="anchor"id="line-106"></span><ul><li>Vectors can't be set to equal float values without specifying one of their members. <spanclass="anchor"id="line-107"></span></li><li>Vectors will not auto convert to strings when used in concatenations, you will receive an error. <spanclass="anchor"id="line-108"></span><spanclass="anchor"id="line-109"></span></li></ul><pclass="line867"><spanclass="anchor"id="line-110"></span><spanclass="anchor"id="line-111"></span><spanclass="anchor"id="line-112"></span><spanclass="anchor"id="line-113"></span><spanclass="anchor"id="line-114"></span><spanclass="anchor"id="line-115"></span><spanclass="anchor"id="line-116"></span><spanclass="anchor"id="line-117"></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">"I am at location "</span><spanclass="SPChar">+</span><spanclass="ID">vFail</span><spanclass="SPChar">)</span><spanclass="SPChar">;</span></span>
<spanclass="anchor"id="line-121"></span><pclass="line867"><strong>Usage:</strong> Entity variables are used to store handles to entities. When an entity is spawned via script, or referenced via entity name, you gain access to his handle. <spanclass="anchor"id="line-122"></span><spanclass="anchor"id="line-123"></span><pclass="line867"><spanclass="anchor"id="line-124"></span><spanclass="anchor"id="line-125"></span><spanclass="anchor"id="line-126"></span><spanclass="anchor"id="line-127"></span><spanclass="anchor"id="line-128"></span><spanclass="anchor"id="line-129"></span><spanclass="anchor"id="line-130"></span><spanclass="anchor"id="line-131"></span><divclass="codearea"dir="ltr"lang="en">
</pre></div><spanclass="anchor"id="line-132"></span><spanclass="anchor"id="line-133"></span><pclass="line867"><imgalt="/!\"height="15"src="wiki/modern/img/alert.png"title="/!\"width="15"/><strong><ahref="./Tutorial_ScriptEntities.html">Check here for a more detailed example.</a></strong><spanclass="anchor"id="line-134"></span><spanclass="anchor"id="line-135"></span><ul><li><pclass="line862">Entities do not convert into strings. You can use <ahref="./ScriptEvent_getName.html">getName</a> to get a string version of the entity name. <spanclass="anchor"id="line-136"></span></li></ul><spanclass="anchor"id="bottom"></span></div><pid="pageinfo"class="info"lang="en"dir="ltr">ScriptVariable (last edited 2005-11-02 23:23:04 by <spantitle="JimShepard @ 67.129.250.254[67.129.250.254]"><aclass="nonexistent"href="./JimShepard.html"title="JimShepard @ 67.129.250.254[67.129.250.254]">JimShepard</a></span>)</p>
<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>