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

[LUA] Quick Programming Question

Started by afischaa, 22 December 2012 - 12:35 PM
afischaa #1
Posted 22 December 2012 - 01:35 PM
Hey guys! I'm somewhat new to these forums so sorry in advance if this question isn't where is needs to be or if there are any flaws on how I'm required to format it.

Let me start off by saying that I'm very new to ComputerCraft and coding. ComputerCraft is the first time I actually tried to learn Lua.
I'm actually trying to make a very simple code where, all in a while loop, a mining turtle mines forward, goes forward, mines up, mines forward, goes forward, etc. The question I have is simply how to program the turtle to, every 10 or so blocks, turn right, place a block (a torch), turn back left, and continue, all of this inside the while loop (so it'll continue forever). Can someone give me a few tips or advise on how to do this? This isn't the only time I've been wanting to know how to do this. I recently tried to make a turtle bomber where is goes forward then places a block of TNT every so many blocks.

TL;DR- How do I make a turtle, every x amount of commands (turtle.forward, etc.), stop and do something else, like place a torch or place a TNT underneath itself.

Thanks for your time,
afischaa
LucasUK #2
Posted 22 December 2012 - 01:38 PM
, x = 0
while loop ,a mining turtle mines forward, goes forward, mines up, mines forward, goes forward , x = x+1, if x =10 torchstufff, set x=0


ps i replied in your language from post, its not code syntax correct etc :P/> let us know if you get stuck.
afischaa #3
Posted 22 December 2012 - 01:52 PM
Sorry about my post being not "code syntax correct". :P/> Like I said I'm new to this! Could you write out what I would put in the console?
afischaa #4
Posted 22 December 2012 - 02:05 PM
What I have:

while true do
turtle.dig()
turtle.forward()
turtle.dugUp()
end
PaxNova #5
Posted 22 December 2012 - 02:27 PM
local steps = 0
while (true) do
turtle.dig()
turtle.forward()
turtle.digUp()
steps = steps + 1
if not steps % 10 then
turtle.turnRight()
turtle.dig()

turtle.place()
turtle.turnLeft()
end
end
remiX #6
Posted 22 December 2012 - 02:27 PM
What I have:

while true do
turtle.dig()
turtle.forward()
turtle.dugUp()
end

a 'while true do' loop runs infinitely so it will never stop.

Do you want something like this?

for i = 1, 20 do
    turtle.forward()
    if math.fmod(i, 5) == 0 then    --does t his every 5
	    print(i .. " is a multiple of 5!")
	    turtle.up()
    end
end
afischaa #7
Posted 22 December 2012 - 05:56 PM
Okay, I think I'm starting to get this. Thanks guys for the help!
afischaa #8
Posted 22 December 2012 - 06:43 PM
Also, earlier I talked about making a bombing turtle. Here's what I have for that so far. Is it the easiest way? Any way to make it cooler/better?


term.clear()
print("Bombing!")
redstone.setOutput("bottom", true)
while true do
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.placeDown()
end

NDFJay #9
Posted 22 December 2012 - 07:12 PM
Also, earlier I talked about making a bombing turtle. Here's what I have for that so far. Is it the easiest way? Any way to make it cooler/better?


term.clear()
print("Bombing!")
redstone.setOutput("bottom", true)
while true do
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.forward()
  turtle.placeDown()
end


slightly shorted and better looking code would be


term.clear()
print("Bombing!")

rs.setOutput("bottom", true)
    for i=1,5 do
        turtle.forward()
    end
turtle.placeDown()
print("BOOM!")
ChunLing #10
Posted 22 December 2012 - 08:01 PM
That only goes once, afischaa wanted to do it several times (but perhaps not forever?).

Use another for loop outside the bombing sequence to make it repeat. And then make the turtle turn around and come back. Like so:
term.clear()
write("Enter number of bombs:")
local bombs = tonumber(read())
rs.setOutput("bottom", true)
for i=1,bombs do
    for i=1,5 do
        turtle.forward()
    end
    turtle.placeDown()
    print("BOOM!")
end
turtle.turnLeft()
turtle.turnLeft()
for i=1,bombs*5 do
    turtle.forward()
end
I only wrote this because I'm too lazy to explain it in English. Don't get full of yourself.
afischaa #11
Posted 24 December 2012 - 06:58 AM
That only goes once, afischaa wanted to do it several times (but perhaps not forever?).

Use another for loop outside the bombing sequence to make it repeat. And then make the turtle turn around and come back. Like so:
term.clear()
write("Enter number of bombs:")
local bombs = tonumber(read())
rs.setOutput("bottom", true)
for i=1,bombs do
	for i=1,5 do
		turtle.forward()
	end
	turtle.placeDown()
	print("BOOM!")
end
turtle.turnLeft()
turtle.turnLeft()
for i=1,bombs*5 do
	turtle.forward()
end
I only wrote this because I'm too lazy to explain it in English. Don't get full of yourself.

Wow, that's awesome. Thanks a ton!

EDIT: I just tried to copy it into a regular turtle and there seems to be a problem. It doesn't go forward. It just drops the TNT under it and says BOOM the number of times you said for it to. Did I copy it wrong? I tried twice.
remiX #12
Posted 24 December 2012 - 08:31 AM
Fuel the turtle, maybe.

turtle.refuel()
ChunLing #13
Posted 24 December 2012 - 06:04 PM
Or exit to shell and use the refuel program.

We're going to have to add a refueling loop to the beginning of every program:
while turtle.getFuelLevel() < 500 do
    print("Low Fuel Warning!")
    write("Place fuel in Slot 16 and press Enter") read()
    turtle.select(16)
    if turtle.refuel() then print("New fuel level "..turtle.getFuelLevel())
    else print("Slot 16 does not contain fuel!") end
end
afischaa #14
Posted 25 December 2012 - 07:44 AM
Thanks ChungLing, but I turned off the fuel setting. Thanks though!
ChunLing #15
Posted 26 December 2012 - 03:16 PM
Did you copy/paste the code or did you retype it?