15 posts

Location
Pacific Northwest

Posted 11 December 2017 - 07:19 AM

what am i doing wrong? i want to set up a reactor to run at fifty percent out put if the batter if half full, 70 percent if at 30 percent, 20 percent if at 80 percent and so on… any tips?

local percent = (math.min(bat1.getEnergyStored("unknown"))/(bat1.getMaxEnergyStored("unknown"))*100)

–this is basically deviding the current batter level by the max battery level to calculate what % full the battery is

if percent >= 95 then

reactor1.setAllControlRodLevels(100)

end

if percent > 90 and not percent < 94 then

reactor1.setAllControlRodLevels(90)

end

if percent > 89 and not percent < 80 then

reactor1.setAllControlRodLevels(80)

end

and so on and so on… but i cant get the code to run. i keep getting an error message saying attempt to compar __lt on boolean and number

end

102 posts

Location
Alone in the dark, looking at the pretty lights with dreams of things I can not have.

Posted 11 December 2017 - 10:11 AM

My advice is to put everything inside parentheses. Not sure if it will help but I always do it for readability so if (percent > 89) and (not (percent < 80 then))

Other than that you are using math.min wrong. To quote the manual:

```
math.min , math.max
Return the minimum or maximum value from a variable length list of arguments.
> = math.min(1,2)
1
> = math.min(1.2, 7, 3)
1.2
> = math.min(1.2, -7, 3)
-7
> = math.max(1.2, -7, 3)
3
> = math.max(1.2, 7, 3)
7
```

So that's going to be the issue really.

62 posts

Location
Sweden

Posted 11 December 2017 - 12:48 PM

I think you want math.floor and not math.min and I never seen get(Max)EnergyStored take any arguments

```
local bat1max = bat1.getMaxEnergyStored() -- constant value, set this once
local percent = math.floor((bat1.getEnergyStored()/bat1max)*100)-
```

Edited on 11 December 2017 - 11:52 AM

3057 posts

Location
United States of America

Posted 11 December 2017 - 01:18 PM

```
not percent < 80
```

is the problematic code.

"not" has a higher order of precedence than a comparison operator, which means that expression is equivalent to

```
(not percent) < 80
```

percent is a "truthy" value (a number), so this ends up being

```
false < 80
```

..which makes no sense, and promptly throws an error.

Instead, I recommend using

```
percent >= 80
```

…for the same (intended) effect, but without the precedence error.

1220 posts

Location
Earth orbit

Posted 11 December 2017 - 04:04 PM

OK I did a goof - I got distracted and accidentally upvoted all three answers instead just Stekelblad's and KoGY's (no offence to Purple as the answer regarding parentheses is technically correct).

Stekeblad has you covered with math.floor instead of math.min - that'll get you the percent you're after.

While Purple's answer (using parentheses) is correct and will solve the comparing lt on a bool and a number error, I'd recommend instead following KingofGamesYami's advice and change your not statements to greater than or equals statements. IMO, it makes the code easier to read (and type - especially since you're not typing all those extra parentheses) and it seems to be the intended way of doing things in Lua.