Optimize my 10 lines of code please

Post » Wed Sep 25, 2013 10:59 am

Two first events mainly.

This is supposed to run on ReferenceAlias script attached to Player + NPCs. Should Unregister/Re-register updates on OnLoad/OnUnload (which does not get triggered for player). OnLoad happens on every loc change for followers, what really kinda forces me to make it as short as possible.

OnLoad block - should re-register for the remaining amount of time since the last OnUnload.

OnUpdateGameTime block - should check for all missing updates after fast travel or wait.

;VARSFloat fTimeRemainingFloat fTimeCurrentFloat fTimeDesiredFloat fUpdate = 1.0 ;ReadOnlyInt iCounterEvent OnLoad()	fTimeCurrent = GetHoursPassed()	If ( fTimeCurrent - fTimeDesired ) > fUpdate		fTimeRemaining = UpdateCheck()	Else			fTimeRemaining = fTimeDesired - fTimeCurrent	EndIf	fTimeDesired = fTimeCurrent + fTimeRemaining	RegisterForSingleUpdateGameTime(fTimeRemaining)	RegisterForModEvent("OnGameReload", "OnGameReload")EndEventEvent OnUpdateGameTime()	fTimeCurrent = GetHoursPassed()	fTimeRemaining = UpdateCheck() + fUpdate	fTimeDesired = fTimeCurrent + fTimeRemaining	RegisterForSingleUpdateGameTime(fTimeRemaining)EndEventEvent OnUnload()	UnregisterForUpdateGameTime()	UnregisterForAllModEvents()EndEventFloat Function UpdateCheck()	Int iMultiplier = Floor(fTimeCurrent - fTimeDesired) / fUpdate	iCounter = iMultiplier * 1 ;my value		;returns remaining value	Return fTimeDesired - iMultiplier * fUpdateEndFunctionFloat Function GetHoursPassed()	Return GameDaysPassed.GetValue() * 24EndFunction

I really hate this code. If you could make it shorter or make it look less "cheap" in general... Thanks in advance for any hints!

User avatar
Eve(G)
 
Posts: 3546
Joined: Tue Oct 23, 2007 11:45 am

Post » Wed Sep 25, 2013 1:42 pm

I'm very confused. Can you just explain in simple terms what are you trying to do? This seems overly complex if you are just trying, essentially, to update something after a certain amount of time passes. OnLoad and OnUnload may also fire unreliably. With a bit more explaination, we can probably figure out something simpler.

User avatar
mike
 
Posts: 3432
Joined: Fri Jul 27, 2007 6:51 pm

Post » Wed Sep 25, 2013 6:05 am

Well, firstly I wanted to have OnUpdateGameTime event running on player and some NPCs (followers) all the time.

Then I found that I could simply unregister OnUpdate on most NPCs that are not loaded currently, then re-register them later according to previously saved timers values, then re-calculating all missing OnUpdates that hasnt happened due to them being unregistered.

An above was about NPCs only.

Now for Player AND npcs I;m trying to catch all missing onUpdates that are getting lost after each Wait or Fast Travel. OnUpdateGameTime is unreliable here, because it pops just ONCE after each fast travel even if it should trigger 10 times according to time. So Im again catching lost updates that should have happened during fast travel.

Basically, in both cases im trying to increase my "iCounter" variable by as many times as I have lost OnUpdates (via iMultiplier value).

BTW OnLoad and OnUnload seem kinda reliable for what im trying to do. They fire exactly when I want for this purpose. so keep them.

User avatar
Clea Jamerson
 
Posts: 3376
Joined: Tue Jun 20, 2006 3:23 pm

Post » Wed Sep 25, 2013 8:39 am

Aha.

Well if all you want to do is make up for lost updates during fast travel (or lost for any other reason), you could simply store the time that you register, and then check it during the update event to see if you need to increment the iCounter an extra amount of times. Just get rid of all the onLoad onUnload stuff, this should work:

Spoiler
Float fTimeCurrentFloat fTimeDesired   ;desired interval in game hoursFloat fTimeRemainder;...registerForSingleUpdateGameTime(fTimeDesired)fTimeCurrent = getCurrentGameTime();...Event onUpdateGameTime()  float timePassed = getCurrentGameTime() - fTimeCurrent()  while timePassed    if timePassed >= fTimeDesired      timePassed -= fTimeDesired      iCounter += 1    else      fTimeRemainder += timePassed      timePassed = 0.0      while fTimeRemainder >= fTimeDesired        fTimeRemainder -= fTimeDesired        iCounter += 1      endWhile    endif  endWhile  registerForSingleUpdateGameTime(fTimeDesired)  fTimeCurrent = getCurrentGameTime()EndEvent

This will keep a running total of the fTimeRemainder across multiple update events and account for all time passed.

User avatar
-__^
 
Posts: 3420
Joined: Mon Nov 20, 2006 4:48 pm


Return to V - Skyrim