<li><aclass="backlink"title="Click to do a full-text search for this title"href="./BasicScriptTutorial?action=fullsearch&value=linkto%3A%22BasicScriptTutorial%22&context=180">BasicScriptTutorial</a></li>
<spanclass="anchor"id="line-1"></span><pclass="line867"><imgalt="/!\"height="15"src="wiki/modern/img/alert.png"title="/!\"width="15"/><strong>This page still under development</strong><spanclass="anchor"id="line-2"></span><spanclass="anchor"id="line-3"></span><pclass="line874">The Quake 4 Script system is a powerful method of creating exciting content for your mod. Much like the special entities you can place in a map to create different effects, the script system contains all sorts of tools to control nearly every aspect of all game entities. <spanclass="anchor"id="line-4"></span><spanclass="anchor"id="line-5"></span><pclass="line874">From gripping cinematics to run and gun boss battles, the script system allows you to add spice and flavor to the standard behavior of Quake 4’s entities. Used in conjunction with map entities and game code, you can create all new game types! <spanclass="anchor"id="line-6"></span><spanclass="anchor"id="line-7"></span><pclass="line874">Many pages could be dedicated to the similarities and contrasts between using script to create gameplay and using map entities, but we won’t do that here. Use this tutorial to unearth the power of scripting, then draw your own conclusions. <spanclass="anchor"id="line-8"></span><spanclass="anchor"id="line-9"></span><pclass="line867">
<spanclass="anchor"id="line-10"></span><spanclass="anchor"id="line-11"></span><pclass="line874">You will need a licensed copy of Quake 4 and the SDK. You will be using both the map editor, and a script editing tool of your choice. <spanclass="anchor"id="line-12"></span><spanclass="anchor"id="line-13"></span><pclass="line874">Script editing can be done in any text editing program. However, since the script system closely resembles C style code, an editor that uses syntax highlighting would be best. Our developers here used both Visual Studio and Ultra Edit 32, but in a pinch you can use notepad.exe. <spanclass="anchor"id="line-14"></span><spanclass="anchor"id="line-15"></span><pclass="line867"><spanclass="anchor"id="savemap"></span><spanclass="anchor"id="line-16"></span>
<h1id="head-5337f61f6ae189ba2205c9d73ef4b7cb773d1169">Setting up your first script file</h1>
<spanclass="anchor"id="line-17"></span><spanclass="anchor"id="line-18"></span><pclass="line874">Script files can be set up two ways. One method is for quick practice and “scratchpad” style development, the other is for distribution. To get started, we’ll use the first method because it’s much simpler. Create a new text file in the directory that has the .map file you want to use the script on. For example, if you’re working out of C:\Program Files\Quake4\q4base\maps\, you’d place the file there. <spanclass="anchor"id="line-19"></span><spanclass="anchor"id="line-20"></span><pclass="line867"><imgclass="attachment"src="basicscripttutorial1.png"title="attachment:basicscripttutorial1.png"/><spanclass="anchor"id="line-21"></span><spanclass="anchor"id="line-22"></span><pclass="line874">Name the file [mapname].script. So if your map is called “awesome.map” you’d name the file “awesome.script” <spanclass="anchor"id="line-23"></span><spanclass="anchor"id="line-24"></span><pclass="line867"><imgclass="attachment"src="basicscripttutorial2.png"title="attachment:basicscripttutorial2.png"/><spanclass="anchor"id="line-25"></span><spanclass="anchor"id="line-26"></span><pclass="line867">
<h1id="head-570f051b69d313da23334cefad6e29530ae2ae75">Your first script function, “Hello Stroggos”</h1>
<spanclass="anchor"id="line-27"></span><spanclass="anchor"id="line-28"></span><pclass="line874">Open up the script file you just created with your editor. Enter the following into the file and save the file. <spanclass="anchor"id="line-29"></span><spanclass="anchor"id="line-30"></span><pclass="line867"><spanclass="anchor"id="line-31"></span><spanclass="anchor"id="line-32"></span><spanclass="anchor"id="line-33"></span><spanclass="anchor"id="line-34"></span><spanclass="anchor"id="line-35"></span><spanclass="anchor"id="line-36"></span><spanclass="anchor"id="line-37"></span><divclass="codearea"dir="ltr"lang="en">
</pre></div><spanclass="anchor"id="line-38"></span><pclass="line874">If you’re not familiar with C-style programming, that all looks like a lot of jive. If you are familiar, go ahead and skip to the next header because this is probably old hat to you. <spanclass="anchor"id="line-39"></span><spanclass="anchor"id="line-40"></span><pclass="line874">What you just wrote is called a function. Functions are called from the map by triggers—something we’ll get into later. Functions can also be called by other functions, but that’s getting way ahead. Let’s break it down. <spanclass="anchor"id="line-41"></span><spanclass="anchor"id="line-42"></span><pclass="line862">The <strong>void</strong> is the <em>return type</em>, this is what a function gives back to you when it’s done. This function doesn’t give anything back, so we use the word “void” to represent that. <spanclass="anchor"id="line-43"></span><spanclass="anchor"id="line-44"></span><pclass="line862">The next part is the <em>function name</em>. The <strong>()</strong> at the end are where 'parameters' go. If we need to send data to a function, we do it with parameters. Right now, we don’t need any, so the parentheses are empty. <spanclass="anchor"id="line-45"></span><spanclass="anchor"id="line-46"></span><pclass="line862">You’ll notice the entire function is wrapped in squirrely braces, these right here: <strong>{ }</strong> This is a must. If you forget them, even one of them, your stuff won’t work. <spanclass="anchor"id="line-47"></span><spanclass="anchor"id="line-48"></span><pclass="line862">The next line, the one with the <strong>//</strong> slashes, is a <em>comment</em>. Comments are just for us, the compiler never sees them. You can write whatever you’d like in a comment. They are not mandatory, but good comments help you remember just what you were doing in a function. <spanclass="anchor"id="line-49"></span><spanclass="anchor"id="line-50"></span><pclass="line862">Finally we get to the meat. This line calls a function from the <ahref="./SystemObject.html">SystemObject</a>, called "<ahref="./SystemObject.html">sys</a>.” <ahref="./SystemObject.html">Sys</a> represents the game system, and can do quite a lot for us, which you’ll see later. Right now, <ahref="./SystemObject.html">sys</a> is using the Print Line function, <ahref="./ScriptFunction_println.html">println</a>. <ahref="./ScriptFunction_println.html">println</a> takes in one parameter, which is a line of text that you wanted printed to the console. It’s very simple. <spanclass="anchor"id="line-51"></span><spanclass="anchor"id="line-52"></span><pclass="line874">Notice that the line ends with a semicolon! Semicolons are the only way the compiler knows to stop reading commands from that line. You must include them at the end of your lines, or your stuff won’t run. Especially during these early steps of learning scripting, nine times out of ten when your scripts don’t compile, you forgot a semicolon. <spanclass="anchor"id="line-53"></span><spanclass="anchor"id="line-54"></span><pclass="line867">
<spanclass="anchor"id="line-55"></span><spanclass="anchor"id="line-56"></span><pclass="line862">At this point, you should save and close the script file. Now it’s time to open up the editor. Open up an empty map suitable for gameplay, a room with a light and an <aclass="nonexistent"href="./InfoPlayerStart.html">InfoPlayerStart</a>. Create a new brush, right click it and change it to a trigger_once. <spanclass="anchor"id="line-57"></span><spanclass="anchor"id="line-58"></span><pclass="line867"><imgclass="attachment"src="basicscripttutorial3.png"title="attachment:basicscripttutorial3.png"/><spanclass="anchor"id="line-59"></span><spanclass="anchor"id="line-60"></span><pclass="line874">A trigger is an invisible brush that is activated by certain conditions. Usually this boils down to the player touching it and something happening. In our case, we want the trigger to call a function for us. So let’s set that key value on the entity. <spanclass="anchor"id="line-61"></span><spanclass="anchor"id="line-62"></span><pclass="line867"><imgclass="attachment"src="basicscripttutorial4.png"title="attachment:basicscripttutorial4.png"/><spanclass="anchor"id="line-63"></span><spanclass="anchor"id="line-64"></span><pclass="line874">Very straightforward. Notice here we don’t have to include the void or parentheses from the script file. Just the name of the function will do for now. <spanclass="anchor"id="line-65"></span><spanclass="anchor"id="line-66"></span><pclass="line874">That should do it! Save and BSP your map, then run that level in game. If all went well, you should see “Hello Stroggos!” on screen. If you aren’t running in developer mode, or if con_showPrint is set to 0, you’ll have to open the console to see the message. <spanclass="anchor"id="line-67"></span><spanclass="anchor"id="line-68"></span><pclass="line867"><ahref="#compileerror">Did it not work for you?</a><spanclass="anchor"id="line-69"></span><spanclass="anchor"id="line-70"></span><pclass="line867">
<spanclass="anchor"id="line-71"></span><spanclass="anchor"id="line-72"></span><pclass="line874">Now that you've got your script running, let's work a little further. We will modify our script and our map to change things up and spawn in a monster. <spanclass="anchor"id="line-73"></span><spanclass="anchor"id="line-74"></span><pclass="line862">First, open the editor and add a <ahref="./Entity_TargetNull.html">target_null</a> to the map. Place the <ahref="./Entity_TargetNull.html">target_null</a> on the map in a place where you'd like the monster to spawn. Remember that monster origins are at the centered at the feet of the monster so place the origin <ahref="./Entity_TargetNull.html">target_null</a> even with the ground unless you want the monster to fall out of the sky. <spanclass="anchor"id="line-75"></span><spanclass="anchor"id="line-76"></span><pclass="line867"><imgclass="attachment"src="basicscripttutorial5.png"title="attachment:basicscripttutorial5.png"/><imgclass="attachment"src="basicscripttutorial6.png"title="attachment:basicscripttutorial6.png"/><spanclass="anchor"id="line-77"></span><spanclass="anchor"id="line-78"></span><pclass="line862">Give that <ahref="./Entity_TargetNull.html">target_null</a> a name you'll remember. For our example we'll use targetMonster. <spanclass="anchor"id="line-79"></span><spanclass="anchor"id="line-80"></span><pclass="line874">Go back to your script and add a few lines to the function we wrote earlier. <spanclass="anchor"id="line-81"></span><spanclass="anchor"id="line-82"></span><pclass="line867"><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><divclass="codearea"dir="ltr"lang="en">
</pre></div><spanclass="anchor"id="line-100"></span><spanclass="anchor"id="line-101"></span><pclass="line874">Let's go over this new stuff! <spanclass="anchor"id="line-102"></span><spanclass="anchor"id="line-103"></span><pclass="line867"><strong><ttclass="backtick">entity newMonster</tt></strong> creates a <em><ahref="./ScriptVariable.html">variable</a></em> for us of the <em>entity</em> type. A <ahref="./ScriptVariable.html">variable</a> is a just a label for a piece of memory. When we spawn in our new creature, we want to have a way to access him later in the script. We use <ahref="./ScriptVariable.html">variables</a> to keep track of all sorts of information. <spanclass="anchor"id="line-104"></span><spanclass="anchor"id="line-105"></span><pclass="line874">Right now newMonster is empty. We're going to spawn in a strogg marine, and store the resulting handle into the newMonster variable. <spanclass="anchor"id="line-106"></span><spanclass="anchor"id="line-107"></span><pclass="line867"><strong><ttclass="backtick">newMonster = sys.spawn("monster_strogg_marine");</tt></strong> does just that! We use the <ahref="./ScriptEvent_spawn.html">spawn</a> command to tell the game to create a new entity of whatever type we tell it. Anytime we enter text between quotes, we call it a <ahref="./ScriptVariable.html#string">string</a>. That string is the parameter-- you remember those from earlier? So we're telling the game to create us a Strogg Marine, and to put the handle to that Strogg Marine inside our variable called newMonster. <spanclass="anchor"id="line-108"></span><spanclass="anchor"id="line-109"></span><pclass="line862">At this point, a Strogg Marine is created, but he's been placed at world location 0,0,0. That's no good, we want him to appear at the <ahref="./Entity_TargetNull.html">target_null</a> we made earlier! <spanclass="anchor"id="line-110"></span><spanclass="anchor"id="line-111"></span><pclass="line862">To move an entity to a different position in the map instantly, we use the <ahref="./ScriptEvent_SetWorldOrigin.html">setWorldOrigin</a> event. This is an instant move, the entity will pop into position over the course of a single frame. We do that with <strong><ttclass="backtick">newMonster.setWorldOrigin( )</tt></strong>. <spanclass="anchor"id="line-112"></span><spanclass="anchor"id="line-113"></span><pclass="line862">Up until now we've been calling all our functions on the <ahref="./SystemObject.html">SystemObject</a>. Functions can be called on all sorts of objects though, the <ahref="./SystemObject.html">SystemObject</a> is only the beginning. To learn more about these functions, check out the <ahref="./ScriptFile_Events.html">Script Events</a> page. <spanclass="anchor"id="line-114"></span><spanclass="anchor"id="line-115"></span><pclass="line862">The function <ahref="./ScriptEvent_SetWorldOrigin.html">setWorldOrigin</a> needs a parameter though. Let's give it the origin of that <ahref="./Entity_TargetNull.html">target_null</a> we made earlier. We can call a function on that <ahref="./Entity_TargetNull.html">target_null</a> to get it's origin. So we will. <spanclass="anchor"id="line-116"></span><spanclass="anchor"id="line-117"></span><pclass="line867"><strong><ttclass="backtick">$targetMonster.getWorldOrigin()</tt></strong> returns to us the entity's origin in the world as a <ahref="./ScriptVariable.html#vector">vector</a>. The <strong><ttclass="backtick">$targetMonster</tt></strong> part is special. We use that $ symbol to reference any entity that is in the map. In this example, we use $targetMonster because that's what we named the <ahref="./Entity_TargetNull.html">target_null</a>. If you named yours differently, use that different name. <spanclass="anchor"id="line-118"></span><spanclass="anchor"id="line-119"></span><pclass="line867"><imgalt="/!\"height="15"src="wiki/modern/img/alert.png"title="/!\"width="15"/> This part is important, and a bit confusing at first. <ahref="./Tutorial_ScriptEntities.html">Understand
<spanclass="anchor"id="line-131"></span><spanclass="anchor"id="line-132"></span><pclass="line862">Now, if you've closed the game since you've run the Hello Stroggos example, just load it again and this script should work out fine. However, if you've still got the game running from before, you'll need to execute a <ahref="./ConsoleCommand_ReloadScript.html">Reload Script</a> command in the console. <spanclass="anchor"id="line-133"></span><spanclass="anchor"id="line-134"></span><pclass="line862">Once you've saved your script, drop the console in game and enter the command <ahref="./ConsoleCommand_ReloadScript.html">reloadScript</a>. The game should pause for a few seconds, then show you this: <spanclass="anchor"id="line-135"></span><spanclass="anchor"id="line-136"></span><pclass="line867"><spanclass="anchor"id="line-137"></span><pre>******************************
<spanclass="anchor"id="line-140"></span></pre><spanclass="anchor"id="line-141"></span><spanclass="anchor"id="line-142"></span><pclass="line862">This is perfectly normal, and it means your script compiled fine. But if you see any other errors, head to the <ahref="#compileerror">troubleshooting section</a> before moving on. <spanclass="anchor"id="line-143"></span><spanclass="anchor"id="line-144"></span><pclass="line874">At this point, run your map again, and you should be able to walk over the trigger and spawn a Strogg Marine into the map without a problem. <spanclass="anchor"id="line-145"></span><spanclass="anchor"id="line-146"></span><pclass="line867"><imgclass="attachment"src="basicscripttutorial7.png"title="attachment:basicscripttutorial7.png"/><spanclass="anchor"id="line-147"></span><spanclass="anchor"id="line-148"></span><pclass="line867">
<spanclass="anchor"id="line-149"></span><spanclass="anchor"id="line-150"></span><pclass="line862">From here, you could move on to the <ahref="./AdvancedScriptTutorial.html">AdvancedScriptTutorial</a>, which will show you a little more about the techniques available to you in script. You could also take a deeper look at the <ahref="./ScriptVariable.html">ScriptVariable</a> page to learn more about variables and how they are used in scripting. <spanclass="anchor"id="line-151"></span><spanclass="anchor"id="line-152"></span><pclass="line874">If something here didn't work out for you, check out the troubleshooting section below. <spanclass="anchor"id="line-153"></span><spanclass="anchor"id="line-154"></span><pclass="line867"><ahref="./ScriptFile.html">Back to the Script Shop</a><spanclass="anchor"id="line-155"></span><spanclass="anchor"id="line-156"></span><spanclass="anchor"id="line-157"></span><pclass="line867">
<spanclass="anchor"id="line-158"></span><spanclass="anchor"id="line-159"></span><pclass="line867"><spanclass="anchor"id="compileerror"></span><spanclass="anchor"id="line-160"></span><spanclass="anchor"id="line-161"></span><ul><li><pclass="line891"><strong>Did you forget a semicolon?</strong><spanclass="anchor"id="line-162"></span><ul><listyle="list-style-type:none">Remember that most command lines in script need to end with a semicolon. Check the examples to make sure your script follows exactly. <spanclass="anchor"id="line-163"></span><spanclass="anchor"id="line-164"></span></li></ul></li><liclass="gap"><pclass="line891"><strong>Did you forget other punctuation?</strong><spanclass="anchor"id="line-165"></span><ul><listyle="list-style-type:none"><pclass="line862">Remember that the function needs to be enclosed entirely in <strong>{}</strong> curly braces. Did you forget one? <spanclass="anchor"id="line-166"></span>Did you miss a parentheses in those functions in the second example? <spanclass="anchor"id="line-167"></span><spanclass="anchor"id="line-168"></span></li></ul></li><liclass="gap"><pclass="line891"><strong>Did you save your script file in the right place?</strong><spanclass="anchor"id="line-169"></span><ul><listyle="list-style-type:none"><pclass="line862">Check <ahref="#savemap">here</a> to make sure you've saved your script where it belongs-- in the same folder as the map you're working on, with the same name as the map. <spanclass="anchor"id="line-170"></span><spanclass="anchor"id="line-171"></span></li></ul></li><liclass="gap"><pclass="line891"><strong>Did you call Reload Script from the console if necessary?</strong><spanclass="anchor"id="line-172"></span><ul><listyle="list-style-type:none"><pclass="line862">Remember that if you changed the script file while the game is running, you need to reload them. Check out the example <ahref="#reloadscript">here</a>. <spanclass="anchor"id="line-173"></span><spanclass="anchor"id="line-174"></span></li></ul></li><liclass="gap"><pclass="line891"><strong>First Example: Hello Stroggos</strong><spanclass="anchor"id="line-175"></span></li></ul><pclass="line867"><spanclass="anchor"id="line-176"></span><spanclass="anchor"id="line-177"></span><spanclass="anchor"id="line-178"></span><spanclass="anchor"id="line-179"></span><spanclass="anchor"id="line-180"></span><spanclass="anchor"id="line-181"></span><divclass="codearea"dir="ltr"lang="en">
</pre></div><spanclass="anchor"id="line-182"></span><spanclass="anchor"id="line-183"></span><ul><li><pclass="line891"><strong>Second Example: Expanding to spawn a monster</strong><spanclass="anchor"id="line-184"></span></li></ul><pclass="line867"><spanclass="anchor"id="line-185"></span><spanclass="anchor"id="line-186"></span><spanclass="anchor"id="line-187"></span><spanclass="anchor"id="line-188"></span><spanclass="anchor"id="line-189"></span><spanclass="anchor"id="line-190"></span><spanclass="anchor"id="line-191"></span><spanclass="anchor"id="line-192"></span><spanclass="anchor"id="line-193"></span><spanclass="anchor"id="line-194"></span><divclass="codearea"dir="ltr"lang="en">
</pre></div><spanclass="anchor"id="line-195"></span><spanclass="anchor"id="line-196"></span><spanclass="anchor"id="line-197"></span><pclass="line867"><ahref="./ScriptFile.html">Back to the Script Shop</a><spanclass="anchor"id="line-198"></span><spanclass="anchor"id="bottom"></span></div><pid="pageinfo"class="info"lang="en"dir="ltr">BasicScriptTutorial (last edited 2005-11-02 00:14:38 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>