This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Agoldfish's profile picture

Making an easier TNT cannon.

Started by Agoldfish, 05 October 2013 - 09:30 AM
Agoldfish #1
Posted 05 October 2013 - 11:30 AM
Hi, I have had ComputerCraft for a while, but I mainly use it to see what OTHER people create. However I did work up the ambition to write my own code to make firing a tnt cannon easier. Here is what i have so far.


print"The TNT will now fire at the village"
redstone.setOutput("back",true)
sleep(.1)
redstone.setOutput("back",false)
sleep(6)
print"Confirmed hit!"
sleep(1.5)
term.clear()
term.setCursorPos(1, 1)
print"Type Launch to fire again"
io.read()
io.read = input
if
  input == "Launch"
then do
  redstone.setOutput("back",true)
  sleep(.1)
  redstone.setOutput("back",false)
end
else
os.reboot()
end

The problem is, when I type "Launch", it does os.reboot() instead of doing the "then" command. Can someone help? Thanks :)/>
Lyqyd #2
Posted 05 October 2013 - 03:45 PM
Split into new topic.
immibis #3
Posted 05 October 2013 - 03:49 PM

io.read()
Read a line of text. Throw away the result.


io.read = input
input is nil at this point, because nil is the default value for variables and you haven't set it to anything. This line sets io.read to nil, so that nobody else can use it to read a line of text.


if
  input == "Launch"
then
Checks if input contains "Launch", which it doesn't, because it contains nil.

Instead of this:

io.read()
io.read = input
you probably meant this:

input = io.read()

Agoldfish #4
Posted 07 October 2013 - 11:39 AM

io.read()
Read a line of text. Throw away the result.


io.read = input
input is nil at this point, because nil is the default value for variables and you haven't set it to anything. This line sets io.read to nil, so that nobody else can use it to read a line of text.


if
  input == "Launch"
then
Checks if input contains "Launch", which it doesn't, because it contains nil.

Instead of this:

io.read()
io.read = input
you probably meant this:

input = io.read()
Thank you! It worked!
jay5476 #5
Posted 08 October 2013 - 05:40 AM
just a note I don't think your understanding if statements they are like

if something then
not

if something then do
the only reason this works is because Lua (not LUA) has do statements
Agoldfish #6
Posted 08 October 2013 - 09:53 AM
just a note I don't think your understanding if statements they are like

if something then
not

if something then do
the only reason this works is because Lua (not LUA) has do statements
You seem to know what you are doing, how would I make the "Type launch to fire again" Loop until you want to stop?
Zudo #7
Posted 09 October 2013 - 12:57 PM

while true do
 -- code
 -- code
 if condition then -- Check if you want to stop
  break -- Stop
 end
end