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

Tower Building turtle

Started by UP844, 05 February 2013 - 02:47 PM
UP844 #1
Posted 05 February 2013 - 03:47 PM
I'm programming a turtle to build a tower. I could probably do it 4 times as fast myself but thats not as fun. Anyway I'm using for loops a lot. I don't want to edit the coding everytime I want to change the height of the tower. First I'm having it build a base with:

turtle.forward()
turtle.up()
for i = 1, (tower height) do
turtle.placeDown()
turtle.forward()
turtle..placeDown()
turtle.forward()

turtle.placeDown()
turtle.turnRight()
turtle.forward()
turtle.placeDown()
turtle.forward()
turtle.placeDown()
turtle.turnRight()
turtle.forward()

turtle.placeDown()
turtle.forward()
turtle.placeDown
turtle.turnRight()

turtle.forward()
turtle.placeDown()
turtle.forward()
turtle.turnRight()
turtle.up()

end

After this there is some more stuff to make the top of the tower look cooler. But is there anyway that I could replace the (tower height) with a variable and when I execute the program, have to type a number afterwards that will equal the variable? I'm just starting out on programming with Lua and I'm really bad with programming in general.
theoriginalbit #2
Posted 05 February 2013 - 03:52 PM
side ntoe: use [code][/code] tags

Something like this would do it

local args = {...} -- this gets the runtime args as long as its not in a function
local height = tonumber(args[1])
local width = tonumber(args[2])

for i = 1, height do
  for x = 1, 4 do -- we have 4 sides
	for y = 1, width - 1 do -- we need to -1 so it lines up and is the actual width
	  turtle.forward()
	  turtle.placeDown()
	  turtle.turnRight()
	end
  end
  turtle.up()
end
UP844 #3
Posted 06 February 2013 - 03:34 AM
side ntoe: use [code][/code] tags

Something like this would do it

local args = {...} -- this gets the runtime args as long as its not in a function
local height = tonumber(args[1])
local width = tonumber(args[2])

for i = 1, height do
  for x = 1, 4 do -- we have 4 sides
	for y = 1, width - 1 do -- we need to -1 so it lines up and is the actual width
	  turtle.forward()
	  turtle.placeDown()
	  turtle.turnRight()
	end
  end
  turtle.up()
end
And to run the program would it be (program name) (tower height)? And I'll edit the code thing.
UP844 #4
Posted 06 February 2013 - 03:44 AM
I'm programming a turtle to build a tower. I could probably do it 4 times as fast myself but thats not as fun. Anyway I'm using for loops a lot. I don't want to edit the coding everytime I want to change the height of the tower. First I'm having it build a base with:

turtle.forward()
turtle.up()
for i = 1, (tower height)
 turtle.placeDown()
 turtle.forward()
 turtle.placeDown()
 turtle.forward()
 turtle.placeDown()
 turtle.turnRight()
 turtle.forward()
 turtle.placeDown()
 turtle.forward()
 turtle.placeDown()
 turtle.turnRight()
 turtle.forward()
 turtle.placeDown()
 turtle.forward()
 turtle.placeDown()
 turtle.turnRight()
 turtle.forward()
 turtle.placeDown()
 turtle.forward()
 turtle.turnRight()
 turtle.up()
end
After this there is some more stuff to make the top of the tower look cooler. But is there anyway that I could replace the (tower height) with a variable and when I execute the program, have to type a number afterwards that will equal the variable? I'm just starting out on programming with Lua and I'm really bad with programming in general.
Goof #5
Posted 06 February 2013 - 04:49 AM
Well this should work:

edit… putted the code on pastebin (easier to read)
link: http://pastebin.com/9pVBGHWh
Edited on 06 February 2013 - 03:56 AM
theoriginalbit #6
Posted 06 February 2013 - 04:53 AM
And to run the program would it be (program name) (tower height)? And I'll edit the code thing.
with the program i supplied there it would be
<program name> <tower height> <tower width>

if you didn't want width you could replace line 3 with this
local width = 4
or whatever width you wish it to be.
UP844 #7
Posted 06 February 2013 - 04:32 PM
And to run the program would it be (program name) (tower height)? And I'll edit the code thing.
with the program i supplied there it would be
<program name> <tower height> <tower width>

if you didn't want width you could replace line 3 with this
local width = 4
or whatever width you wish it to be.
Wait I'm running this on an older version of Lua since im using Tekkit it says tower:5: 'for' limit must be a number. Is this because of the version of Lua I'm using?
theoriginalbit #8
Posted 06 February 2013 - 04:42 PM
Wait I'm running this on an older version of Lua since im using Tekkit it says tower:5: 'for' limit must be a number. Is this because of the version of Lua I'm using?
Not one bit… it probably means that this line
local height = tonumber(args[1])
you have actually typed out like this
local height = args[1]
yes?
UP844 #9
Posted 07 February 2013 - 11:05 AM
Wait I'm running this on an older version of Lua since im using Tekkit it says tower:5: 'for' limit must be a number. Is this because of the version of Lua I'm using?
Not one bit… it probably means that this line
local height = tonumber(args[1])
you have actually typed out like this
local height = args[1]
yes?
I did exactly what you did I've tried it several times I have no idea what's wrong.
theoriginalbit #10
Posted 07 February 2013 - 03:06 PM
I did exactly what you did I've tried it several times I have no idea what's wrong.
The code I supplied is working perfectly fine for me… run it like this and see what happens
tower 5 5
UP844 #11
Posted 08 February 2013 - 03:24 PM
I did exactly what you did I've tried it several times I have no idea what's wrong.
The code I supplied is working perfectly fine for me… run it like this and see what happens
tower 5 5
Still not working. If I put something in my cd network file in my .tekkit folder what do I have to save it as will notepad work?
ChunLing #12
Posted 08 February 2013 - 07:50 PM
Are you using this code?
side ntoe: use [code][/code] tags

Something like this would do it

local args = {...} -- this gets the runtime args as long as its not in a function
local height = tonumber(args[1])
local width = tonumber(args[2])

for i = 1, height do
  for x = 1, 4 do -- we have 4 sides
	for y = 1, width - 1 do -- we need to -1 so it lines up and is the actual width
	  turtle.forward()
	  turtle.placeDown()
	  turtle.turnRight()
	end
  end
  turtle.up()
end
There are a number of places where you could mess this up. What is your exact error?

A missing tonumber() wouldn't really matter, since both for parameters and arithmetic automatically tonumber (but it is better style to tonumber things if they're numbers). Your previous error must have been because you didn't supply all the necessary command line parameters…you can put in a default value by editing those lines like so:
local height = tonumber(args[1]) or 5 --default height value
local width = tonumber(args[2])  or 3 --default width value
That way if you omit a command line parameter (or supply something that isn't convertible to number) the program doesn't error.

Anyway, you need to post the exact code you're using and the exact error (or other unexpected behavior) you're seeing. Don't just keep saying "it doesn't work". We have no real idea what "it" is, and we have no idea what "doesn't work" means.

Looking over this code, it does indeed seem odd. It should be:
local args = {...}
local height = tonumber(args[1]) or 5
local width = tonumber(args[2]) or 3

for i = 1, height do
    for x = 1, 4 do
        for y = 1, width-1 do
            turtle.forward()
            turtle.placeDown()
        end
        turtle.turnRight()
    end
    turtle.up()
end
The other code would have produced a 2x2 tower…in time…with a lot of running around in circles (okay, tight squares).
Edited on 08 February 2013 - 06:56 PM
UP844 #13
Posted 09 February 2013 - 12:52 PM
Are you using this code?
side ntoe: use [code][/code] tags

Something like this would do it

local args = {...} -- this gets the runtime args as long as its not in a function
local height = tonumber(args[1])
local width = tonumber(args[2])

for i = 1, height do
  for x = 1, 4 do -- we have 4 sides
	for y = 1, width - 1 do -- we need to -1 so it lines up and is the actual width
	  turtle.forward()
	  turtle.placeDown()
	  turtle.turnRight()
	end
  end
  turtle.up()
end
There are a number of places where you could mess this up. What is your exact error?

A missing tonumber() wouldn't really matter, since both for parameters and arithmetic automatically tonumber (but it is better style to tonumber things if they're numbers). Your previous error must have been because you didn't supply all the necessary command line parameters…you can put in a default value by editing those lines like so:
local height = tonumber(args[1]) or 5 --default height value
local width = tonumber(args[2])  or 3 --default width value
That way if you omit a command line parameter (or supply something that isn't convertible to number) the program doesn't error.

Anyway, you need to post the exact code you're using and the exact error (or other unexpected behavior) you're seeing. Don't just keep saying "it doesn't work". We have no real idea what "it" is, and we have no idea what "doesn't work" means.

Looking over this code, it does indeed seem odd. It should be:
local args = {...}
local height = tonumber(args[1]) or 5
local width = tonumber(args[2]) or 3

for i = 1, height do
	for x = 1, 4 do
		for y = 1, width-1 do
			turtle.forward()
			turtle.placeDown()
		end
		turtle.turnRight()
	end
	turtle.up()
end
The other code would have produced a 2x2 tower…in time…with a lot of running around in circles (okay, tight squares).
First off sorry for the saying "it doesn't work" I would've specified but I didn't feel the need since it was giving me the same error report I will next time. And thank you so much the code is working now :)/>