Difference between revisions of "OnInit"
Jump to navigation
Jump to search
imported>Rhavlovick m (1 revision: Clobber re-import by Henning) |
imported>Scrivener07 (Restored the document from DavidJCobb's last commit but I retained the example changes made by XJDHDR.) |
||
(13 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
'''Member of:''' Any and all scripts. | '''Member of:''' Any and all scripts. | ||
Line 10: | Line 7: | ||
Event OnInit() | Event OnInit() | ||
</source> | </source> | ||
== Parameters == | |||
None | |||
== Example == | == Example == | ||
This example uses state switching to run our OnInit code. | |||
<source lang="papyrus"> | <source lang="papyrus"> | ||
; This is within the "empty" state | ; This is within the "empty" state | ||
Event OnInit() ; This event will run once, when the script is initialized | |||
GotoState("InitStart") | |||
EndEvent | |||
State InitStart | |||
Event OnBeginState() | |||
Debug.Trace("OnInit code started") | |||
; Do everything we need to here | |||
GotoState("") ; An empty string represents the "empty state" for scripts. | |||
EndEvent | |||
Event OnInit() | |||
EndEvent | |||
EndState | |||
</source> | </source> | ||
= | This script uses an OnUpdate event to run our OnInit code. | ||
<source lang="papyrus"> | |||
; This is within the "empty" state | |||
Event OnInit() ; This event will run once, when the script is initialized | |||
RegisterForSingleUpdate(2.0) | |||
EndEvent | |||
Event OnUpdate() | |||
Debug.Trace("OnInit code started, so stop polling!") | |||
UnregisterForUpdate() | |||
; Do everything we need to here | |||
GotoState("active") ; Switch to a state that doesn't use OnUpdate() | |||
EndEvent | |||
State active | |||
Event OnUpdate() | |||
; Do nothing in here. | |||
EndEvent | |||
EndState | |||
</source> | |||
== Notes == | == Notes == | ||
*Until OnInit has finished running, your script will not receive any events, and other scripts that try to call functions or access properties on your script will be paused until the event finishes. The only exceptions are when the functions are being called from another script inside its OnInit event, or inside a property set function being set by the master file. | *Until OnInit has finished running, your script will not receive any events, and other scripts that try to call functions or access properties on your script will be paused until the event finishes. The only exceptions are when the functions are being called from another script inside its OnInit event, or inside a property set function being set by the master file. | ||
**As a result, it is generally not advisable to fill your OnInit event with a large amount of processes. It is safer and more reliable to allow your OnInit to finish running while only performing minor tasks, and defer other larger activities to an update (as in the example above) or something similar. | |||
*OnInit is called at the following times: | *OnInit is called at the following times: | ||
**For Quests and Aliases: On game startup, and again whenever the quest starts, due to the quest being reset. | **For Quests and Aliases: On game startup, and again whenever the quest starts, due to the quest being reset. | ||
**For other base objects like Topic Infos, Perks, etc that have scripts that run on the base object: At game start | **For other base objects like Topic Infos, Perks, etc that have scripts that run on the base object: At game start the first time this script is loaded | ||
**For persistent refs: At game start | **For persistent refs: At game start the first time this script is loaded | ||
**For non-persistent refs: When the ref is loaded the first time | **For non-persistent refs: When the ref is loaded the first time | ||
*OnInit is called ''again'' when an object is reset. All your variables and properties will be reset before OnInit is called. | *OnInit is called ''again'' when an object is reset. All your variables and properties will be reset before OnInit is called. | ||
**For Quests and Aliases this will happen when your quest starts. This means that a quest and alias will see OnInit twice before it starts the first time (game startup, and on reset). This should not cause any problems unless you are manipulating something outside the quest that doesn't reset in your OnInit. | **For Quests and Aliases this will happen when your quest starts. This means that a quest and alias will see OnInit twice before it starts the first time (game startup, and on reset). This should not cause any problems unless you are manipulating something outside the quest that doesn't reset in your OnInit. | ||
**For references this happens when the cell resets. | **For references this happens when the cell resets. | ||
*If a Quest is set to run on game startup and doesn't have the "Run Once" flag ticked, its OnInit event will fire twice when it starts. Ticking the "Run Once" flag, however, prevents the Quest from being reset when it starts, so in this case its OnInit event will only fire once. | |||
*If OnInit is defined within a [[States_(Papyrus)|state]], it must also be defined in the empty state (i.e. outside of all explicitly defined states), even if it is not the default state for that script. The definition of OnInit in the empty state can be empty. | |||
== See Also == | == See Also == | ||
None | None | ||
[[Category:Scripting]] | |||
[[Category:Papyrus]] | |||
[[Category:Events]] |
Latest revision as of 19:35, 23 January 2019
Member of: Any and all scripts.
Event called when the script has been created and all its properties have been initialized.
Syntax[edit | edit source]
Event OnInit()
Parameters[edit | edit source]
None
Example[edit | edit source]
This example uses state switching to run our OnInit code.
; This is within the "empty" state
Event OnInit() ; This event will run once, when the script is initialized
GotoState("InitStart")
EndEvent
State InitStart
Event OnBeginState()
Debug.Trace("OnInit code started")
; Do everything we need to here
GotoState("") ; An empty string represents the "empty state" for scripts.
EndEvent
Event OnInit()
EndEvent
EndState
This script uses an OnUpdate event to run our OnInit code.
; This is within the "empty" state
Event OnInit() ; This event will run once, when the script is initialized
RegisterForSingleUpdate(2.0)
EndEvent
Event OnUpdate()
Debug.Trace("OnInit code started, so stop polling!")
UnregisterForUpdate()
; Do everything we need to here
GotoState("active") ; Switch to a state that doesn't use OnUpdate()
EndEvent
State active
Event OnUpdate()
; Do nothing in here.
EndEvent
EndState
Notes[edit | edit source]
- Until OnInit has finished running, your script will not receive any events, and other scripts that try to call functions or access properties on your script will be paused until the event finishes. The only exceptions are when the functions are being called from another script inside its OnInit event, or inside a property set function being set by the master file.
- As a result, it is generally not advisable to fill your OnInit event with a large amount of processes. It is safer and more reliable to allow your OnInit to finish running while only performing minor tasks, and defer other larger activities to an update (as in the example above) or something similar.
- OnInit is called at the following times:
- For Quests and Aliases: On game startup, and again whenever the quest starts, due to the quest being reset.
- For other base objects like Topic Infos, Perks, etc that have scripts that run on the base object: At game start the first time this script is loaded
- For persistent refs: At game start the first time this script is loaded
- For non-persistent refs: When the ref is loaded the first time
- OnInit is called again when an object is reset. All your variables and properties will be reset before OnInit is called.
- For Quests and Aliases this will happen when your quest starts. This means that a quest and alias will see OnInit twice before it starts the first time (game startup, and on reset). This should not cause any problems unless you are manipulating something outside the quest that doesn't reset in your OnInit.
- For references this happens when the cell resets.
- If a Quest is set to run on game startup and doesn't have the "Run Once" flag ticked, its OnInit event will fire twice when it starts. Ticking the "Run Once" flag, however, prevents the Quest from being reset when it starts, so in this case its OnInit event will only fire once.
- If OnInit is defined within a state, it must also be defined in the empty state (i.e. outside of all explicitly defined states), even if it is not the default state for that script. The definition of OnInit in the empty state can be empty.
See Also[edit | edit source]
None