Jump to content

[LE] Is there a way to check if a script is attached to an object without printing an error?


rkkn

Recommended Posts

In the case that prompted this topic, I already tried what I could think of. like adding a bool that's set on effect finish

Event OnEffectFinish(Actor akTarget, Actor akCaster)
	is_active = false

and

if is_active
	RegisterForSingleUpdate(0.2)
endif

but errors can still slip in. the effect can get unloaded on the same frame that the register is called. so for this case I have decided to simply opt for calling RegisterForUpdate() at the start instead

 

 

In another case, I have an array of ActiveMagicEffects and want to purge invalid entries on game load or periodically.

I just had an idea, though. StringUtil.Find(effect, "<None>") >= 0

Edited by rkkn
Link to comment
Share on other sites

Your topic headline: "Is there a way to check if a script is attached to an object without printing an error?"

(1) and again "Currently for ActiveMagicEffect scripts I use GetTargetActor() to check if the effect is still valid"
BAD IDEA !! -- effect has been finished there is no valid target

(2) and still more "effect can get unloaded on the same frame that the register is called. so for this case I have decided to simply opt for calling RegisterForUpdate() at the start instead"
VERY BAD IDEA !! -- if such mod will be removed from game load order, this kind of update cannot be unregister.. no way!

https://www.creationkit.com/index.php?title=RegisterForUpdate_-_Form

(3) some more "I .. want to purge invalid entries on game load or periodically." (invalid entries from array filled with ActiveMagicEffects)

Why do you need an array of MGEFs?

---
Sample papyrus codes for understanding.

1.

 

Scriptname XYZSampleEffectScript extends ActiveMagicEffect
; https://forums.nexusmods.com/index.php?/topic/10576588-is-there-a-way-to-check-if-a-script-is-attached-to-an-object-without-printing-an-error/

  GlobalVariable PROPERTY myGlobal auto        ; new created by CK


; -- EVENTs -- 2

EVENT OnEffectStart(Actor akTarget, Actor akCaster)
IF (myGlobal.GetValue() == 1.0)
    self.Dispel()
    RETURN    ; - STOP -    effect is already running
ENDIF
;---------------------
    myGlobal.SetValue(1.0)        ; *T*
    myF_Action(akTarget)
ENDEVENT


EVENT OnEffectStart(Actor akTarget, Actor akCaster)
    myGlobal.SetValue(0.0)        ; ***
ENDEVENT


; -- FUNCTION --

;--------------------------------
FUNCTION myF_Action(Actor target)
;--------------------------------
    WHILE (TRUE)
        Utility.Wait(0.2)
    
        IF myGlobal.GetValue()
        ELSE
            RETURN    ; - STOP -    break loop and leave the function immediately
        ENDIF
;        ----------------------
        ; your code here
    
    ENDWHILE
ENDFUNCTION

 

2.

 

Scriptname XYZSampleEffectScript2 extends ActiveMagicEffect
; https://forums.nexusmods.com/index.php?/topic/10576588-is-there-a-way-to-check-if-a-script-is-attached-to-an-object-without-printing-an-error/


; -- EVENTs -- 2

EVENT OnEffectStart(Actor akTarget, Actor akCaster)
    Debug.Trace(" OnEffectStart() - target = " +akTarget+ ", caster = " +akCaster+ "  " +self)
    myF_Action(akTarget)
ENDEVENT


EVENT OnEffectStart(Actor akTarget, Actor akCaster)
    Debug.Trace(" OnEffectFinish() - target = " +akTarget+ ", caster = " +akCaster+ "  " +self)
ENDEVENT


; -- FUNCTION --

;--------------------------------
FUNCTION myF_Action(Actor target)
;--------------------------------
    WHILE target.Is3DLoaded()            ; as long a the target has 3D loaded run the loop

        ; your code here
    
        Utility.Wait(0.25)
    ENDWHILE
ENDFUNCTION

 

3.

 

Scriptname XYZSampleEffectScript3 extends ActiveMagicEffect
; https://forums.nexusmods.com/index.php?/topic/10576588-is-there-a-way-to-check-if-a-script-is-attached-to-an-object-without-printing-an-error/

  Keyword PROPERTY myKW auto    ; a keyword added to the effect this script is running


; -- EVENTs -- 2

EVENT OnEffectStart(Actor akTarget, Actor akCaster)
    Debug.Trace(" OnEffectStart() - target = " +akTarget+ ", caster = " +akCaster+ "  " +self)

IF akTarget.HasMagicEffectWithKeyword(myKW)
    self.Dispel()
    RETURN    ; - STOP -    effect is already running on target, do not allow this effect more than once
ENDIF
;---------------------
    myF_Action(akTarget)
ENDEVENT


EVENT OnEffectStart(Actor akTarget, Actor akCaster)
    Debug.Trace(" OnEffectFinish() - target = " +akTarget+ ", caster = " +akCaster+ "  " +self)
ENDEVENT


; -- FUNCTION --

;--------------------------------
FUNCTION myF_Action(Actor target)
;--------------------------------
    WHILE target.Is3DLoaded()            ; as long as the target has 3D loaded run the loop

        ; your code here
    
        Utility.Wait(0.25)
    ENDWHILE
ENDFUNCTION

 

 

 

Without informations about the MGEF(s) you are using like effect duration, "fire and forget" or "cloak ability", as well as CK made conditions to the effect and finally the whole papyrus script code its nearly impossible to give you a good advice.

 

You wrote: "I just had an idea, though. StringUtil.Find(effect, "<None>") >= 0"

 

A patch is a patch. It does not really make sense to use SKSE functions to hide script related errors. Better is a redesign of your mod to avoid such errors.

Edited by ReDragon2013
Link to comment
Share on other sites

yeah I thought about using a Utility.Wait loop, but RegisterForUpdate seems fine in this particular case as it is a temporary effect and there are other conditions for terminating, so the forever loop wouldn't be a thing.

Though, I suppose extraordinarily slow systems could have issues with the function not completing within the interval.

Link to comment
Share on other sites

This is not knowing what your main goal or line of thinking is

 

States are my goto pun intended for cleanliness

 

OnStart GotoState("Whatever")

 

OnFinish GotoState("")

 

Anytime a check is called I would make sure I am in a state to continue else purge whatever. Nothing continues unless the effect is still active

Link to comment
Share on other sites

  • Recently Browsing   0 members

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