Difference between revisions of "Scripting Tutorial: My Second Script"

m
imported>Grundulum
imported>8asrun6aer
 
(31 intermediate revisions by 12 users not shown)
Line 1: Line 1:
{{Featured}}
While the [[My First Script]] tutorial is a good first taste of scripting for Oblivion, it does not appreciably demonstrate what the scripting language in Oblivion can do.  It is a wonderful introduction for those who have never seen a programming or scripting language before, but a more in-depth tutorial would be helpful to further introduce modders to this amazing resource.
While the [[My First Script]] tutorial is a good first taste of scripting for Oblivion, it does not appreciably demonstrate what the scripting language in Oblivion can do.  It is a wonderful introduction for those who have never seen a programming or scripting language before, but a more in-depth tutorial would be helpful to further introduce modders to this amazing resource.


This tutorial has been largely adapted from GhanBuriGhan's excellent ''Morrowind Scripting for Dummies''; all credit goes to GhanGuriGhan for his fantastic work on the original.
This tutorial has been largely adapted from GhanBuriGhan's excellent ''Morrowind Scripting for Dummies''; all credit goes to GhanBuriGhan for his fantastic work on the original.


This tutorial is meant to be a more complete introduction to scripting for Oblivion than the My First Script tutorial, and assumes that the reader is already familiar with My First Script.  If you do not understand the main points of that tutorial, you may find yourself in over your head here.  If you're comfortable with My First Script, though, let's begin scripting!
This tutorial is meant to be a more complete introduction to scripting for Oblivion than the My First Script tutorial, and assumes that the reader is already familiar with My First Script.  If you do not understand the main points of that tutorial, you may find yourself in over your head here.  If you're comfortable with My First Script, though, let's begin scripting!
{{Tools|req0=[[The Elder Scrolls Construction Set|Construction Set]]|opt0=[[OBSE]]}}


==More information about scripting in Oblivion==
==More information about scripting in Oblivion==
Line 32: Line 35:
You enter the script editor either by selecting Gameplay –> Edit Scripts from the menu; by clicking the edit script button (the pencil) at the far right of the taskbar; or by accessing it from an Object or NPC dialogue, clicking the button with the ellipses […] next to the script field. The editor window is pretty basic, and doesn't look like much right now:
You enter the script editor either by selecting Gameplay –> Edit Scripts from the menu; by clicking the edit script button (the pencil) at the far right of the taskbar; or by accessing it from an Object or NPC dialogue, clicking the button with the ellipses […] next to the script field. The editor window is pretty basic, and doesn't look like much right now:


[[Image:1_script_window.JPG|thumb|left|The scripting window]]Let's have a look at the buttons in the taskbar, from left to right: ''Open'' lets you select a script to edit. ''Save'' error checks the current script and either compiles it or gives out error messages. Note, however, that the plugin and thus the script is not really saved to disk at this time. When programming large scripts you should frequently use the save command in the main TESCS window after you have saved the script here, just in case the TESCS crashes. Note also that if you edit the script and suddenly hit "save plugin" to backup in the middle of the work, your updated script will NOT be saved with it. You must save it manually first. Also, if you simply close the script window, it doesn’t mean that script will be saved. You must take care of it yourself.  
[[Image:1_script_window.JPG|thumb|right|The scripting window]]Let's have a look at the buttons in the taskbar, from left to right:
<ul><li>''Open'' lets you select a script to edit.</li>
<li>''Save'' error checks the current script and either compiles it or gives out error messages. Note, however, that the plugin and thus the script is not really saved to disk at this time. When programming large scripts you should frequently use the save command in the main TESCS window after you have saved the script here, just in case the TESCS crashes. Note also that if you edit the script and suddenly hit "save plugin" to backup in the middle of the work, your updated script will NOT be saved with it. You must save it manually first. Also, if you simply close the script window, it doesn’t mean that script will be saved. You must take care of it yourself.  


''Forward'' and ''Backward'' arrows jump to the next or previous script, respectively (in alphabetical order). If you give your scripts a common tag, that will make it easier to jump between the different scripts of your project. For example, my pseudonym is Grundulum, and he starts every script name with "GR_ShortReferencetoProject_", with that second part being a two or three letter reference to the current mod; this keeps all of the scripts you're working on neatly together. ''Compile all'' recompiles all scripts (what's this good for? Ghanburighan didn't know, and neither do I). Finally, the ''Delete'' button deletes a script,
<blockquote>Side Note: You can also write your code in external editors and paste it into the CS. This can make it easier to keep scripts saved, and can be more convenient, since you don't have to load the CS and your mod. Options include [[S!lk]], emacs, and [[Notepad++ Script Definitions|Notepad++]], all of which have syntax highlighting plugins for Oblivion script (only Notepad++ has up-to-date OBSE functions included, however).</blockquote></li>
and the last ''Arrow down'' button closes the script window.
<li>''Forward'' and ''Backward'' arrows jump to the next or previous script, respectively (in alphabetical order). If you give your scripts a common tag, that will make it easier to jump between the different scripts of your project. For example, say a modder's pseudonym is Grundulum, and he starts every script name with "GR_ShortReferencetoProject_", with that second part being a two or three letter reference to the current mod; this keeps all of the scripts you're working on neatly together.</li>
<li>''Compile all'' recompiles all scripts in all loaded files. '''Do not ever press this button.''' It also adds every script in Oblivion to your mod, so you'll wind up with a 2MB esp file that conflicts with '''''everything'''''.</li>
<li>Finally, the ''Delete'' button deletes a script, and</li>
<li>the last ''Arrow down'' button closes the script window.</li></ul>


At the far right of your toolbar, you'll see a dropdown box called ''Script Type''.  This box allows you to choose one of three kinds that your script will fall under: Object, Quest, and Magic Effect.  More on these later, but by way of brief introduction Object scripts are attached to objects in the game world (such as items or NPCs), Quest scripts control the flow of quests (such as character generation), and Magic Effect scripts control a very special magic effect (specifically, the Script Effect).
At the far right of your toolbar, you'll see a dropdown box called ''Script Type''.  This box allows you to choose one of three kinds that your script will fall under: Object, Quest, and Magic Effect.  More on these later, but by way of brief introduction Object scripts are attached to objects in the game world (such as items or NPCs), Quest scripts control the flow of quests (such as character generation), and Magic Effect scripts control a very special magic effect (specifically, the Script Effect).
Line 42: Line 50:


===What do we want?===
===What do we want?===
Before we really start writing our tutorial script we should decide what we want it to do! For this tutorial we are going to make a '''Riddle Chest: The chest will ask a riddle and only the right answer will open the chest. If the player provides the wrong answer, a trap will go off, hurting the player, and the chest can't be opened.''' That’s a fairly complex undertaking, but we will take it step by step and see that it's not so bad after all.
Before we really start writing our tutorial script we should decide what we want it to do! For this tutorial we are going to make a '''Riddle Cupboard: The cupboard will ask a riddle and only the right answer will open the cupboard. If the player provides the wrong answer, a trap will go off, hurting the player, and the cupboard can't be opened.''' That’s a fairly complex undertaking, but we will take it step by step and see that it's not so bad after all.


===Writing the script===
===Writing the script===
Line 62: Line 70:
   ; Here we will enter what happens when the chest is opened.
   ; Here we will enter what happens when the chest is opened.
End</pre>
End</pre>
'''Click ''Save'' again now.''' The script still doesn't do anything, but at least now we have defined under what conditions it will run.  There are a couple of things to note here.  First, we have ended the current block before beginning a new one—not so important when there's only one block, but as this script gets more complicated we'll need multiple blocks in a single script.  In somewhat more technical terms, '''Begin/End blocks do not nest.'''  The other thing to notice is the semicolon ";" on line 4.  A semicolon marks the rest of the line as ''comment''.  Whatever you type after the semicolon ''on that one line'' will be ignored when the script compiles.  If you want more than one line of comment, you must have one semicolon for each line.
'''Click ''Save'' again now.''' The script still doesn't do anything, but at least now we have defined under what conditions it will run.  There are a couple of things to note here.  First, we have ended the current block before beginning a new one—not so important when there's only one block, but as this script gets more complicated we'll need multiple blocks in a single script.  In somewhat more technical terms, '''Begin/End blocks do not nest.'''  The other thing to notice is the semicolon ";" on line 4.  A semicolon marks the rest of the line as ''comment''.   
 
===Code Comments===
Comments can be placed anywhere in-code with the semicolon character - ''';''' - Whatever you type ''after'' the semicolon on the same line will be ignored when the script compiles.  If you want more than one line of comment, you must have one semicolon for each line.
 
You use comments inside your scripts to generally explain what you are trying to accomplish in your code, which can help you and others understand what's going on inside the script quickly at a later date.  Comments can also serve as a debugging technique by commenting out lines of code one-by-one to pinpoint the source of some odd or unintended effect in-game. 
 
Note: Because everything appearing on a line after the semicolon is not treated as compilable code, one issue you might face is that you cannot simply put semicolons in strings.  If you need to have a semicolon as part of a string - for example a message in-game - you could instead use the [[OBSE]] "%a" Format Specifier for functions that support this and passing in ASCII character code 59, or use the [[AsciiToChar]] OBSE function.


===Writing text and obtaining decisions from the player===
===Writing text and obtaining decisions from the player===
Line 70: Line 85:
The first ''string'' (the text between the first pair of quotation marks) is the text actually displayed in the box; the other texts, separated by commas, tell the game to make "buttons" with the given text displayed one string per choice.
The first ''string'' (the text between the first pair of quotation marks) is the text actually displayed in the box; the other texts, separated by commas, tell the game to make "buttons" with the given text displayed one string per choice.


But how do we ensure that the riddle is asked only the first time we try to open the chest and not every time? We now come to a very central point: the use of do-once conditions and state variables. Most of the problems that beginners encounter with scripting for Oblivion have their roots in misunderstanding how the scripts are actually executed and how scripts should accordingly be structured. So let's have a look at this in greater detail.
But how do we ensure that the riddle is asked only the first time we try to open the chest and not every time? We now come to a very central point: the use of do-once conditions and state variables. Most of the problems that beginners encounter with scripting for Oblivion have their roots in misunderstanding how the scripts are actually executed and how scripts should accordingly be structured. So let's have a look at this in greater detail. Remember to save regularly.


===How Object scripts are executed===
===How Object scripts are executed===
Line 107: Line 122:
===Saving and preparing the mod===
===Saving and preparing the mod===
Now our script is already capable of being run, so lets test it:
Now our script is already capable of being run, so lets test it:
* '''Save the script and close the script editor window.'''
* Save the script and close the script editor window.
* '''Go to the Object Window, click on the + sign beside WorldObjects, click on the + sign beside Containers, and the + sign next to Clutter. Now find CupboardFoodLower from that list and double-click on it to bring it up for editing.'''
* Look to the Object Window, and proceed in order through the directories of ''WorldObjects > Container > Clutter'' until you find the object with the Editor ID of CupboardFoodLower.
* '''In the script dropdown field, select RiddleChestScript. Hit OK, save the mod, and quit TESCS. NOTE: WHAT WE HAVE JUST DONE, EDITING AN ITEM WITHOUT RENAMING IT, IS TERRIBLE MODDING PRACTICE.  NEVER DO THIS UNLESS YOU ARE ''SURE'' YOU KNOW WHAT YOU ARE DOING!'''
* Now either right-click and choose Edit or double-click the object to bring up its properties.
* In the script dropdown menu, select the script you just made RiddleChestScript.
* Hit OK, save the mod, and quit TESCS.


'''NOTE: PAY ATTENTION TO WHAT WE HAVE JUST DONE. EDITING AN OBJECT WITHOUT GIVING IT A NEW ''ID'' IS TERRIBLE MODDING PRACTICE.  NEVER DO THIS UNLESS YOU ARE ''SURE'' YOU KNOW WHAT YOU ARE DOING!'''


===The script in-game===
===The script in-game===
Line 258: Line 276:


==How to learn more==
==How to learn more==
After this tutorial you may ask yourself how to continue learning how to script. A good way is to look at the example scripts in this guide or at scripts that are already in the game (either form Bethesda or from mods). Try to find a script that is similar to what you want to do and then copy the script and change it to fit your needs. Read the general information on the functions page and the descriptions of the functions you may need to do what you have planned. The categorization of the functions into function types should help you to find the right ones. Finally, the official forums are a great place to find information (use the search function) or to get help on a specific problem. The rest is practicing, practicing, practicing.
After this tutorial you may ask yourself how to continue learning how to script. A good way is to look at the example scripts in this guide or at scripts that are already in the game (either from Bethesda or from mods). Try to find a script that is similar to what you want to do and then copy the script and change it to fit your needs. Read the general information on the functions page and the descriptions of the functions you may need to do what you have planned. The categorization of the functions into function types should help you to find the right ones. Finally, the official forums are a great place to find information (use the search function) or to get help on a specific problem. The rest is practicing, practicing, practicing.


==Last notes==
==Last notes==
Line 266: Line 284:


[[Category:Tutorials]]
[[Category:Tutorials]]
[[Category:Scripting]]
[[Category:Scripting Tutorials]]
Anonymous user