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

Variables not adding

Started by uior, 19 April 2017 - 05:28 AM
uior #1
Posted 19 April 2017 - 07:28 AM
I tried to make a turtle that would let the user specify a rectangle's dimensions, then the turtle would replace all blocks beneath it with a desired block. I'm still a bit new to this, but I can't see what is going wrong.


turtle.select(1)
print("Slots 1- 12 hold placing materials.")
print("Slots 13 - 15 must have torches.")
print("Slot 16 holds the material type to place")
print("\nEnter the forward value:")
x = read()
print("\nEnter the sideways value:")
y = read()

inventoryID = 1
depth = 0
turnNum = 0
torchID = 1

repeat

turtle.select(inventoryID)

–Ensuring materials are present–
if turtle.getItemDetail(inventoryID) == turtle.getItemDetail(16) then
if inventoryID == 13 then
print("Out of materials.")
break
end
end

–Ensuring the select switches when needed–
if turtle.getItemDetail(inventoryID) == turtle.getItemDetail(15) then
inventoryID = inventoryID + 1
turtle.select(inventoryID)
end

if turtle.detectDown() == true then
turtle.digDown()
turtle.select(15)
turtle.drop()
turtle.select(inventoryID)
end

turtle.placeDown()
turtle.forward()
depth = depth + 1

if (depth % 6) == 0 then
if (turnNum % 6) == 0 then
turtle.placeDown()
turtle.select(torchID)

if (turtle.getItemCount) == 1 then
torchID = torchID + 1
end
end
end

if depth == (x) then
if (turnNum % 2) == 1 then
turtle.placeDown()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
depth = 0
elseif (turnNum % 2) == 0 then
turtle.placeDown()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
depth = 0
end
turnNum = turnNum + 1
end

until turnNum == y
Exerro #2
Posted 19 April 2017 - 09:51 PM
Your x and y values are strings, so the comparison with depth later on will never return true as you're checking if a string is equal to a number. Also, when you're comparing the item detail, that will always return false as you're comparing two different table references. You want to compare something like the names. There's also a case where you reference turtle.getItemCount but don't call it.

Try fixing these things then say if it works?
uior #3
Posted 20 April 2017 - 01:44 AM
Thanks, the help is very much appreciated. This is what I ended up with, and it works fairly well so far, though I plan to improve it still.

turtle.select(1)
print("Slots 1- 12 hold placing materials.")
print("Slots 13 - 15 must have torches.")
print("Slot 16 holds the material type to place")
print("\nEnter the forward value:")
x1 = read()
print("\nEnter the sideways value:")
y1 = read()

x = tonumber(x1)
y = tonumber(y1)

inventoryID = 1
depth = 0
turnNum = 0
torchID = 1

repeat

turtle.select(inventoryID)

–Ensuring materials are present–
if turtle.getItemDetail(inventoryID) == turtle.getItemDetail(16) then
if inventoryID == 13 then
print("Out of materials.")
break
end
end

–Ensuring the select switches when needed–
if turtle.getItemDetail(inventoryID) == turtle.getItemDetail(15) then
inventoryID = inventoryID + 1
turtle.select(inventoryID)
end

if turtle.detectDown() == true then
turtle.digDown()
turtle.select(15)
turtle.drop()
turtle.select(inventoryID)
end

turtle.placeDown()
turtle.forward()
depth = depth + 1

if (depth % 6) == 0 then
if (turnNum % 6) == 0 then
turtle.placeDown()
turtle.select(torchID)

if (turtle.getItemDetail) == nil then
torchID = torchID + 1
end
end
end

if depth == (x - 1) then
if (turnNum % 2) == 1 then
if turtle.detectDown() == true then
turtle.digDown()
turtle.placeDown()
end
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
depth = 0
elseif (turnNum % 2) == 0 then
if turtle.detectDown() == true then
turtle.digDown()
turtle.placeDown()
end
turtle.placeDown()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
depth = 0
end
turnNum = turnNum + 1
end

until turnNum == (y)
Exerro #4
Posted 20 April 2017 - 04:41 PM
I can still see a few issues
  • line 23, comparing two non-identical table references will always be false except in the case that both functions return nil
  • line 31, ditto
  • line 52, you're not calling turtle.getItemDetail, therefore you're just testing if the function `turtle.getItemDetail` is nil, which shouldn't ever be the case
There are also a few ways you can improve this
  • line 6/10 and 7/11 - you can do `x = tonumber(read())`, you don't need two (global :/) variables
  • line 36, 60, and 69 - you don't need to put `== true`, think about it, what is `a == true` if a is true? it's just true, and `a` is true, so you may as well just write `a`
  • line… all over the place - you don't need to put brackets around expressions either side of `==`, it has one of the lowest precedences, so `x - a == b` is automatically read as `((x - a) == B)/>`, the only exception to this is using 'and'/'or'.
Where you're comparing table references, line on lines 23 and 31, you can use this function instead:
Spoiler

local function compare_tables( a, b )
local checked = {}

for k, va in pairs( a ) do
local vb = b[k]
local t1, t2 = type( va ), type( vb )

checked[k] = true

if not (t1 ~= t2 or t1 ~= "table" or not compare_tables( a, b )) and a ~= b then
return false
end
end

-- you can remove this if you know the tables will share the same keys
for k, v in pairs( b ) do
if not checked[k] then
return false
end
end

return true
end
Or, minified:

local function a(b,c)local d={}for e,f in pairs(b)do local g=c[e]local h,i=type(f),type(g)d[e]=true;if not(h~=i or h~="table"or not a(b,c))and b~=c then return false end end;for e,j in pairs(c)do if not d[e]then return false end end;return true end