Better explanation of onUpdate?

Post » Wed Jun 20, 2012 1:03 am

I've been reading through the documentation, and I can't seem to figure out how onUpdate works. Could someone explain it to me with some example code?
User avatar
Brιonα Renae
 
Posts: 3430
Joined: Mon Oct 22, 2007 3:10 am

Post » Tue Jun 19, 2012 8:41 am

Check it out: http://www.creationkit.com/OnUpdate_(Papyrus)
User avatar
Kyra
 
Posts: 3365
Joined: Mon Jan 29, 2007 8:24 am

Post » Tue Jun 19, 2012 4:06 pm

You RegisterForUpdate(seconds) or RegisterForSingleUpdate(seconds) an active magic effect, alias, or form, and after that amount of seconds the OnUpdate event is fired for that active magic effect, alias, or form.

Edit: If you use RegisterForUpdate the event continues to fire after every interval of seconds, if you use RegisterForSingleUpdate it only fires once
User avatar
Elizabeth Falvey
 
Posts: 3347
Joined: Fri Oct 26, 2007 1:37 am

Post » Tue Jun 19, 2012 12:21 pm

I'll try.

Function SomeFunction()    ; This should be what causes the OnUpdate to fire.    registerForUpdate(1)    ; This makes the OnUpdate actually run, you decide how many seconds between each run - in this case one second.EndFunctionEvent OnUpdate()    ; Here you put the code that you want to repeat once every second.EndEvent

So what it does is loop through the actions within the event once every X seconds (where X is the number you specify when registering for update.)
It will keep looping like this until you unregister, using: "UnregisterForUpdate()" within the OnUpdate() -event.

Looping is "risky business": if you forget to unregister (end/break the loop) it will keep looping forever and slow down your computer a lot.
I prefer using "RegisterForSingleUpdate(1)", as explained in this example:

Function StartChain()    RegisterForSingleUpdate(1) ; Give us a single update in one secondendFunctionEvent 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)    endIfendEvent

This way you will run the "OnUpdate()" only once, then check if "bkeepUpdating" == true. If it is, it runs "OnUpdate()" again, and makes another check - and continues to do so, until "bkeepUpdating" == false; which breaks the loop.
"bkeepUpdating" could be replaced with a global variable, and be controlled from several other scripts, if you wish. -Making it easier to break the loop as a "safety feature".


Sources:
http://www.creationkit.com/OnUpdate_-_Form
http://www.creationkit.com/RegisterForSingleUpdate_-_Form
http://www.creationkit.com/UnregisterForUpdate_-_Form
User avatar
James Rhead
 
Posts: 3474
Joined: Sat Jul 14, 2007 7:32 am


Return to V - Skyrim