Stormworks: Build and Rescue

Stormworks: Build and Rescue

Help with Lua toggle output
I am currently attempt to make a lua script that output True if i pressed and when i pressed again if will output false. I try to read the Help in the desciption menue but found out that example is only just a push button style output. So i am now confused and cannot find a way to convert the push to toogle in lua script. Any possible suggestion?
< >
1-9 / 9 のコメントを表示
Use a variable to remember the previous value and change the output only if the current state is different from the previous. I don't have any lua experience so this syntax might be wrong but it would look something like this:

boolean laststate = false
boolean button_out = false

if (button_in == true and (button ~= laststate))
then
button_out = not button_out
end

laststate = button_in
Tajin 2019年5月31日 7時56分 
yeah you got the right idea there, except it would look roughly like this:
toggle = false function onTick() touch = input.getBool(1) if touch == nil then return false end -- safety check if touch and not t then toggle = not toggle end t = touch end
Thx so much i will try it by myself.
toggle = false
function onTick()
inputX = input.getNumber(3)
inputY = input.getNumber(4)
Time = input.getNumber(1)
touch = input.getBool(1)
if touch == nil then return false end
True = toggle and isPointInRectangle(inputX, inputY, 250, 135, 20, 20)
if touch and not t then
toggle = not toggle
t = touch
end
end
function isPointInRectangle( x, y, rectX, rectY, rectW, recth)
return x > 250 and y > 135 and x < 250+20 and y < 135+20
end
function onDraw()
w = screen.getWidth()
h = screen.getHeight()
screen.setColor(0, 255, 0)
screen.drawText(5, 5, 'Time:'..Time)
screen.setColor(255, 0, 0)
screen.drawRectF(250, 135, 20, 20)
if True then
screen.drawLine(w/2, 70, w/2, 90)
screen.drawLine(134, h/2, 154, h/2)
screen.drawCircle(w/2, h/2, 10)
else
screen.drawText(90, h/2, 'Please press the button')
end
end


This is a test on my first lua it does not have a specific purpose just my first try out but i couldn't figure out how to implement your logic into mine
I think there's a problem with using "if touch == nil then return false end" in this way because if you exit the function immediately then "toggle" won't be reset to false when the touch is removed.

Maybe change that to:

if touch == nil then
toggle = false
return false
end
Tajin 2019年5月31日 12時50分 
Nope that is just to avoid running the code before the input variables are actually available (which can take a few ticks after spawning)


touch can only be nil when it isn't initialized yet. Otherwise its either true or false.
最近の変更はTajinが行いました; 2019年5月31日 12時51分
This was actually touched upon in MrN Jersey's Discord lua channel...

Here's some solutions:
Straight Code - from Carrot Cake:
Create a touch point on the Screen. The canPress is to alleviate the bouce you get back and forth between On/Off while a key is held.

if isPressed then
if canPress then
out = not out
canPress = false
end
else
canPress = true
end

Alternate solution I put forward with a JK Flip Flop outside of the lua block but still in the microcontroller:
Have a touchscreen spot that you click to toggle, output the results of that (in composite) as Boolean. In the Microcontroller housing your Lua block have a JK Flip Flop and have the output of that Boolean go in to both the set and reset at the same time. Have the result of the JK Flip Flop come back in as an On/Off channel input back to your Lua Block. The result of that input is your toggle. Every time you click on the touchscreen spot it will invert the set of the JK Flip Flop which toggles the value of the input back in to Lua. La voila, toggle in Lua.
Tajin の投稿を引用:
Nope that is just to avoid running the code before the input variables are actually available (which can take a few ticks after spawning)


touch can only be nil when it isn't initialized yet. Otherwise its either true or false.

Thanks for the explanation. I come from a C background and assumed nil == false.
Just made this myself, got this

if isPressed then
if canPress then
out = not out
canPress = false
end
else
canPress = true
end

@ElfBossHogg I think your and mine solution is the most elegant one, to whomever is scrolling through here this is the best solution.

actual code had diff variables, but I put yours because ur variables are far better named. I named canPress "t" for no reason.
最近の変更はForge_Builderが行いました; 2023年11月19日 22時27分
< >
1-9 / 9 のコメントを表示
ページ毎: 1530 50

投稿日: 2019年5月31日 6時56分
投稿数: 9