Jump to content

[LE] OnUpdateGameTime() doesn't count waiting or sleep!


TangerineDog

Recommended Posts

Store the previous game time (when you registered for the update)

Compare the new game time with the previous when the update triggers

If more than the expected count, do a bit of math to get a multiplier.

Use the multiplier to change values of whatever you are doing every so often

 

Brief example:

 

Float OldTime
Float NewTime
Float Property DesiredTime Auto
Int MyMultiplier

;some function or event
OldTime = Utility.GetCurrentGameTime()
RegisterForGameTime(DesiredTime)
;end some function or event

Event OnUpdateGameTime()
NewTime = Utility.GetCurrentGameTime()
MyMultiplier = Floor( (NewTime - OldTime) / DesiredTime)
;multiply increasing values by MyMultiplier as in
HungerCount = MyMultiplier * 1
;other stuff
EndEvent

 

Disclaimer: I've not actually tested this idea. I feel the theory is sound however.

 

I suggest testing with a simple counter that merely displays the number of game hours that passed in order to determine if it works correctly before applying to a real project.

Link to comment
Share on other sites

OnUpdateGameTime can't trigger while the game is in any MenuMode state even if game time is passing. Sleep, Wait, and Fast Travel all block it, so you need to check how much time actually passed like IsharaMeradin shows. It's also possible for a mod to directly change the current time or day so you might want to double-check how big that multiplier is before applying it. In rare cases the value might be negative if a mod does something strange!

Link to comment
Share on other sites

OnUpdateGameTime can't trigger while the game is in any MenuMode state even if game time is passing. Sleep, Wait, and Fast Travel all block it, so you need to check how much time actually passed like IsharaMeradin shows. It's also possible for a mod to directly change the current time or day so you might want to double-check how big that multiplier is before applying it. In rare cases the value might be negative if a mod does something strange!

 

It will however trigger after you exit those menus, in my experience, so if you're after just at least X time passing you're okay.

Link to comment
Share on other sites

 

It will however trigger after you exit those menus, in my experience, so if you're after just at least X time passing you're okay.

 

Yes, the fact that the update triggers as soon as the Sleep/Wait menu or loading screen completes was implied but I should probably have stated that. Thanks.

Link to comment
Share on other sites

After looking up what floor does, I, too, see the logic behind that approach.

 

I've worked it into my script like this:

 

Scriptname TimeTestScript extends activemagiceffect

GlobalVariable Property TimeCount Auto
Float Property TimeFloatThing Auto
Float OldTime
Float NewTime
Int MyMultiplier

Event OnEffectStart(Actor akTarget, Actor akCaster)

; start timer
RegisterForSingleUpdateGameTime(TimeFloatThing)
OldTime = Utility.GetCurrentGameTime()

EndEvent

Event OnUpdateGameTime()

; debug.trace(self + "OnUpdateGameTime")
NewTime = Utility.GetCurrentGameTime()
MyMultiplier = math.floor((NewTime-OldTime)/TimeFloatThing)
TimeCount.GetValue()
TimeCount.Mod(MyMultiplier)
Debug.Notification("Time increased")
RegisterForSingleUpdateGameTime(TimeFloatThing)
OldTime = Utility.GetCurrentGameTime()



EndEvent

 

but unfortunately it doesn't change the TimeCount at all.

 

Also tried it with my other script - doesn't change the global there either.

Edited by TangerineDog
Link to comment
Share on other sites

Make sure you test new changes on a new game or a save that hasn't seen the file(s) in question. There are instances where scripts and other things will not update mid-game giving you unexpected results. If it still does not work after that, I don't know.

Link to comment
Share on other sites

Make sure you test new changes on a new game or a save that hasn't seen the file(s) in question. There are instances where scripts and other things will not update mid-game giving you unexpected results. If it still does not work after that, I don't know.

 

I did. I have a clean save I test all the scripts on, the mods are 100% new every time.

Could it have something to do with the value the global should be set to being a variable, not a fixed integer?

 

Edit: Nope, just tried setting MyMultiplier to 7 instead of using math.floor((NewTime-OldTime)/TimeFloatThing) and it worked fine.

Something is wrong with this line:

 

math.floor((NewTime-OldTime)/TimeFloatThing)

 

Edit 2: waiting a day does something. I think I'll need to figure out what values NewTime and OldTime give, maybe their floor ends up being 0 if I wait less than half a day...

 

I'm using pHourstoWait in my main script and have set it to 0.4, so that HungerCount increases by 1 point every 0.4 hours for a total of 45 points over 24 hours, so (NewTime-OldTime) gets divided by 0.4.

If it happens to be less than 0.4, the floor would be 0.

I'll write something up that shows me the exact value...

 

And Edit 3: yep, that's it! I just waited for 3 hours and it got me 0.29something.

Now I'll try to come up with some workaround...

 

Last Edit: math.floor((NewTime*24-OldTime*24)/TimeFloatThing)

That way, it's completely based around hours and one hours passed - the smallest possible time to wait - nets 2,5. So far, so good.

Unfortunately, hat means that a lot of time is still gonna go unnoticed.

Edited by TangerineDog
Link to comment
Share on other sites

I think this is it.

 

This script - I'm yet to test it - should always raise HungerCount by at least 1.

 

Also, it will, if the last update happened before a full multiple of the set timer had been reached, set the next timer for the amount of time that still was on the timer.

 

It will do that by updating the time waited that's been determined by the floor-command.

 

If the floor, however, had to knock a part of the set timer down, then the timer will be set for that fraction. If the knocked down fraction was 0 - which is the case if teh timer was not interrupted - then the timer is set as usual.

 

Here it is:

 

ScriptName HungerCounterScript extends activemagiceffect

Float Property pHoursToWait Auto
GlobalVariable Property HungerCount Auto
GlobalVariable Property HungerCount01 Auto
GlobalVariable Property HungerCount02 Auto
GlobalVariable Property HungerCount03 Auto
Spell Property HungerSpell01 Auto
Spell Property HungerSpell02 Auto
Spell Property HungerSpell03 Auto
Float OldTime
Float NewTime
Int ChangeMultiplier

Event OnUpdateGameTime()

; debug.trace(self + "OnUpdateGameTime")
NewTime = Utility.GetCurrentGameTime()
ChangeMultiplier = math.floor((NewTime*24-OldTime*24)/pHoursToWait)
if ChangeMultiplier < 1
ChangeMultiplier = 1
endif
HungerCount.GetValue()
HungerCount.Mod(ChangeMultiplier)
Debug.Notification("Hunger increased")
if HungerCount.GetValue() == HungerCount03.GetValue()
Debug.Notification("You are starving.")
Game.GetPlayer().RemoveSpell(HungerSpell02)
Game.GetPlayer().AddSpell(HungerSpell03, false)
endif
if HungerCount.GetValue() == HungerCount02.GetValue()
Debug.Notification("You are hungry.")
Game.GetPlayer().RemoveSpell(HungerSpell01)
Game.GetPlayer().AddSpell(HungerSpell02, false)
endif
if HungerCount.GetValue() == HungerCount01.GetValue()
Debug.Notification("You have an appetite.")
Game.GetPlayer().AddSpell(HungerSpell01, false)
endif
if (((NewTime*24-OldTime*24)/pHoursToWait)-math.floor((NewTime*24-OldTime*24)/pHoursToWait)) > 0
RegisterForSingleUpdateGameTime((((NewTime*24-OldTime*24)/pHoursToWait)-math.floor((NewTime*24-OldTime*24)/pHoursToWait))*pHoursToWait)
else
RegisterForSingleUpdateGameTime(pHoursToWait)
endif
OldTime = Utility.GetCurrentGameTime()



EndEvent

Event OnEffectStart(Actor akTarget, Actor akCaster)

; start timer
RegisterForSingleUpdateGameTime(pHoursToWait)
OldTime = Utility.GetCurrentGameTime()

EndEvent

 

 

Edit: Just tested it - success!

Edited by TangerineDog
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...