Jump to content

How to measure how much power coming in ?


Recommended Posts

Hello.

I'm trying to measure how much power coming in to the object.

I wrote the code below, and it works, but it's too slow.

power = 1
SetValue(PowerRequired, power)
while (power < 65536 && IsPowered())
	power += 1
	SetValue(PowerRequired, power)
	Utility.Wait(0.1)
endWhile
SetValue(PowerRequired, 0)
power -= 1

Utility.Wait(0.1) is needed or loop goes overrun.

Is there way to force update power requirement and power status instead of just waiting?

Or is there more efficient way to measure how much power coming in?

Link to comment
Share on other sites

I'd say there's more efficient ways, just from a mathematical standpoint. Might make the script longer, but it would be faster. Might try doubling the power required every loop until the thing shuts off, then knocking it back by 1 until it's powered again. Also, I think you can use a smaller wait than 0.1, but don't quote me. There might be even more efficient methods, but that would require me to google stuff. :tongue:

power = 1
While IsPowered()
	power = power * 2
	SetValue(PowerRequired, power)
	Utility.Wait(0.01)
EndWhile
While !IsPowered()
	power -= 1
	SetValue(PowerRequired, power)
	Utility.Wait(0.01)
EndWhile
SetValue(PowerRequired, 0)
Edited by GenghisKhanX
Link to comment
Share on other sites

Thank you for the suggestion.

 

There's some efficient ways from a mathematical standpoint, indeed.

But sticking on SetValue(PowerRequired) - Wait - IsPowered method, I couldn't make them fast enough.

 

Now I found a way to calculate how much power connected maybe faster like below.

float fpower = 0.0
ObjectReference[] possibleGenerators = workshop.GetWorkshopResourceObjects( PowerGenerated )
int i = 0
while (i < possibleGenerators.length)
	if ( HasSharedPowerGrid( possibleGenerators[i] ))
		fpower += possibleGenerators[i].GetValue( PowerGenerated )
	endIf
	i += 1
endWhile
Link to comment
Share on other sites

The first code snippet actually works?! Has Bethesda actually bothered to fix that?!

Last time I've tried increasing power requirements via SetValue, it didn't count as unpowered until I dis- and reconnected one of the wires, or flipped a switch somewhere.

 

In the last code snippet, you are lacking the consumers in the same power grid. Maybe there is a way to get all objects in the current power grid, and iterate through them all?

Or, try getting an array with

 

ObjectReference[] possibleGenerators = workshop.GetWorkshopResourceObjects( PowerRequired )

And then substract it's value from your fpower.

Link to comment
Share on other sites

The first code snippet actually works?! Has Bethesda actually bothered to fix that?!

Last time I've tried increasing power requirements via SetValue, it didn't count as unpowered until I dis- and reconnected one of the wires, or flipped a switch somewhere.

 

No, it is not fixed :laugh: . You still have to do the switch-limbo... If it is an activator you may try SetOpen(true)/SetOpen(false) which actually is the same as turning a switch off/on.

Link to comment
Share on other sites

In terms of speed, the thing to do, I think, would be to chunk the math. That is, work from the largest place value, in this case 10,000, and then move on to the next lower place value, etc. It should be somewhat faster, especially at edge cases.

 

Something like this (note I haven't compiled this or checked that it works at all):

Int Function GetRequiredPower(ObjectReference powobj)
	int requiredpower = 0
	int incrementpower = 10000
	While (incrementpower > 0)
		requiredpower += incrementpower
		powobj.SetValue(PowerRequired, requiredpower)
		
		powobj.SetOpen(false)		
		Utility.Wait(0.01)	
		powobj.SetOpen(true)		
		
		if (IsPowered() == false)
			requiredpower -= incrementpower
			if (incrementpower == 1)
				incrementpower = 0
			endif
			if (incrementpower == 10)
				incrementpower = 1
			endif			
			if (incrementpower == 100)
				incrementpower = 10
			endif			
			if (incrementpower == 1000)
				incrementpower = 100
			endif			
			if (incrementpower == 10000)
				incrementpower = 1000
			endif									
		endif		
	EndWhile
	powobj.SetValue(PowerRequired, 0)	
	return requiredpower
endFunction
Edited by Reneer
Link to comment
Share on other sites

First code worked for me.

I tried setOpen on a switch on the power line and/or power measuring object itself, but it didn't update power status. I had to use Utility.Wait for the update.

I forgot about consumers, thank you.

I know some mathematical way like binary search, but "SetValue(PowerRequired) - Wait - IsPowered" tactics cannot be so fast due to its Utility.Wait , so I gave up this tactics.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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