Death check with weapon script

Revision as of 14:42, 13 August 2010 by imported>Spd1274 (Writing the Article)


Introduction

In the process of working on a project that requires the Player to kill an NPC, and than adds an item upon the death of the NPC, and only an NPC, it became necessary to layout the steps for the project, and this seemed the best, and most helpful way to others, to do that.

This Tutorial will follow the steps below:
1. Create a Magic Effect Script that will check for the death of an NPC at the Player's hand, than add an Item to the inventory with a nice little message.
2. Create the item to be added, in this case, the soul of the NPC in the form of a bottled potion.
3. Create a scripted enchantment that runs our script. (Because there is no way to activate a script when the Player hits an NPC i.e. Onstrike or anything of that kind. See: Beginning Scripts).
4. Finally create, and place into Tamriel, the weapon that will execute the script.

Notes:
It is assumed the reader has a prior basic knowledge of scripting, however, as it is most frustrating to see things work but not understand why, most work done here will be well explained.

Other Tutorials of possible relevance:
Deadcount on a weapon.

So without further ado, "Get on with it!"


The Script

To begin, we load the CS and than open the script editor by clicking on the File:ScriptEditorIcon.png symbol. Which is at the very end of the toolbar:
File:Toolbarwscriptcircle.png

Now that we have the editor open, here is our skeleton script:

scriptname Name

ref Target

short Dead
short Once

Begin ScriptEffectStart
	set Target to GetSelf
	if Target.GetDead == 1 && Target.GetIsPlayableRace == 1
	   set Dead to 1
	endif
	set Once to 0
end

Begin GameMode
	If Dead == 1 && Once == 0 
	   player.additem Item 1
	   MessageBox "Message."
           set Once to 1
	endif
end

Now I understand that this seems quite alot to take in at first glance. However, lets break it down:

scriptname name

ref Target

short Dead
short Once

Here we name our script, and give it the Script ID it will be saved and called by.
Than we create a reference, which in this case will point to the Target of the player, hence the name 'Target.'
Than we list two shorts, which are variables this script can reference inside of itself, Dead and Once. Which we will see later.

Begin ScriptEffectStart
	set Target to GetSelf
	if Target.GetDead == 1 && Target.GetIsPlayableRace == 1
	   set Dead to 1
	endif
	set Once to 0
end

This portion of the script does all the hard work. It begins as soon on ScriptEffectStart, which in the case of an enchantment, means it will begin with the Player strikes a creature.
It tells our reference Target to find out what exactly it is, thus retrieving the data on the NPC or creature the Player is attacking.
Than Target is tested if it is dead, and if it is a playable race, and therefore and NPC. (This excludes creatures such as Golden Saints, because there does not seem to be a NPC test in Vanilla CS.) If these tests both evaluate as true, the script sets the short Dead to 1 and than Once to 0, confirming a run of the script.

Begin GameMode
	If Dead == 1 && Once == 0 
	   player.additem Item 1
	   MessageBox "Message."
           set Once to 1
	endif
end

Finally our script finishes up by running a test that Dead and Once have been set to the correct values for a killed NPC.
Than the script adds an Item of our choice to the Player's inventory and displays a message.
In order to avoid the script giving the Player the item over and over and over, the last action of the script is to set Once to 1, avoiding a loop.