Difference between revisions of "OnUpdate - Form"

From the CreationKit Wiki
Jump to navigation Jump to search
imported>Cipscis
(→‎Notes: Added note about increasing file size in some cases if single update chains aren't used)
imported>JustinOther
m (Floatified parameters)
 
(One intermediate revision by one other user not shown)
Line 17: Line 17:
<source lang="papyrus">
<source lang="papyrus">
Function SomeFunction()                 
Function SomeFunction()                 
  registerForUpdate(5) ; Before we can use onUpdate() we must register.
RegisterForUpdate(5.0) ; Before we can use OnUpdate() we must register.
EndFunction
EndFunction
Event OnUpdate() ; because of how we registered, this event occurs every five seconds
Event OnUpdate() ; This event occurs every five seconds
  If myQuest.getStage() == 10
If myQuest.GetStage() == 10
    UnregisterForUpdate() ; when we're done with it, make sure to unregister
UnregisterForUpdate() ; when we're done with it, make sure to unregister
    Debug.Trace("Got what we needed, so stop polling!")
Debug.Trace("Got what we needed, so stop polling!")
  EndIf
EndIf
EndEvent
EndEvent
</source>
</source>
Line 31: Line 31:
*Aliases and quests will automatically unregister for this event when the quest stops. Active magic effects will automatically unregister when they are removed.
*Aliases and quests will automatically unregister for this event when the quest stops. Active magic effects will automatically unregister when they are removed.
*This event is '''not''' relayed to any aliases or magic effects attached to the form.
*This event is '''not''' relayed to any aliases or magic effects attached to the form.
*This event '''is''' relayed to other scripts attached to the same object.  eg. On a quest form with 2 main quest scripts and a fragment script,  an update event registered by one will be received by all three.
*Be careful with the use of this event. Because it uses real-time, it can start piling up on itself if the OnUpdate doesn't finish before the time is up. To avoid this, try using a longer time at registration, or do a 'single update chain' which looks like the following:
*Be careful with the use of this event. Because it uses real-time, it can start piling up on itself if the OnUpdate doesn't finish before the time is up. To avoid this, try using a longer time at registration, or do a 'single update chain' which looks like the following:
<source lang="papyrus">
<source lang="papyrus">
Function StartChain()
Function StartChain()
   RegisterForSingleUpdate(1) ; Give us a single update in one second
   RegisterForSingleUpdate(1.0) ; Give us a single update in one second
endFunction
EndFunction


Event OnUpdate()
Event OnUpdate()
  bool bkeepUpdating = true
Bool bKeepUpdating = True
  ; Do stuff here, and update the bkeepUpdating variable depending on whether you want another update or not
; Do stuff here, and update the bKeepUpdating variable depending on whether you want another update or not
  if bkeepUpdating
If bKeepUpdating
    RegisterForSingleUpdate(1)
RegisterForSingleUpdate(1.0)
  endIf
EndIf
endEvent
EndEvent</source>
</source>
This prevents multiple update events from stacking up on top of each other and slowing Papyrus down. If multiple update events do stack up on top of one another, this will also increase save file size. If ''enough'' instances of the event are stacked up in this way, it can cause significant increases in the size of any save file created.
This prevents multiple update events from stacking up on top of each other and slowing Papyrus down. If multiple update events do stack up on top of one another, this will also increase save file size. If ''enough'' instances of the event are stacked up in this way, it can cause significant increases in the size of any save file created.



Latest revision as of 12:33, 8 October 2012

Member of: ActiveMagicEffect Script, Alias Script, and Form Script

Event called periodically if the active magic effect/alias/form is registered for update events. This event will not be sent if the game is in menu mode.

Syntax[edit | edit source]

Event OnUpdate()

Parameters[edit | edit source]

None

Example[edit | edit source]

Function SomeFunction()                
	RegisterForUpdate(5.0) ; Before we can use OnUpdate() we must register.
EndFunction
	
Event OnUpdate() ; This event occurs every five seconds		
	If myQuest.GetStage() == 10
		UnregisterForUpdate() ; when we're done with it, make sure to unregister
		Debug.Trace("Got what we needed, so stop polling!")
	EndIf
EndEvent

Notes[edit | edit source]

  • Aliases and quests will automatically unregister for this event when the quest stops. Active magic effects will automatically unregister when they are removed.
  • This event is not relayed to any aliases or magic effects attached to the form.
  • This event is relayed to other scripts attached to the same object. eg. On a quest form with 2 main quest scripts and a fragment script, an update event registered by one will be received by all three.
  • Be careful with the use of this event. Because it uses real-time, it can start piling up on itself if the OnUpdate doesn't finish before the time is up. To avoid this, try using a longer time at registration, or do a 'single update chain' which looks like the following:
Function StartChain()
  RegisterForSingleUpdate(1.0) ; Give us a single update in one second
EndFunction

Event OnUpdate()
	Bool bKeepUpdating = True
	; Do stuff here, and update the bKeepUpdating variable depending on whether you want another update or not
	If bKeepUpdating
		RegisterForSingleUpdate(1.0)
	EndIf
EndEvent

This prevents multiple update events from stacking up on top of each other and slowing Papyrus down. If multiple update events do stack up on top of one another, this will also increase save file size. If enough instances of the event are stacked up in this way, it can cause significant increases in the size of any save file created.

See Also[edit | edit source]