167 posts
Posted 29 July 2012 - 01:29 AM
I'm new to programming, and I need some help re-writing a bit of code.
I wrote this code to toggle a variable on a redstone input, but is there anyway else to do it that's a lot more simpler, and reliable?
while true do
shell.run("clear")
print(toggle)
os.pullEvent("redstone")
shell.run("clear")
toggle = 1 - toggle
print(toggle)
sleep(1)
os.pullEvent("redstone")
shell.run("clear")
toggle = 1 - toggle
print(toggle)
sleep(1)
end
8543 posts
Posted 29 July 2012 - 02:00 AM
Why not just use an actual boolean instead of 1 or 0? Change "side" to whichever side you want to accept input on.
local toggle = false
while true do
os.pullEvent("redstone")
toggle = rs.getInput("side")
if toggle then
print("true")
else
print("false")
end
end
1604 posts
Posted 29 July 2012 - 02:02 AM
local toggle = false
while true do
term.clear()
term.setCursorPos(1, 1)
print(toggle)
os.pullEvent("redstone")
toggle = not toggle
end
EDIT: ninja'd :ph34r:/>/>
8543 posts
Posted 29 July 2012 - 02:04 AM
I couldn't remember whether or not the print function would complain about being passed a boolean instead of a string.
167 posts
Posted 29 July 2012 - 02:06 AM
Why not just use an actual boolean instead of 1 or 0? Change "side" to whichever side you want to accept input on.
local toggle = false
while true do
os.pullEvent("redstone")
toggle = rs.getInput("side")
if toggle then
print("true")
else
print("false")
end
end
That code works as well, thank you. But now it's an issue of using a button, the redstone "sticks" and the code is able to loop through it twice in the amount of time the redstone stays on. I know there's physical ways to get around that (Redstone Pulser) But I'd rather do it inside the code. I got around it using the sleep() function, but it's not exactly reliable.
1604 posts
Posted 29 July 2012 - 02:09 AM
I couldn't remember whether or not the print function would complain about being passed a boolean instead of a string.
It converts anything to a string, since it uses tostring on every parameter. The only thing that it doesn't print is nil (and everything after a nil), since it stops when there's one.
Why not just use an actual boolean instead of 1 or 0? Change "side" to whichever side you want to accept input on.
local toggle = false
while true do
os.pullEvent("redstone")
toggle = rs.getInput("side")
if toggle then
print("true")
else
print("false")
end
end
That code works as well, thank you. But now it's an issue of using a button, the redstone "sticks" and the code is able to loop through it twice in the amount of time the redstone stays on. I know there's physical ways to get around that (Redstone Pulser) But I'd rather do it inside the code. I got around it using the sleep() function, but it's not exactly reliable.
os.pullEvent("redstone") will make it wait for a change in redstone (turn on or off), so it can't loop twice with the same "pulse", unless there's another input that changes.
167 posts
Posted 29 July 2012 - 02:16 AM
I couldn't remember whether or not the print function would complain about being passed a boolean instead of a string.
It converts anything to a string, since it uses tostring on every parameter. The only thing that it doesn't print is nil (and everything after a nil), since it stops when there's one.
Why not just use an actual boolean instead of 1 or 0? Change "side" to whichever side you want to accept input on.
local toggle = false
while true do
os.pullEvent("redstone")
toggle = rs.getInput("side")
if toggle then
print("true")
else
print("false")
end
end
That code works as well, thank you. But now it's an issue of using a button, the redstone "sticks" and the code is able to loop through it twice in the amount of time the redstone stays on. I know there's physical ways to get around that (Redstone Pulser) But I'd rather do it inside the code. I got around it using the sleep() function, but it's not exactly reliable.
os.pullEvent("redstone") will make it wait for a change in redstone (turn on or off), so it can't loop twice with the same "pulse", unless there's another input that changes.
You're Right. I need to get around it using the Redstone signal turning off, as a redstone activity. Because I turn the button on, it toggles the variable, but then when the button turns off, it toggles the variable again, (like it's supposed to I guess)