Stormworks: Build and Rescue

Stormworks: Build and Rescue

(LUA If Statements) Is It Just Me
or do LUA scripts start to fail when if statements have a lot of operands/parameters?

I tried to write a script that took several on/off inputs and a few number inputs to determine if my engine starters needed to be on or if the PID needed turned on. I even had the script to control the lever/throttle. There were statements that clearly should have failed (for example, the lack of a certain button input), yet the code within the if statement still executed. Every single logic output it was in charge of pressing was pressed all at once.

It's like the script ignored the actual if statement and just executed every single variable write it found in the entire script. This isn't a post-update thing either. This has been happening to me pretty much since I started playing. I have been using LUA scripts sparingly, relying almost completely on actual logic gates to perform complex tasks.
< >
Showing 1-9 of 9 comments
GrumpyOldMan Oct 22, 2023 @ 2:01am 
Most likely caused by your script rather than lua itself, an example would go a long way.

You can also use some sort of simplified state machine and use tables for states, if you have more complex conditions to check, helps with readability and convenience.

An engine controller would be a go-to example to make use of such a state machine, since an engine can only have a single state at any time (off, starting, idle, running, generator mode etc.) you'd set functions for each state in a table and go from there, instead of having tangled and intertwined nested if-statements.
Captain Oveur Oct 22, 2023 @ 3:55am 
I happen to still have the full script:
function onTick() rps = input.getNumber(1) fuel = input.getNumber(2) throt = input.getNumber(3) manMode = input.getBool(1) genOn = input.getBool(2) bat = input.getBool(3) starter = 0 up = 0 down = 0 on = 0 --If generator is supposed to be on if (manMode == 0 and bat == 1 and fuel > 0) or (manMode == 1 and genOn == 1) then on = 1 end --Adjust throttle to starting position (if starting) if on == 1 and manMode == 0 and throt < .10 and rps < 2.5 then up = 1 elseif on == 1 and manMode == 0 and throt > .13 and rps < 2.5 then down = 1 end --Start engine if not running if on == 1 and rps < 2.5 then starter = 1 end --If running and automatic, send throttle to full if on == 1 and rps > 2.5 and manMode == 0 then up = 1 end --Stop the engine if on == 0 and rps > 0 then down = 1 end output.setBool(1, up) output.setBool(2, down) output.setBool(3, starter) end
GrumpyOldMan Oct 22, 2023 @ 5:07am 
Just took a quick glance, seems you use output.setBool with numbers, which won't work.
First argument is the channel number, second one is required to be boolean (false/true), since lua does it differently than other languages and 0 is interpreted as true (or something among the lines).
Last edited by GrumpyOldMan; Oct 22, 2023 @ 5:08am
ShizNator Oct 22, 2023 @ 5:39am 
Not having hands on and visual as to what is controlling/being controlled. But with what you gave I cleaned up your code and added some missing turn offs. And moved your variable checks out of onTick so they remain static and not deactivate every tic.

And as Grumpy stated on/offs must be true or false but you can use an alias like I show below.
T=true F=false starter = F up = F down = F on = F function onTick() rps = input.getNumber(1) fuel = input.getNumber(2) throt = input.getNumber(3) manMode = input.getBool(1) genOn = input.getBool(2) bat = input.getBool(3) --If generator is supposed to be on if not manMode and bat and fuel > 0 or manMode and genOn then on = T else on = F end --Adjust throttle to starting position (if starting) if on and not manMode and throt < .10 and rps < 2.5 then up = T elseif on and not manMode and throt > .13 and rps < 2.5 then down = T end --Start engine if not running if on and rps < 2.5 then starter = T else starter = F end --If running and automatic, send throttle to full if on and rps > 2.5 and not manMode then up = T end if throt >= 1 then up= F end --Stop the engine if not on and rps > 0 then down = T end if throt <= 0 then down= F end output.setBool(1, up) output.setBool(2, down) output.setBool(3, starter) end
Captain Oveur Oct 22, 2023 @ 5:44pm 
Originally posted by GrumpyOldMan:
Just took a quick glance, seems you use output.setBool with numbers, which won't work.
First argument is the channel number, second one is required to be boolean (false/true), since lua does it differently than other languages and 0 is interpreted as true (or something among the lines).
Okay, but take the following example. It was the first iteration and produced a warning similar to "attempt to perform bitwise operation on a boolean value":
--If generator is supposed to be on if (~manMode and bat and fuel > 0) or (manMode and genOn) then on = 1 end

It only threw that error for the "~manMode" part. That iteration of the script, I know I no longer have a copy of.

[EDIT]
Here's a gently modified version of the script I tried to make layed out in logic gate format. I'll go back over it eventually and replace a lot of it with boolean function blocks (I keep forgetting those exist).
https://steamcommunity.com/sharedfiles/filedetails/?id=3058432293
Last edited by Captain Oveur; Oct 22, 2023 @ 6:00pm
Captain Oveur Oct 22, 2023 @ 5:46pm 
Originally posted by ShizNator:
And moved your variable checks out of onTick so they remain static and not deactivate every tic.
Wait, you can do that? I was under the impression the whole entire script was refreshed on each tick.

[edit]
Well, I'll be danged.
Last edited by Captain Oveur; Oct 22, 2023 @ 5:52pm
GrumpyOldMan Oct 22, 2023 @ 9:12pm 
Originally posted by Captain Oveur:
Okay, but take the following example. It was the first iteration and produced a warning similar to "attempt to perform bitwise operation on a boolean value":
--If generator is supposed to be on if (~manMode and bat and fuel > 0) or (manMode and genOn) then on = 1 end

It only threw that error for the "~manMode" part. That iteration of the script, I know I no longer have a copy of.
That error is to be expected, since you're using a bitwise operator on a boolean value, as the error message states. You can use "not" to check for false boolean:
--If generator is supposed to be on if (not manMode and bat and fuel > 0) or (manMode and genOn) then on = 1 end
Captain Oveur Oct 22, 2023 @ 10:26pm 
So '~' is an operator and not a conditional? I'll blame the internet for that one because I looked it up before using it.
The author of this thread has indicated that this post answers the original topic.
GrumpyOldMan Oct 22, 2023 @ 10:33pm 
SW is using lua 5.3, feel free to use this one for reference:
https://www.lua.org/manual/5.3/manual.html
< >
Showing 1-9 of 9 comments
Per page: 1530 50

Date Posted: Oct 21, 2023 @ 6:18pm
Posts: 9