Goal: So, my goal is to add a book to the game and when you pick that book up - the book will cast a spell on the carrier (only if the carrier is the player) every X seconds. The book is actually one of the traps that will exist in my dungeon - so taking the book will cast a damaging spell on the player ever few seconds until they drop it (at which point it teleports back to it's original position). The book IS safe to read however.
Script: The following script is commented, but I go over my expected and seen outcomes below.
Scriptname JWTrappedBookScript extends ObjectReference ; PropertiesSpell Property SpellTrap Auto ; Cast this spell every X seconds on the player when this book is in their possession.Float Property PulseRate = 0.5 Auto ; The amount of time to wait between casting this spell.; Private VariablesBool IsPlayerCarrying = False ; Is the player currently carrying this book?; When the player picks up this book, do a meaty amount of damage to them.Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer) if akNewContainer == Game.GetPlayer() IsPlayerCarrying = True; ; The player is now carrying this book ZapPlayer(); ; Zap the player straight away - if they are of a high level, they might survive. RegisterForSingleUpdate(PulseRate) ; Start zapping the player else UnregisterForUpdate(); IsPlayerCarrying = False; ; The player is no longer carrying this book Self.MoveToMyEditorLocation(); ; Always returns to starting position endifEndEvent; Update every 'PulseRate' secondsEvent OnUpdate() Debug.Trace("JWTrappedBookScript: OnUpdate Called.") ZapPlayer() if ( IsPlayerCarrying ) RegisterForSingleUpdate(PulseRate) EndIfEndEvent; This is the function we call to zap the playerFunction ZapPlayer() SpellTrap.Cast(Self, Game.GetPlayer()) Debug.Trace("JWTrappedBookScript: Just zapped player.")EndFunction
Expected Outcome: I would expect the above script to zap the player immediately after they pick up the book and then every 'PulseRate' seconds afterwards, until the book is dropped or the player dies.
Actual Outcome: The player does get zapped with the expected spell when they first pick up the book, but the OnUpdate event is never called. So they only get zapped once.
I've tried all sorts - hard-coding the PulseRate to 0.5, adding Disable(), Enable() calls at random places. Any help would be greatly appreciated, especially as it will help me to better understand the role of the OnUpdate method in an object's lifecycle. I understand that I could probably start a MagicEffect on the player in this case, but I like the idea of re-using this script to add other traps to future books (and indeed, any item you can pickup I guess!)