Difference between revisions of "Adding Working Scripts to Items"

From the CreationKit Wiki
Jump to navigation Jump to search
imported>Mr6
imported>Cipscis
m (Fixing capitalisation of quote from Creation Kit after checking in Creation Kit)
 
(2 intermediate revisions by 2 users not shown)
Line 12: Line 12:
   
   
===Writing the script===
===Writing the script===
Now that the easy part is done, we will move on to the difficult part. From Gameplay at the top select [[Papyrus Script Manager]], right click on a script and select New. You will be greeted with a dialogue box.
Now that the easy part is done, we will move on to the difficult part. From Gameplay at the top select [[Papyrus Script Manager]], right click on a script and select New. You will be greeted with a dialogue box. (''You could also add the script from the item window by pressing "Add" on the script section and choosing ''<nowiki>[New Script]</nowiki>'', that way it will default to the right type'')


[[File:AddingScriptsToItems-NewScript.jpg|200px|thumb|none|alt=Container window settings|Create a Script]]
[[File:AddingScriptsToItems-NewScript.jpg|200px|thumb|none|alt=Container window settings|Create a Script]]


Because it's an item, it must extend [[ObjectReference_Script|ObjectReference]] because [[OnEquipped_-_ObjectReference|OnEquipped]] is an ObjectReference Script.
Because it's an item, it must extend [[ObjectReference_Script|ObjectReference]]. Because [[OnEquipped_-_ObjectReference|OnEquipped]] is an event that will be called natively for ObjectReferences, we will be able to use it in this script.
Now we can simply just write out our script and save it.


This is the scriptname:
Now we can simply write out our script and save it.
 
This is the ScriptName declaration, and its documentation comment:
<source lang="papyrus">
<source lang="papyrus">
Scriptname tutGlovesofBlizzardScript extends ObjectReference   
ScriptName tutGlovesofBlizzardScript extends ObjectReference   
{OnEquip adds a spell}
{OnEquip adds a spell}
</source>
</source>


In order to use things like [[AddSpell]] and [[AddItem]], you have to make a [[Variables_and_Properties|property]] for them in your script. If not you will get an "Undefined variable" error.
 
In order to use things like [[AddSpell - Actor|AddSpell]] and [[AddItem - ObjectReference|AddItem]], you have to make a [[Variables_and_Properties|property]] for them in your script. If you don't do this, you will get an "undefined variable" error.


<source lang="papyrus">
<source lang="papyrus">
Line 31: Line 33:
</source>
</source>


This is our ObjectReference.OnEquipped event. It is important you extend it by ObjectReference in the Scriptname, or else this will not compile properly saying it is "not a valid or non existent function".
 
This next snippet our OnEquipped event. It is important, if this event is being used, that the script extends ObjectReference, or another script that extends ObjectReference (like [[Actor Script|Actor]]), or else the the event will never fire.


<source lang="papyrus">
<source lang="papyrus">
Event OnEquipped(Actor akActor)
Event OnEquipped(Actor akActor)
     if akActor == Game.GetPlayer()
     If (akActor == Game.GetPlayer())
         Game.GetPlayer().AddSpell(Blizzard, true)
         Game.GetPlayer().AddSpell(Blizzard)
     endIf
     EndIf
endEvent
EndEvent
</source>
</source>


This is simply just another event using the same property to remove the spell when the player unequipped it. We only want them to have the spell if they wear it.
 
This is simply another event, [[OnUnequipped - ObjectReference|OnUnequipped]] using the same property to remove the spell when the player unequips  our scripted gloves. We only want them to have the spell if they wear it.


<source lang="papyrus">
<source lang="papyrus">
Event OnUnequipped(Actor akActor)
Event OnUnequipped(Actor akActor)
     if akActor == Game.GetPlayer()
     If (akActor == Game.GetPlayer())
         Game.GetPlayer().RemoveSpell(Blizzard)
         Game.GetPlayer().RemoveSpell(Blizzard)
     endIf
     EndIf
endEvent
EndEvent
</source>
</source>


So now that our script is finished, we can save it. Hopefully yours compiles as flawlessly as this example.
 
Now that our script is finished, we can save it. If you've done everything correctly, yours should compile as flawlessly as this example.


===Attaching the Script===
===Attaching the Script===
Now, we go back to our gloves, and add the script, edit the property and select the spell that will stand in for the Blizzard property.
Now, we go back to our gloves in the Creation Kit, and add the script, edit the property and select the spell that will stand in for the Blizzard property.


[[File:AddingScriptsToItems-AttachScript.jpg|200px|thumb|none|alt=Container window settings|Attach the Script]]
[[File:AddingScriptsToItems-AttachScript.jpg|200px|thumb|none|alt=Container window settings|Attach the Script]]


===Testing In Game===
===Testing In Game===
Now we can test them. I added the gloves in to the game by allowing the players to combine normal elven gauntlets with Frost Salts at the Forge. You can use any method you want to put them in the game.
Now that our script has been set up in the Creation Kit, we can test it. I added the gloves in to the game by allowing the players to combine normal elven gauntlets with Frost Salts at the Forge. You can use any method you want to put them in the game, including the console (use the [[Help]] command to find the right formID, and [[AddItem|player.AddItem]] to add them to the player).
 
Everything looks good on the gauntlets so far, and when we equip them our script runs successfully.
Everything looks good on the gauntlets so far, and when we equip them our script runs successfully.



Latest revision as of 16:25, 21 February 2012


What it does:[edit | edit source]

This tutorial will go step-by-step to adding a simple script to an item-- gloves that grant you the Blizzard spell while wearing them.

Create the item[edit | edit source]

First, you want to create the gloves. Let's say our gloves are Elven Gauntlets of Thalmor origin. The fields I edited are highlighted in red.

Container window settings
Create an Item

Writing the script[edit | edit source]

Now that the easy part is done, we will move on to the difficult part. From Gameplay at the top select Papyrus Script Manager, right click on a script and select New. You will be greeted with a dialogue box. (You could also add the script from the item window by pressing "Add" on the script section and choosing [New Script], that way it will default to the right type)

Container window settings
Create a Script

Because it's an item, it must extend ObjectReference. Because OnEquipped is an event that will be called natively for ObjectReferences, we will be able to use it in this script.

Now we can simply write out our script and save it.

This is the ScriptName declaration, and its documentation comment:

ScriptName tutGlovesofBlizzardScript extends ObjectReference  
{OnEquip adds a spell}


In order to use things like AddSpell and AddItem, you have to make a property for them in your script. If you don't do this, you will get an "undefined variable" error.

Spell Property Blizzard auto


This next snippet our OnEquipped event. It is important, if this event is being used, that the script extends ObjectReference, or another script that extends ObjectReference (like Actor), or else the the event will never fire.

Event OnEquipped(Actor akActor)
    If (akActor == Game.GetPlayer())
        Game.GetPlayer().AddSpell(Blizzard)
    EndIf
EndEvent


This is simply another event, OnUnequipped using the same property to remove the spell when the player unequips our scripted gloves. We only want them to have the spell if they wear it.

Event OnUnequipped(Actor akActor)
    If (akActor == Game.GetPlayer())
        Game.GetPlayer().RemoveSpell(Blizzard)
    EndIf
EndEvent


Now that our script is finished, we can save it. If you've done everything correctly, yours should compile as flawlessly as this example.

Attaching the Script[edit | edit source]

Now, we go back to our gloves in the Creation Kit, and add the script, edit the property and select the spell that will stand in for the Blizzard property.

Container window settings
Attach the Script

Testing In Game[edit | edit source]

Now that our script has been set up in the Creation Kit, we can test it. I added the gloves in to the game by allowing the players to combine normal elven gauntlets with Frost Salts at the Forge. You can use any method you want to put them in the game, including the console (use the Help command to find the right formID, and player.AddItem to add them to the player).

Everything looks good on the gauntlets so far, and when we equip them our script runs successfully.

Container window settings
View Ingame

This concludes the tutorial, and I hope it helped people avoid a lot of frustration.