mindParalysis Posted January 1, 2021 Share Posted January 1, 2021 (edited) Hi guys, So the situation is this: I want my NPC character to cast a spell whenever he has enough mana for it, during combat. The spell is called consecrated blade from the arcanum mod. It blesses the weapon for 30seconds. Once the spell wears off, I want the script to realise that the effect has worn off, and then recast the spell if he has enough mana. Now on testing, he casts the spell at the start of combat, but does not refresh the spell, once the effect fades. Can anyone help? code below. Scriptname VSB_CombatAI extends ReferenceAlias Actor Property Verus Auto ;This boolean will determine whether still in combat or not Bool CombatLoop = False GlobalVariable Property consecratedBladeCasted Auto Spell Property consecratedBlade Auto MagicEffect Property consecratedBladeEffect Auto Function castConsecratedBlade(Actor theActor) If consecratedBladeCasted.GetValueInt() == 0 If Verus == theActor consecratedBlade.Cast(Verus) EndIf EndIf EndFunction Event OnUpdate() While CombatLoop If Verus.GetActorValue("Magicka") >= 120 castConsecratedBlade(Verus) EndIf If !Verus.HasMagicEffect(consecratedbladeEffect) If consecratedBladeCasted.GetValueInt() == 1 consecratedBladeCasted.SetValueInt(0) EndIf EndIf EndWhile EndEvent Event OnCombatStateChanged(Actor akTarget, int aeCombatState) If aeCombatState == 1 CombatLoop = True RegisterForUpdate(0.5) EndIf If aeCombatState == 0 CombatLoop = False EndIf EndEvent Edited January 1, 2021 by mindParalysis Link to comment Share on other sites More sharing options...
mindParalysis Posted January 1, 2021 Author Share Posted January 1, 2021 Never mind, it actually is working, it's just that my NPC kept wasting mana on healing himself, so couldn't cast the spell. All good. Link to comment Share on other sites More sharing options...
ReDragon2013 Posted January 1, 2021 Share Posted January 1, 2021 (edited) You wrote: "Never mind, it actually is working" ARE YOU SURE! (1) Do never use in scripts RegisterForUpdate(f) ; f is an alias for waittime herewithout to stop this registration by UnRegisterForUpdate()better take next RegisterForSingleUpdate(0.5)(2) Do never run a while loop without a wait() inside, except you want to fill only array(s) or formlist(s) or similiar fast things. (3) And I do not really understand why are you using a GlobalVariable here. For next script I assume the script is attached to Alias, which refers to actor Verus. VSB_CombatAI Scriptname VSB_CombatAI extends ReferenceAlias ; https://forums.nexusmods.com/index.php?/topic/9463778-help-with-scripting-recurring-spell-casts-during-combat/ ;GlobalVariable PROPERTY consecratedBladeCasted auto ;Actor PROPERTY Verus auto ; we assume this is the actor refered to this alias script Spell PROPERTY consecratedBlade auto MagicEffect PROPERTY consecratedBladeEffect auto Bool CombatLoop ; [default=False], boolVar will determine whether (this alias refered actor is) still in combat or not ; -- EVENTs -- 3 EVENT OnCombatStateChanged(Actor akTarget, int aeCombatState) ;========================= IF (aeCombatState == 1) CombatLoop = TRUE RegisterForSingleUpdate(0.5) RETURN ; - STOP - the refered Actor of this Alias goes into combat ENDIF ;--------------------- ; IF (aeCombatState == 0) ; left combat ; IF (aeCombatState == 2) ; is looking for combat target UnRegisterForUpdate() CombatLoop = False ENDIF ENDEVENT ;EVENT OnDying(Actor akKiller) ; this alias refered actor is going to die ; UnRegisterForUpdate() ; CombatLoop = False ;ENDEVENT EVENT OnDeath(Actor akKiller) ; this alias refered actor is dead ;============ UnRegisterForUpdate() CombatLoop = False self.Clear() ENDEVENT EVENT OnUpdate() ;============= WHILE (CombatLoop) ; (CombatLoop == TRUE) myF_TryCast() ; give time to recognize for effect runtime Utility.Wait(0.1) ; (0.25) ENDWHILE ENDEVENT ; -- FUNCTION -- ;--------------------- FUNCTION myF_TryCast() ; internal helper ;--------------------- actor Verus = self.GetActorReference() IF (Verus) && !Verus.IsDead() ELSE CombatLoop = False UnRegisterForUpdate() RETURN ; - STOP - mod uninstalled OR Verus is dead ENDIF ;--------------------- IF Verus.HasMagicEffect(consecratedbladeEffect) ; effect is still active ELSE ; effect is gone IF (Verus.GetActorValue("Magicka") >= 120.0) ; spell costs are 120.0 consecratedBlade.Cast(Verus) ; cast the spell to Verus ENDIF ENDIF ENDFUNCTION Edited January 1, 2021 by ReDragon2013 Link to comment Share on other sites More sharing options...
Recommended Posts