Dynamically Attaching Scripts

From the CreationKit Wiki
Revision as of 01:57, 11 April 2012 by imported>Fg109 (→‎Create the First Ability: messed up cell size)
Jump to navigation Jump to search

Dynamically Attaching Scripts

There are times that you may want to dynamically attach scripts to objects and actors. This could be because you don't want to muck around with the vanilla records, or because you think it's going to be too much work to edit every single vanilla record, or because you want your mod to work on mod added items.

I'm sure that there are many ways to do this that I haven't thought of, but I will explain what I think is the best way. Actually, I'm actually going to be explaining two ways, because I attach scripts to actors by using magic effects, which obviously won't work for non-actors.

Attaching Scripts to Actors

We're going to attach scripts to actors by making use of magic effects. We're going to give the player an ability, and that ability is going to be used to give nearby actors their own abilities containing scripted magic effects.

Create a Reference Alias

First we need to create a reference alias. This step is not actually necessary, but I prefer to just load up the mod with the player already having the ability, instead of needing to script an AddSpell in somewhere, or use the console.

So create a quest (it doesn't really matter what you name it) and make sure that Start Game Enabled is checked. Click on the Quest Alias tab, then right-click in the empty list and choose to create a new reference alias. You can name the alias whatever you want, but make sure that you choose to have it filled with a specific reference. Choose the reference 'PlayerRef' in the '(any)' cell.

Click OK to create the reference alias, and OK again to close the quest window. Hereafter, I will refer to the quest as DASQuest and the reference alias as PlayerAlias.

Create the First Ability

Next, we create the ability that we're going to put on the player. Navigate to Spells which is under Magic in the Object Window. Right-click in the list and choose to create a new spell. Name the spell whatever you want and change the Type to 'Ability'. Make sure that Casting is 'Constant Effect' and that Delivery is 'Self'. For now, we can leave the list of effects blank.

After closing the spell window, navigate to Magic Effects, under Magic in the Object Window. Right-click in the list and choose to create a new magic effect. Name the effect whatever you want and change the Effect Archetype to 'Cloak'. The Casting Type should be 'Constant Effect' and Delivery 'Self'. Check the flag for Hide in UI. Click OK to accept the effect as is for now.

Go back to the ability we just created, and in the effects list, add in the magic effect we just created. There doesn't have to be any conditions or duration, but the Magnitude needs to be changed. I used 192.0 for magnitude.

InDepth.jpg The magnitude of a Cloak archetype effect is the range of the cloak in feet. So a magnitude of 192.0 is 192 feet, or 4096 units, which is the length of a cell (4096 x 4096).

Hereafter, I will refer to this ability as CloakAbility and the magic effect as CloakEffect.

Create the Second Ability

Now we create the ability that we're going to put on our actors. Create a new ability similarly to how we created the first ability. The difference here is that we're going to use another magic effect.

When creating the magic effect, change its Effect Archetype to 'Script'. Again, make sure that the Casting Type is 'Constant Effect' and that Delivery is 'Self'. Check the No Death Dispel flag if you want the magic effect to stay active even when the actor is dead.

Click OK to accept the effect as is for now. Once the magic effect window has closed, double-click the magic effect to bring it back up again. This step is needed because when you first create a magic effect, you cannot attach a script to it. Well, now we can. This script is going to be the script that you want to attach to your actor. For the purposes of this tutorial, I will provide a script to monitor damage dealt by the player:

Scriptname DamageMonitorScript extends ActiveMagicEffect  

Actor MySelf
Float Health

Event OnEffectStart(Actor akTarget, Actor akCaster)
	MySelf = akTarget
	Health = MySelf.GetActorValue("Health")
	RegisterForSingleUpdate(0.25)
EndEvent

Event OnUpdate()
	Health = MySelf.GetActorValue("Health")
	RegisterForSingleUpdate(0.25)
EndEvent

Event OnEffectFinish(Actor akTarget, Actor akCaster)
	UnregisterForUpdate()
EndEvent

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, \
  bool abSneakAttack, bool abBashAttack, bool abHitBlocked)
	Float Damage = Health - MySelf.GetActorValue("Health")
	Health = MySelf.GetActorValue("Health")
	if (akAggressor == Game.GetPlayer())
		Debug.Notification("You did " + Damage as Int + " points of damage.")
	endif
EndEvent

Click OK to close the magic effect window. Now go back to the ability you have just made (the second one) and add the magic effect you just made (again, the second one) as its magic effect. You can leave the conditions blank with the duration and magnitude at 0. Click OK to close the spell window.

Hereafter, I will refer to this ability as MonitorAbility and the magic effect as MonitorEffect.

Create the Spell

We need to create another new spell. This is the spell that will be cast on an actor when he/she comes into contact with the cloak from the player's ability.

So navigate to Spells under Magic in the Object Window again. Right-click the list and choose to create a new spell. This time, keep the Type as 'Spell', but change the Casting to 'Concentration' and Delivery to 'Aimed'. Click OK to accept the spell as it is for now.

Now create another new magic effect. For this effect, change the Effect Archetype to 'Script', the Casting Type to 'Concentration' and Delivery to 'Aimed'. If you want this effect to affect dead actors as well, then check the flag for No Death Dispel.

Close the window, and then open it again to attach a script to the magic effect:

Scriptname AddAbilityScript extends ActiveMagicEffect  

Spell Property MonitorAbility Auto

Event OnEffectStart(Actor akTarget, Actor akCaster)
	akTarget.AddSpell(MonitorAbility)
EndEvent

Remember to set the value of the 'MonitorAbility' to the MonitorAbility that we created earlier. Click OK to close the magic effect window.

Go back to the spell we just created, and add this new magic effect to its effects list. The magnitude and duration can be left at 0, but this time we need to add a condition. The condition is:

HasMagicEffect		MagicEffect: 'MonitorEffect'	==	0

Make sure that it's set to run on Subject. If you don't want the spell to run on dead actors, you should add another condition run on Subject:

GetDead	NONE	==		0

Hereafter I will refer to this spell as ApplyingSpell and the magic effect as ApplyingEffect.

Putting It All Together

Go back to CloakEffect and edit the Assoc. Item 1 to point to ApplyingSpell. Click OK to close the magic effect window and save the changes.

Open up DASQuest and edit PlayerAlias. Click and drag CloakAbility into the spell list of PlayerAlias. Click OK twice to close the alias and quest windows, and save the mod.

Now start the game with your mod loaded and attack someone. You should see the damage you dealt as messages on the upper left corner of your screen.