Jump to content

Terminating an ObjectReference script once it runs successfully


Tron91

Recommended Posts

Am trying to make a script which adds a Faction to an Actor using the OnLoad event. If the script finds a particular mod, it attaches a Faction from that mod to the Actor. But, the script keeps running on every OnLoad event. How to terminate the script once the Faction has been added by the script.

ScriptName SexFactionsSupport extends ObjectReference

Actor Property akActor Auto

Bool Processed = False

Event OnLoad()
    If Processed == True
        Return
    EndIf
    While akActor.Is3DLoaded() == False
        Utility.Wait(6.0)
    EndWhile
    Process(akActor)
EndEvent

Function Process(Actor akActor)
    If Game.GetModByName("SexFactions.esm") != 255
        Faction SexFaction = Game.GetFormFromFile(0x00000d62, "SexFactions.esm") as Faction
        If SexFaction
            akActor.AddToFaction(SexFaction)
            Processed = True
        EndIf
    EndIf
EndFunction
Link to comment
Share on other sites

Changing your OnLoad event to an OnInit event I believe would solve your issue, since then the script should only run once.

 

Edit: Barring cell resets for Objectreferences and stopping Quests, as IsharaMeradin pointed out below.

Link to comment
Share on other sites

OnInit would be called again when the cell containing the object resets.

 

Whether you use OnInit or OnLoad a slight adjustment should be in order:

If Processed == True
  Return
Else
  While akActor.Is3DLoaded() == False
    Utility.Wait(6.0)
  EndWhile
  Process(akActor)
EndIf

By shifting the code meant to run when the bool is false into the Else section you ensure that the code will not run again. While the OnInit and OnLoad events may be triggered again the bool variable will not be changed even with a full cell reset.

 

You can also use states so that after what you want done is complete subsequent loads or initializations will not trigger any code at all.

Link to comment
Share on other sites

In your case its harmless anyway. AddToFaction is really just SetFactionRank to 0 if they arent in the faction already. So there isn't exactly a taxing or otherwise harmful routine just calling that everytime anyhow. Like others said though if you do need to ensure something doesnt fire multiple times, states are a good route

Link to comment
Share on other sites

  • Recently Browsing   0 members

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