Creating A Simple Do Once Script - First Time Activation
Scriptname MyTriggerScript Extends ObjectReference
Actor Property PlayerRef Auto; the quickest and most efficient way to refer to the player
Sound Property FXBoomSound Auto; I'm going to play a sound when the trigger activates.
Event onTriggerEnter(Actor AkActionRef)
If (AkActionRef == PlayerRef)
FXBoomSound.Play(Self)
PlayerRef.DamageAV("Health", (PlayerRef.GetBaseAV("Health")/2))
EndIf
EndEvent
Scriptname MyTriggerScript Extends ObjectReference
Actor Property PlayerRef Auto; the quickest and most efficient way to refer to the player
Sound Property FXBoomSound Auto; I'm going to play a sound when the trigger activates.
Int Controlvar = 0; This is the variable we will use to control when the trigger is activated
Event onTriggerEnter(Actor AkActionRef)
If (AkActionRef == PlayerRef) && (Controlvar == 0)
FXBoomSound.Play(Self)
PlayerRef.DamageAV("Health", (PlayerRef.GetBaseAV("Health")/2))
Controlvar = 1; The If statement above will no longer be true
EndIf
EndEvent
Then, I add an extra check into the if statement, making sure that the Controlvar is equal to 0. Finally, after our code is carried out, we set Controlvar to 1. Now, the Play and DamageAV functions can no longer be used because the If statement won't be processed. Why? Because it's conditions are no longer met. We changed Controlvar to 1, so Controlvar is no longer == (equal) to 0.
That is how you make a simple "Do Once" script.
If you want something else to happen after the first time, you can create an ElseIf statement. Have the ElseIf statement be: ElseIf Controlvar >= 1 if you want one thing to happen after you've first entered the trigger, or you can create an ElseIf for each controlvar stage. For example, I could have multiple ElseIfs for 2, 3, 4, and 5+.
Creating an Advanced Do Once Script
Do Once on Certain Activation
1) Open up your script. Refer to the base script I used in "Creating a Simple Do Once Script - First Time Activation" to see my base script. Then I would change it to the following:
Scriptname MyTriggerScript Extends ObjectReference
Actor Property PlayerRef Auto; the quickest and most efficient way to refer to the player
Sound Property FXBoomSound Auto; I'm going to play a sound when the trigger activates.
Int Controlvar = 0
Event onTriggerEnter(Actor AkActionRef)
If AkActionRef == PlayerRef
While Controlvar < 8
Controlvar += 1; this is shorthand for Set Controlvar to Controlvar + 1
EndWhile
If Controlvar == 8
FXBoomSound.Play(Self)
PlayerRef.DamageAV("Health", (PlayerRef.GetBaseAV("Health")/2))
Controlvar = 9
EndEvent
You could use that very method to make the triggerbox only activateable every x (replace x with your number of choice) times, if you want. However, this would create a never-ending loop which would could cause issues should somebody try to uninstall your mod.
Do Once With Item in Inventory
1) Open up your script. Refer to the base script I used in "Creating a Simple Do Once Script - First Time Activation" to see my base script. Then change it to the following.
Scriptname MyTriggerScript Extends ObjectReference
Actor Property PlayerRef Auto; the quickest and most efficient way to refer to the player
Sound Property FXBoomSound Auto; I'm going to play a sound when the trigger activates.
Weapon Property MyCoolSword Auto; this is the item we want the player to have
Int Controlvar = 0; This is the variable we will use to control when the trigger is activated
Event onTriggerEnter(Actor AkActionRef)
If (AkActionRef == PlayerRef) && (Controlvar == 0) && (PlayerRef.GetItemCount(MyCoolSword) >= 1)
FXBoomSound.Play(Self)
PlayerRef.DamageAV("Health", (PlayerRef.GetBaseAV("Health")/2))
Controlvar = 1; The If statement above will no longer be true
EndIf
EndEvent
You could also do a condition for if the player has any sword (use the WeapTypeSwordKeyword), or if the player has one or more items in a Formlist.
Do Once Combination - Has Item on Certain Activation
Scriptname MyTriggerScript Extends ObjectReference
Actor Property PlayerRef Auto; the quickest and most efficient way to refer to the player
Sound Property FXBoomSound Auto; I'm going to play a sound when the trigger activates.
Weapon Property MyCoolSword Auto; a very cool sword
Int Controlvar = 0
Event onTriggerEnter(Actor AkActionRef)
If AkActionRef == PlayerRef
While Controlvar < 8
Controlvar += 1; this is shorthand for Set Controlvar to Controlvar + 1
EndWhile
If Controlvar == 8
FXBoomSound.Play(Self)
PlayerRef.DamageAV("Health", (PlayerRef.GetBaseAV("Health")/2))
Controlvar = 9
EndEvent
We added the Controlvar variable to check when the player should be able to enter the trigger and activate the effects. Then, we added a While statement that checked if Controlvar was less than 8, and if it was, then it added 1 to Controlvar (if the player is the AkActionRef - the one entering the trigger). But once the Controlvar is no longer less than 8, it is not added to. And If the player is the AkActionRef and Controlvar is equal to 8, then our enclosed script (inside the If Controlvar == 8 statement) will be activated. Then, Controlvar is set to 9, so none of the prior script can be used again, since it relies on Controlvar being less than or equal to 8.
And that concludes this tutorial. If you'd like to learn how to do a certain "Do Once" script, tell me in the comments and I'll try to include a tutorial to answer your question.