Programming Class #1
This is the first draft of the class. It should get you going with scripts in Skyrim.
Post your questions and I will try to answer them. Feel free to answer others questions if you think you know the answer.
Your first script that does something interesting with the player.
I will be going over a lot of stuff not related directly with scripting in this first lesson. But this lesson will show you how to get references attached to scripts and return button presses.
Cipscis has also made a beginners tutorial that you might find some useful information in.
Link to Cipscis tutorial: http://www.cipscis.com/skyrim/tutorials/beginners.aspx
Lets jump right in and create a script that asks the player a question, and then does something based on the players response.
The scripting as it was called in previous construction sets has advanced to a full programming language using Papyrus. This has caused the scripting to now be done outside the construction kit (CK) so there are extra steps you will need to take to get your 'code' attached to objects in the game.
1. First, lets open the CK and load up 'Skyrim.esm'.
2. After its done loading, which can take 3 or 4 minutes, you will be able to find the object we will attach the script to.
You will need to locate the 'Activators' folder. Using a filter is not necessary but it can help you find things your looking for. We will be making a copy of "ImpButton01"
Simply double-click it and its base properties window will open. Give it a new 'Editor ID' name and click the OK button. When it asks if you want to "Create a new Form", click the Yes button.
Congratulations, you just made your first unique object.
This is the "activator" object properties window. Each object type has its own properties you can set. As you can see, Activators allow scripts to be attached to them.
3. Okay, we now have the object we will place the code on, lets write the code now.
Lets open the Papyrus Script Manager so we can create a new code object.
Select from the toolbar, >Gameplay >Papyrus Script Manager
You may notice you have a 'filter' box here too. You can use it if you like.
Right-click in the script window and select 'New'
You will now need to give your script a name, and what type of script it will be. In our case, we will create an "ObjectReference" script.
You can find out what to put here by referring to this page, since we want to do an 'OnActivate' script, you see its an ObjectReference type of event.
Index page: http://www.creationkit.com/Category:Events
OnActivate page: http://www.creationkit.com/OnActivate_-_ObjectReference
If you like, you can type something into the 'Documentation String' box to help you remember what the script is supposed to do.
Click the OK button to create the code base object.
Smarty Says: Your new code will NOT show up in the box. You need to close the Papyrus Script Manager window and reopen it again to see your new code object.
4. Lets write some code!
Select your script from the list and then double-click it. You may notice that the text you typed into the Documentation String shows up in a balloon window when you hover over your script.
It should open up in Notepad, or if you installed it, Notepad ++. If you get an error, you need to associate the code files with one of these two programs.
Link to setting up Notepad ++: http://www.creationkit.com/Notepad%2B%2B_Setup
You will then be presented with your default code.
Instead of trying to write everything you need by hand, lets make the CK do it for us. Close the code text box for now and go back to your button properties window.
On the right is the 'Papyrus Scripts' window with one script attached, 'TrapLever'. Select it and click the Remove button.
Then click on the Add button and click on your code and click the 'OK' button. Your code is now attached to this button.
5. Attaching message properties to our script/code
Back on the button's properties window, now select the script you just added and click on the 'Properties' button. You will be presenting with the script properties window.
We know we need at least 3 message box properties added, so lets do that now.
For the 'Type' drop down list, choose 'Message'.
The 'Name' allows you to give the property a 'name' that you will use in the script to point to this message.
And as before, the Documentation String allows you to say what this is with balloon windows.
Do this again for your 'Yes' response, and again for your 'No' response messages.
6. Making the messages
Select the message folder and right-click in the right messages area and select 'New' to create a new message.
This is an empty message box which you will fill out with the messages you will use in the script.
Give your new message a new ID and make sure the 'Message Box' is checked.
If you enter text into the 'title' box, it will display on the top of the message box.
In the Message Text section, the text you enter here will display in the main body of the message window.
Since our first message will be the "Question" messagebox, you will also need to add 'Menu buttons'. You can add them by right-clicking in that box.
You then enter the 'Item Text' for the message button. Do this twice, one for the yes and one for the no.
The Yes and No message box will not contain the Menu buttons, so leave them empty.
You should now have your three messages for the script.
7. Attaching the messages
Your probably thinking, this is sure a lot to go through for a message box? I would agree but its worth it.
Go back to the properties of your activator button, select your script and click on the 'Properties' button. Click on one of the button properties and then click the 'Edit Value' button.
Then from the drop down list, select the correct message for each Property.
You have now attached your messages to the script! Good work!
8. Finishing the script
Now to finalize the script with the 'action' of what we want to do. We will be pulling the messageboxes in and using them along with getting which button the player selected and acting on it.
Pull up your script and lets edit it. >Gameplay >Papyrun Manager and double-click your script in the list.
It should look similar to this:
Scriptname Class01Script extends ObjectReference
{Script for TESA Class 01}Message Property Question Auto
{Ask the player a question}Message Property Yes Auto
{Player answered Yes}Message Property No Auto
{Player answered No}[/code]Since we are activating a button, we need to use the 'onActivate' event type. So, lets add that line to the bottom of your script.[code]EVENT onActivate(objectReference akActionRef)
Reference Link to OnActivate: http://www.creationkit.com/OnActivate_-_ObjectReference"akActionRef" is a variable name you will be using in the script below. It holds the reference of the actor that activated the object this script is attached to. For clarity, you should leave it named as is.Now we want to make sure the actor that is activating this button is the player. You do that with an 'IF' condition check.
If akActionRef == Game.GetPlayer()
Note here that we are checking if the variable 'akActionRef ' is the player. Smarty says, if you use a double equals between two variables, you are checking for a condition, like do they equal. If you use a single equal between two variable, you are making the variable on the left equal the variable on the right.
Button = question.show()
Now we will display the message we created that is the 'question' and has the button index on it. Also note that we have 'button =' in front of this message box. This will automatically return the index number of the button that the player clicked. This is MUCH easier than it was in Oblivion.Since you are using a variable called Button, you also have to define that variable. Above the OnActivate event line, lets define it as such:
int ButtonEVENT onActivate(objectReference akActionRef)[/code]Now to figure out what the player wants to do.
[code]if Button == 0
Yes.show()
Game.GetPlayer().AddItem(Gold001, 1000, true)
Next, we will check the value in the variable 'Button'. If the value is 0 (remember when we made the question messagebox above, each button we created had an index number to the left of the text) then we know the player selected 'Yes'.Reference Link to AddItem: http://www.creationkit.com/AddItem_-_ObjectReferenceNow lets finish it up.
elseif Button == 1
No.show()
endif
EndIfendEVENT[/code]You would use the 'elseif' or just an 'else' since there are no other conditions we are checking for. But for clarity I use the index number for all my Button checks.I display the 'No' message and nothing else.I then finish the script up closing all the IF conditions with the 'endif' command.And to close my OnActivate event, I use the 'endEVENT' command.Now, this will not work since the script is saved outside of the CK. It does not know what the object 'Gold001' is. So we have to attach another property to the script (as explained above when you added messagebox properties to the >Activator Button object).I could not find anything related to how to get this to work, but in playing around with it, I found this seemed to work:This time we will add a 'MiscObject' property and give it the name, 'Gold001'.
[code]MiscObject Property Gold001 Auto
Save this and go back to your script. You will notice the property you just added was placed at the bottom of the script. I moved it above my OnActivate event.Now all you have to do is place your >Activator >Class01Button01 into the world somewhere where you can find it. Do that and test it in-game. You should find that when you answer yes, you get 1000 gold and nothing when you answer no.
Scriptname Class01Script extends ObjectReference
{Script for TESA Class 01}Message Property Question Auto
{Ask the player a question}Message Property Yes Auto
{Player answered Yes}Message Property No Auto
{Player answered No}MiscObject Property Gold001 Auto
int Button
EVENT onActivate(objectReference akActionRef)
If akActionRef == Game.GetPlayer()
Button = question.show()
if Button == 0
Yes.show()
Game.GetPlayer().AddItem(Gold001, 1000, true)
elseif Button == 1
No.show()
endif
EndIfendEVENT
[/code]