5 posts
Posted 03 October 2014 - 09:15 AM
Hello lovely people,
I am trying to make a program that sends a redstone pulse out of the back of the computer with a 2 second delay until told to stop by a redstone signal from the front. Here is the code that I tried to piece together but it doesn't work:
while true do
while redstone.getInput(front) do
redpulse back 10 2
sleep(2)
end
I am sure this is totally wrong so please go easy on me! I have never done coding other than HTML/CSS before!
Thanks
3057 posts
Location
United States of America
Posted 03 October 2014 - 01:17 PM
Indenting only works with
tags.
Here's my contribution, from what I understand.
while not rs.getInput( "front" ) do
rs.setOutput( "back", true )
sleep( 2 )
rs.setOutput( "back", false ) --#oops
sleep( 2 )
end
Edited on 03 October 2014 - 04:13 PM
5 posts
Posted 03 October 2014 - 04:31 PM
Thanks for your reply. I tired your code and it half works.
If there is a redstone signal going to the front it runs and does nothing (as intended).
If there is no redstone signal in the front then it returns "attempt to index ? (a nil value)"
Any ideas?
7508 posts
Location
Australia
Posted 03 October 2014 - 05:07 PM
for future reference, you should always post the full error message, it contains vital information such as the line number the error has occurred on. that particular error happens when you're trying to call a function in a table that is nil, the most common reason for this is typos, or accidentally overriding the value. however since you said it works until it gets a signal, make sure the calls to the rs api within the loop don't have typos.
5 posts
Posted 03 October 2014 - 05:47 PM
Thanks, I wondered if a typo was a causing it and checked but didn't see one. I just had another look and found it. The program now works, except it sends a constant redstone signal out of the back, instead of pulsing. :(/>
3057 posts
Location
United States of America
Posted 03 October 2014 - 06:14 PM
Thanks, I wondered if a typo was a causing it and checked but didn't see one. I just had another look and found it. The program now works, except it sends a constant redstone signal out of the back, instead of pulsing. :(/>
Fixed it, I had true instead of false in the second statement. Edited post with updated code.
5 posts
Posted 05 October 2014 - 12:24 PM
Thanks! It works perfectly!
I have a question about how it works though if you don't mind answering. Does work like this:
Line 1 - Check if there is a redstone signal in the front, if there isn't go to line 2
Line 2 - Send redstone signal out the back
Line 3 - Wait 2 seconds
Line 4 - Turn off the redstone signal at the back.
Line 5 - Wait 2 seconds
if this is right where does the loop back to the start come from? Why doesn't it just step through each line until it reaches the end and then stop (when there is no redstone signal coming to the front)?
Thank you again so much for your help! My blaze farmer is working nicely now :)/>
3057 posts
Location
United States of America
Posted 05 October 2014 - 01:33 PM
if this is right where does the loop back to the start come from? Why doesn't it just step through each line until it reaches the end and then stop (when there is no redstone signal coming to the front)?
Yes, it is correct! Here's how the loop works
Line 1 - Check if there is rs signal from the front, if not skip rest of code and end
Line 6 - end closing the while loop. Go back to Line 1
Here's a link that explains it as well:
http://www.lua.org/pil/4.3.2.html
5 posts
Posted 07 October 2014 - 04:59 PM
Thank you so much for all your help! That explains it nicely!