Jump to content

Script Compiles, Doesn't Work as Intended (noob)


CompGuyJMB

Recommended Posts

OIC, in this case you have the ObjectID, fine. Though the generator is still not emitting onPowerOn/onPowerOff events.

You may register for "onActivation" events. There will be an event if the generator is switched on or off, there is no difference. But you can check, if the generator is running:

ObjectReference Property PowerSource Auto
ActorValue Property PowerGenerated Auto Const
{ 0000032E }


Event onInit()
    RegisterForRemoteEvent (PowerSource, "onActivate")
EndEvent

Event ObjectReference.OnActivate(ObjectReference akSender, ObjectReference akActionRef)
    if (PowerSource.GetValue(PowerGenerated) > 0.0)
         ; generator is running
    Else
         ; generator is just wasting space
    EndIF
EndEvent
Link to comment
Share on other sites

  • Replies 57
  • Created
  • Last Reply

Top Posters In This Topic

Hey Guys,

 

I've pasted a link to my ESP file from Dropbox. I hope it linked OK, I've never share a file through Dropbox. I didn't see how to directly upload a file here.

 

I stripped out all of the extra stuff.

 

The cell name is JMBBunker. Entrance is behind the Player Home in Sanctuary Hills.

 

Dropbox Link: https://www.dropbox.com/s/3lxifp1pnnz44i8/JMB.esp?dl=0

 

Thanks again for the help!

Link to comment
Share on other sites

Ok, last time for tonight...

Â

Below is the new version of my script. It's a mashup of everyone's suggestions.

Â

I think it's working better (based on the debug messages). However, the PowerSource.GetValue(PowerGenerated) value never changes. Does this variable only return what the Generator is SET to output, and not what it is CURRENTLY outputting? I can probably change it with SetValue- but I think that would be a script on the Generator. I can't seem to get OnPowerOn, OnPowerOff, or IsPowered to work on the remote call (it never returns a value that changes the state in my script), so that must be for a device and not a generator?

Scriptname JMBBunkerCheckForPower extends ObjectReference

{Checks to see if PowerSource is on before allowing DeviceToPower to turn on}

Â

ObjectReference Property DeviceToPower Auto

{Device to Turn On / Off}

Â

ObjectReference Property PowerSource Auto

{The Power Source Device, NOT Enable Marker}

Â

ActorValue Property PowerGenerated Auto Const

Â

Event OnInit()

RegisterForRemoteEvent (PowerSource, "OnActivate")

Â

If (PowerSource.GetValue(PowerGenerated) > 0.0)

GotoState ("PowerIsOn")

Else

GotoState ("PowerIsOff")

EndIf

EndEvent

Â

State PowerIsOff

Event OnBeginState(string asOldState)

Debug.MessageBox ("Entered PowerIsOff State")

Debug.MessageBox ("PowerGenerated is " + PowerSource.GetValue(PowerGenerated))

DeviceToPower.BlockActivation(true)

EndEvent

Â

Event OnActivate(ObjectReference akActionRef)

Debug.MessageBox ("The Power Source is Off")

EndEvent

  Â

Event ObjectReference.OnActivate(ObjectReference akSender, ObjectReference akActionRef)

Debug.MessageBox ("Entered OnActivate Event")

If (PowerSource.GetValue(PowerGenerated) > 0.0)

GotoState ("PowerIsOn")

EndIf

EndEvent

EndState

Â

State PowerIsOn

Event OnBeginState(string asOldState)

Debug.MessageBox ("Entered PowerIsOn State")

Debug.MessageBox ("PowerGenerated is " + PowerSource.GetValue(PowerGenerated))

DeviceToPower.BlockActivation(false)

EndEvent

Â

 Event OnActivate(ObjectReference akActionRef)

Debug.MessageBox ("Enjoy Your Device!")

EndEvent

  Â

Event ObjectReference.OnActivate(ObjectReference akSender, ObjectReference akActionRef)

Debug.MessageBox ("Entered OnActivate Event")

If (PowerSource.GetValue(PowerGenerated) > 0.0)

GotoState ("PowerIsOn")

EndIf

EndEvent

EndState

Â

; Empty state placeholders may be required for remotes.

Event ObjectReference.OnActivate(ObjectReference akSender, ObjectReference akActionRef)

EndEvent

 

 

Edit: One last thought - is there a variable I can check to see how much power the entire workshop is generating? I can use that as my PowerIsOn check, and skip the PowerSource device altogether. If the entire workshop output is over 1 let's say, then I know the generator is on.

Edited by CompGuyJMB
Link to comment
Share on other sites

Finally got it it work. I have to use a timer to periodically check if the generator is on, but I'm happy for now. Script posted below if anyone else has this issue.

 

Thanks again BigAndFlabby and deadbeeftffn for getting me started!

Scriptname JMBBunkerCheckForPower extends ObjectReference
{Checks to see if PowerSource is on before allowing DeviceToPower to turn on}

ObjectReference Property DeviceToPower Auto
{Device to Turn On / Off}

ObjectReference Property CellWorkshop Auto
{Link this to the cells Workbench}

; Needed for the “CurrentPowerGeneration” variable to get its value
WorkshopParentScript Property WorkshopParent Auto Const
{Set this to WorkshopParent}

; Create a Timer that when it expires, runs function IsPowerOn
; This means that there will be a constant check for power in the workshop
; This Timer will re-create itself when function IsPowerOn runs.
; Only doing this until an OnWorkshopRecalculateResources event is found so I can determine if Workshop Power changes to zero.

int TimerLength = 10 ; Timer Length in Seconds
int TimerIDNumber = 25 ; ID number of the timer

;=============================================
; EVENTS
;=============================================

Event OnCellAttach()
; When Player enters cell
	Utility.Wait(2.0) ; Wait 2 seconds to give the cell some time to load.  Cannot use OnLoad or OnCellLoad - not fired everytime cell is entered, or OnInit - only fires when ESP is loaded.
	IsPowerOn()
EndEvent

Event OnTimer (int aiTimerID)
; This Event runs when the timer expires
	If (aiTimerID==TimerIDNumber)
		IsPowerOn()
	EndIf
EndEvent

Event OnActivate(ObjectReference akActionRef)
	If (DeviceToPower.IsActivationBlocked())
		Debug.MessageBox("Requires Power")
	EndIf
EndEvent

;=============================================
; Function IsPowerOn
;=============================================

bool Function IsPowerOn()
	; Retrieve the Current Power Output in the Workshop
	float CurrentPowerGeneration = CellWorkshop.GetValue(WorkshopParent.WorkshopRatings[WorkshopParent.WorkshopRatingPower].resourceValue)
	
	If (CurrentPowerGeneration > 1.0)
		;There is power present in the settlement.  Want at least 1 in case of “trace” power sources
		DeviceToPower.BlockActivation (false)
		;=============================== Requires DeviceToPower.Activate(Game.GetPlayer(), true) to actually turn device on.  True Blocks firing of another OnActivate() event
		; Debug.MessageBox("Power Is Present.  It is: " + CurrentPowerGeneration)
		
		StartTimer(TimerLength, TimerIDNumber)
		Return True
	Else
		; There is NO power present in the settlement
		DeviceToPower.BlockActivation (true)
		; Debug.MessageBox("Power Is Off")
		
		If (DeviceToPower.IsRadioOn())
			DeviceToPower.Activate(Game.GetPlayer(), true)  
		EndIf
		
		StartTimer(TimerLength, TimerIDNumber)
		Return False
	EndIf
EndFunction
Edited by CompGuyJMB
Link to comment
Share on other sites

You were trying to grab the events check IsPowered on the PowerSource/generator, when you should have been grabbing them checking IsPowered on the DeviceToPower. The previous mentions about using self and the generators not receiving those events are correct. The good news is that you can dump all of the generator / workshop / infinite X unit timer code.

 

Edit: The following script should be attached to the radio, not the generator. (I've been awake for 24 hours, but I think I read that was your original goal.)

 

Edit 2: Radios don't require wires/power. If this doesn't work, then radios probably don't get power events or radio IsPowered is always true (?!)

Scriptname JMBBunkerCheckForPower extends ObjectReference

; "Self" is whatever script is attached to, so no need for DeviceToPower property.

; However if you decide to attach script to a different object or a utility quest,
; then you will need DeviceToPower property and RegisterForRemoteEvent(s).

Event OnInit()
    Bool HasPower = Self.IsPowered()                ; Check status OF THE RADIO OBJECT the first time.
    
    If !HasPower                                    ; If generator/wires are not connected OnInit,
        Self.BlockActivation()                      ; flag and let events do heavy lifting.
    EndIf
EndEvent

Event OnPowerOn(ObjectReference akPowerGenerator)   ; The generator/wires started working, no other checks needed...
    Self.BlockActivation(False)                     ; unblock activation.
EndEvent

Event OnPowerOff()                                  ; The generator/wires failed, no other checks needed...
    Self.BlockActivation()                          ; block activation.
EndEvent


Event OnActivate(ObjectReference akActionRef)
    If Self.IsActivationBlocked()
        Debug.MessageBox("Requires Power")
    Else
        ; Everything is fine so normal activation behavior occurs.
    EndIf
EndEvent

Link to comment
Share on other sites

I was thinking the OnPowerOn event was fired when an object was turned on. It looks like (based on the above) that event means if the device has power (available)? I was fighting with that for a while!

 

If I read it correctly, your script would still let a radio turn on if there is no generator turned on in the cell. I was looking to make that not possible for immersion reasons.

 

Thanks so much for the input, I like the "Self" reference - much cleaner.

Link to comment
Share on other sites

I was thinking the OnPowerOn event was fired when an object was turned on. It looks like (based on the above) that event means if the device has power (available)? I was fighting with that for a while!

 

If I read it correctly, your script would still let a radio turn on if there is no generator turned on in the cell. I was looking to make that not possible for immersion reasons.

 

Thanks so much for the input, I like the "Self" reference - much cleaner.

When you tunr the object on, only OnActivate is fired. You can check different things in this event. OnPowerOn fires when you provide the required power to the object either by connecting it to a generator or by building a generator nearby ( also when you repair the broken generator that provides power to the object).

Edited by kitcat81
Link to comment
Share on other sites

If I understand correctly the only your goal is to make radio depended on power. Then you can add a keyword to your radio object in the CK ' WorkshopCanBePowered' and attach something like this :

 

RadioScript:

Bool myStateOn

Event OnInit()
If !IsPowered() ;check if we don't recieve any power
    BlockActivation(true)
EndIf
EndEvent

Event OnPowerOn(ObjectReference akPowerGenerator)
  BlockActivation(false)
    If myStateOn ==true ; check if we were on when power went off 
        SetRadioOn(true)
        myStateOn = false 
    EndIf    
EndEvent

Event OnPowerOff()
    BlockActivation(true)
    if  IsRadioOn() ;check if the radio is playing 
        SetRadioOn(false) ; stop playing
        myStateOn = true ;  save  our state 
    EndIf
EndEvent
Edited by kitcat81
Link to comment
Share on other sites

Thanks ThoraldGM, I had just assumed that the timer would cancel itself when the cell unloads. Thanks for the tip, I will have to add that!

 

Kitkat81, that is a much more straightforward way of doing things, and avoids the timer all together. I was worried about what kind of performance impact a timer would have. I definitely want to take a look at that. I looked around, have you found a source that explains what some of the different keywords mean / how to use them? Creation kit documentation is somewhat lacking, IMO.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.

×
×
  • Create New...