Jump to content

OnSpellCast won't trigger when casting spells, what am I doing wrong?


Recommended Posts

So, very new to scripting, made a basic script which *should* level up your lockpicking skill (which I've repurposed to an unarmored skill tree). Some background, I created a perk labeled "MSGUnarmoredLeveling" as a perk that does nothing, but is added to your perks any time you're not wearing armor. I've tested that this perk works correctly, and it is listed in my active effects while I am unarmored. Anyway, here is the script:

 

Scriptname MSGUnarmoredLevelup extends Perk
;;;;;;;;;;;;;;;;;;;;;;;;;
;Input:None so far
;
;Progresses the Unarmored skill by leveling lockpicking whenever
;you cast a spell while unarmored
;
;;;;;;;;;;;;;;;;;;;;;;;;;
Actor Property Player Auto
Perk Property MSGUnarmoredLeveling Auto
Event OnInit()
;Display WIP Message Box
Debug.MessageBox("The Unarmored skill line is currently a work in progress.")
EndEvent
;This is a test script to level up your unarmored while casting spells and not wearing armor
Event OnSpellCast()
If Player.HasPerk(MSGUnarmoredLeveling)
Game.AdvanceSkill("Lockpicking", 10.0)
Debug.MessageBox("Skill Advancement is being triggered!")
Else
Debug.MessageBox("OnSpellCast Event is being triggered, but seeing if the player has the perk is not being triggerd")
EndIf
EndEvent
I've assigned player property to PlayerRef and the perk to MSGUnarmoredLeveling in the creation kit, but when I cast a spell with MSGUnarmoredLeveling listed under my active effects, nothing happens. The OnInit messagebox appears when I start up the game, so I know the script is active somewhere, it just doesn't seem to be doing anything.
Thanks a bunch for any help, and sorry if this is painfully obvious to everyone else!
Link to comment
Share on other sites

My previous comment may have been in error. I thought you were saying that you attached the script to the player through a magic effect. How are you attaching the script to the player? If it is a reference alias then the script should extend ReferenceAlias, if it is a magic effect then it should extend ActiveMagicEffect. If you don't know what I am talking about then you should walk us through the steps you have taken so far.

Link to comment
Share on other sites

So I didn't attach it to the player, I've attached it to a perk I've created called "MSGLevelingAid" I've rewritten the code as follows:

 

Scriptname MSGUnarmoredLevelup extends Perk
;;;;;;;;;;;;;;;;;;;;;;;;;
;Input:None so far
;
;Progresses the Unarmored skill by leveling lockpicking whenever
;you cast a spell while unarmored
;
;;;;;;;;;;;;;;;;;;;;;;;;;
Actor Property Player Auto
Perk Property MSGUnarmoredLeveling Auto
Perk Property MSGMixedArmorLeveling Auto
Perk Property MSGLightArmorLevelingPure Auto
Perk Property MSGHeavyArmorLevelingPure Auto
Perk Property MSGLevelingAid Auto
Message Property MSGUnarmoredLevelingMessage Auto
Message Property MSGLightArmorLevelingMessage Auto
Message Property MSGHeavyArmorLevelingMessage Auto
Message Property MSGMixedArmorLevelingMessage Auto
Event OnInit()
;Display WIP Message Box
;Debug.MessageBox("The Unarmored skill line is currently a work in progress.")
;Gives the player the perk that will apply leveling aid perks based on worn status
Game.GetPlayer().AddPerk(MSGLevelingAid)
;Call the static function
MSGStaticLevelUp()
EndEvent
;This is a test script to level up your unarmored while casting spells and not wearing armor
Event OnSpellCast()
If Player.HasPerk(MSGUnarmoredLeveling)
Game.AdvanceSkill("Lockpicking", 10.0)
Debug.MessageBox("Skill Advancement is being triggered!")
Else
Debug.MessageBox("OnSpellCast Event is being triggered, but seeing if the player has the perk is not being triggerd")
EndIf
EndEvent
Function MSGStaticLevelUp()
;check to see if player is unarmored
if Player.HasPerk(MSGUnarmoredLeveling)
Game.AdvanceSkill("Lockpicking",10.0)
;MSGUnarmoredLevelingMessage.Show()
ElseIf Player.HasPerk(MSGMixedArmorLeveling)
Game.AdvanceSkill("HeavyArmor",1.0)
Game.AdvanceSkill("LightArmor",1.0)
;MSGMixedArmorLevelingMessage.Show()
ElseIf Player.HasPerk(MSGHeavyArmorLevelingPure)
Game.AdvanceSkill("HeavyArmor",2.0)
;MSGHeavyArmorLevelingMessage.Show()
ElseIf Player.HasPerk(MSGLightArmorLevelingPure)
Game.AdvanceSkill("LightArmor",2.0)
;MSGLightArmorLevelingMessage.Show()
Else
;Debug.MessageBox("None of your leveling aid perks have been applied")
EndIf
Utility.Wait(10.0)
MSGStaticLevelUp()
EndFunction
This has successfully applied the "MSGLevelingAid" perk to the player, and then that perk applies an Ability which gives you any of the other 4 sub perks (MSGUnarmoredLeveling, etc.) based on your current armored status. I've successfully tested the recursion function I added to the script and it'll slowly level over time your Unarmored, Light Armor, or Heavy Armor based on what you're wearing, so I know the perks are working correctly. Perhaps the Event OnSpellCast() is not the best trigger for this script? I'll try adding an extra level to the recursion which will check to see if the player has the appropriate perk and is casting a spell, but I was trying to avoid having the script rerunning every second to see if you're casting.
Link to comment
Share on other sites

This is not a great way to go about this at all. You should move your scripts off of the perk into an ability, and have the magic effects of the ability turn on and off based on condition functions in the spell. That way you can use RegisterForSingleUpdate() loops instead of Utility.Wait() loops, which will be much more efficient and easier to abort if necessary. You can have a different magic effect for each condition. Magic effects also receive the OnSpellCast event (which, again, requires a parameter). Perks do not receive that event.

 

As written your script will consume massive resources and eventually either cause crazy script lag or crash the game.

Link to comment
Share on other sites

Sweet, that worked like a champ, and made for a much neater script. Thanks again for the help!

Here's how much shorter the script is, for anyone who's working with similar issues:

 

Scriptname MSGSpellCastUnarmoredLeveling extends activemagiceffect
Actor Property Player Auto
MagicEffect Property MSGSpellCastUnarmored Auto
Message Property MSGSpellCastUnarmoredMessage Auto
Event OnEffectStart(Actor akTarget, Actor akCaster)
ObjectReference Caster = akCaster
MSGSpellCastUnarmoredLevelingLoop()
EndEvent
Function MSGSpellCastUnarmoredLevelingLoop()
If Player.HasMagicEffect(MSGSpellCastUnarmored)
Game.AdvanceSkill("Lockpicking",20.0)
MSGSpellCastUnarmoredMessage.Show()
RegisterForSingleUpdate(1)
Else
UnRegisterForUpdate()
EndIf
EndFunction
Event OnUpdate()
MSGSpellCastUnarmoredLevelingLoop()
EndEvent
Script is attached to a magic effect which is applied to an ability which triggers that magic effect any time you are not wearing armor and casting.
Edited by the0champion
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...