Jump to content

RegisterForUpdateGameTime Help Please.


Recommended Posts

OK, here's an updated version that should make the system activate automatically when the batteries are fully charged. It also has the option for having a limit on how long the batteries work before being drained down and need to be recharged. (Note that keeping the new HoursUntilDrained value at 0 means they never drain down.)

 

Depending on what is happening Notification messages don't always appear on screen, so it's possible that message was sent but you couldn't see it.

 

 

 

ScriptName BatteryCharger extends ObjectReference
{Control Lever for battery charging and system activation}


ObjectReference Property BattSwitch Auto
{Marker controlling effects when batteries are charged and system is operational.}

ObjectReference Property ChargeSwitch Auto
{Marker controlling effects showing the batteries are charging.}


float Property ChargeHoursRequired = 12.0 Auto
{Number of hours needed to charge the batteries. Default 12}

float Property HoursUntilDrained = 0.0 Auto
{Number of hours the batteries can provide power. The default of 0 means they never drain down.}


; these two variables keep track of any partial charge state
float ChargeStartHour = 0.0
float ChargeHoursCompleted = 0.0



State Busy
	Event OnActivate(ObjectReference akActionRef)
		; prevent double activation
	EndEvent

	Event OnUpdateGameTime()
		; prevent timeout and activation conflict
	EndEvent
EndState

State Draining
	Event OnActivate(ObjectReference akActionRef)
		; prevent activation after batteries have been fully charged and are now being used
	EndEvent

	Event OnUpdateGameTime()
		if HoursUntilDrained > 0.0
			DrainBatteries()
		endif
	EndEvent
EndState

Event OnActivate(ObjectReference akActionRef)
	FlipTheSwitch()  ; player is flipping it
EndEvent

Event OnUpdateGameTime()
	FlipTheSwitch()  ; it flips itself when the charge is complete
EndEvent


Function FlipTheSwitch()
	GoToState("Busy")
	UnregisterForUpdateGameTime()
	if ChargeStartHour == 0.0 ; Not currently charging, so record the start time
		ChargeStartHour = Utility.GetCurrentGameTime() * 24.0 ; game time in hours
	else ; Has been charging and now won't be
		ChargeHoursCompleted += Utility.GetCurrentGameTime() * 24.0 - ChargeStartHour
		ChargeStartHour = 0.0
	endif
	float HoursNeeded = ChargeHoursRequired - ChargeHoursCompleted

	if HoursNeeded <= 0.0  ; Full Charge
		Debug.Notification("Batteries are fully charged!")
		BattSwitch.Enable(); turns on sfx and fx
		; Do whatever is needed (visual effects, setting globals, etc.) when the batteries are fully charged
		if HoursUntilDrained > 0.0
			RegisterForSingleUpdateGameTime(HoursUntilDrained)
		endif
		GoToState("Draining")

	elseif ChargeStartHour > 0.0 ; Starting (or restarting) the charging process
		RegisterForSingleUpdate(HoursNeeded)
		Debug.Notification("Batteries Charging: " + (HoursNeeded) + " hours needed" )
		ChargeSwitch.Enable()
		; Do whatever is needed (visual effects, setting globals, etc.) when the batteries start to charge
		GoToState("")

	else ; Charging was paused by pulling the lever too soon
		Debug.Notification("Battery Charging Aborted: " + HoursNeeded + " more hours needed for full charge")
		ChargeSwitch.Disable()
		; Take any other action needed (visual effects, setting globals, etc.) when battery charging is interrupted
		GoToState("")
	endif
EndFunction

Function DrainBatteries()
	BattSwitch.Disable()
	; Do whatever is needed (visual effects, setting globals, etc.) to show the batteries are drained of power
	ChargeHoursCompleted = 0.0
	ChargeStartHour = 0.0
	GoToState("")
EndFunction

 

 

 

With this version the lever position isn't always going to match the battery charging state. That can be fixed but unless the visual position of the lever is critical I wouldn't bother. Since you're making changes both when the player flips the switch and at a specific time it's fairly complicated to keep everything synced up using the proper animations.

Link to comment
Share on other sites

@cdcooley

Can you not call Activate on Self when the update hits? It would still call the FlipTheSwitch function and cause the lever to change visual status. Or is there some issue with the 3D not always being loaded for that to work as expected?

I'm not sure what would happen if you have a script self activate when the object isn't in a loaded cell and so I decided to play it safe. There could be both animation and persistence issues. But yes, in theory having OnUpdate() call OnActivate(self) instead of FlipTheSwitch() might work.

Link to comment
Share on other sites

Script is great, love the idea of batteries draining. I haven't messed around with the draing part yet but will be useful in future uses. The script works completely fine as is but when battery is fully charged the sfx and fx don't activate. I understand what IsharaMeradin was getting at and I get that if a cell isn't loaded then we won't see the results.

I am more than happy with the script as it is. With a message or to, I can remind player that in 1 hour to return and the battery will be charged and the lever must be activated. Here's a video of the current operation...

https://onedrive.live.com/?cid=B60C11D037429D8E&id=B60C11D037429D8E%2134450&parId=B60C11D037429D8E%21191&o=OneUp

Link to comment
Share on other sites

  • Recently Browsing   0 members

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