Utility.Wait() vs RegisterForSingleUpdate()

Post » Mon Apr 04, 2016 9:51 am

Hi,



Both those functions are useful for waiting in a script.



Is there any reason I should use one instead of the other (eg : savegame stability or whatsoever) ?



I use those functions to monitor every second if an event is supposed to happen.



Red

User avatar
Stefanny Cardona
 
Posts: 3352
Joined: Tue Dec 19, 2006 8:08 pm

Post » Sun Apr 03, 2016 11:38 pm

"Wait" literally waits. Nothing in the Script after it will run until that period of time has expired.


"RegisterForSingleUpdate" will run everything after it in the Script (as appropriate), and then add the Script into the 'Queue' to be run again in the future.



Each of them has a use, and they're not always interchangeable; depending how your Script is set up, and what you want to do, one might be better than the other. For example, in one of my Scripts I play an IMOD, then use Wait(10.0) so that the first-half of the IMOD plays out before I make changes to the Player. On the other hand, another Script I have is constantly monitoring the Player's Health, and so uses 'SingleUpdate(1.0) to run the whole script every second.





What is it you're doing? Generally you should not be running a Script every second--there are times when you'll need to do that, but for the most part it should be avoided.

User avatar
Angela Woods
 
Posts: 3336
Joined: Fri Feb 09, 2007 2:15 pm

Post » Mon Apr 04, 2016 12:19 am

I use a script that monitors the distance between a NPC and the player.



Once the conditions are met, it starts a scene.



Presently the monitoring is done with RegisterForSingleUpdate(), while the scene starting is done with Wait(), because it has for example to wait for a discussion to be over before starting the scene.



I was just wondering if those functions could cause some harm :)

User avatar
Michelle Smith
 
Posts: 3417
Joined: Wed Nov 15, 2006 2:03 am

Post » Mon Apr 04, 2016 2:26 pm

From what you describe, it sounds OK to me--maybe someone else will have another comment, but that sounds like how I'd do it--use RFSU to start the scene, then Wait to hold the script until it's played. I've not worked much with dialogue/scenes, so you might be able to advance the script with a ScriptFragment on the dialogue/scene itself, but I'll leave that to someone else :P

User avatar
Glu Glu
 
Posts: 3352
Joined: Sun Apr 01, 2007 5:39 am

Post » Mon Apr 04, 2016 11:19 am

:)



Thanks for your advises CDM !

User avatar
Daniel Lozano
 
Posts: 3452
Joined: Fri Aug 24, 2007 7:42 am

Post » Mon Apr 04, 2016 3:29 pm

RegisterForSingleUpdate has the advantage of being able to update the script/functions, unlike a utility.wait loop. In the example below, if you run SomeFunction() and for some reason Bob can never get close enough to Susan, modifying the SomeFunction() function will do nothing and you'll be stuck in an infinite loop. However, if you run the OnUpdate() (you can call it directly like a function if you want), you can update the OnUpdate() function and the updated version will be run the next time OnUpdate() is called.



Actor Bob
Actor Susan

float fMinDist
float fUpdateTime

Function SomeFunction()
while Bob.GetDistance(Susan) > fMinDist
Utility.Wait(fUpdateTime)
endWhile
;do stuff
endFunction

Event OnUpdate()
if Bob.GetDistance(Susan) > fMinDist
RegisterForSingleUpdate(fUpdateTime)
else
;do stuff
endIf
endEvent


A utility.wait will also keep the script active unlike a registerforsingleupdate, so it would typically be beneficial to register for update for long running waits for performance reasons.

User avatar
Davorah Katz
 
Posts: 3468
Joined: Fri Dec 22, 2006 12:57 pm


Return to V - Skyrim