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

What's wrong?

Started by tonkku107, 14 May 2013 - 06:30 AM
tonkku107 #1
Posted 14 May 2013 - 08:30 AM

--[[ Cobble Turtle ]]--

local mined = 0
local amount = false

function dropItems()
    turtle.turnRight()
    turtle.turnRight()
    print("Dropping Items...")
    for i = 1, 16 do
        turtle.select(i)
        turtle.drop()
    end
    turtle.select(1)
    print("Dropped!")
    turtle.turnRight()
    turtle.turnRight()
end

write("Amount of cobblestone: ")
repeat
        amount = tonumber(read())
        if not amount then
                write("I need a number: ")
        end
until amount

print("Amount set to " .. tostring(amount))

repeat
        if turtle.dig() then
         print("Mined!")
                mined = mined + 1
        end
        sleep(0.2)
        local full = true
for i = 1, 16 do
    full = (turtle.getItemSpace(i) == 0) and full
end
if full then
    print("Inventory is full!")
    dropItems()
end
until amount == mined
print("Successfully mined "..mined.." cobblestone!")
    dropItems()
The error is: cobble:19: attempt to perform arimethic __add on nil and number

What is wrong? And the turtle is in front of a cobble generator.

Also -1 won't work properly

No more :3 See below… Code updated
D3matt #2
Posted 14 May 2013 - 08:32 AM
3rd line. Cobble needs to be cobble. lua is case sensitive.
tonkku107 #3
Posted 14 May 2013 - 08:39 AM
3rd line. Cobble needs to be cobble. lua is case sensitive.
FFFFUUUUUU…
Why didn't i notice that? I checked the spelling but not that
tonkku107 #4
Posted 14 May 2013 - 08:47 AM
Didn't fix it:
Mackan90096 #5
Posted 14 May 2013 - 08:53 AM
Try:



print("Amount set to "..input)

if thats whats wrong.
makerimages #6
Posted 14 May 2013 - 09:11 AM
Whats exactly wrong now?


and also, why set input twice?




inf = 1
input = 200  //here
else
for a = 1,input do
  turtle.select(1)
  if turtle.compare() then
   turtle.dig()
   print("Mined!")
   cobble = cobble + 1
   if inf == 1 then
 input=200  //here
   end
tonkku107 #7
Posted 14 May 2013 - 09:15 AM
Whats exactly wrong now?


and also, why set input twice?




inf = 1
input = 200  //here
else
for a = 1,input do
  turtle.select(1)
  if turtle.compare() then
   turtle.dig()
   print("Mined!")
   cobble = cobble + 1
   if inf == 1 then
input=200  //here
   end
What do you mean? I tried -1 and 5 in that image
The problem is: -1 doesn't mine anything and other input mines only 1 time!
makerimages #8
Posted 14 May 2013 - 09:16 AM
look at the commented lines
tonkku107 #9
Posted 14 May 2013 - 09:47 AM
look at the commented lines
Because it will be infinite
tonkku107 #10
Posted 14 May 2013 - 10:12 AM

--[[ Cobble Turtle ]]--

inf = 0
Cobble = 0

write("How much cobblestone you want: ")
local input = read()
print("Amount set to "..input)
if input == "-1" then
print("infinite!")
inf = 1
input = 200
else
for a = 1,input do
turtle.select(1)
if turtle.compare() then
turtle.dig()
print("Mined!")
cobble = Cobble + 1
sleep(1.5)
if inf == 1 then
input = 200
end
end
end
print("Successfully mined "..cobble.." cobblestone!")
end
1 problem fixed. -1 still wont work. The sleep(1.5) made it wait for a new cobblestone
One new problem: at the end it will display only 1 in place of cobble. So it doesn't count
Spongy141 #11
Posted 14 May 2013 - 10:21 AM
Try indenting (Wont make your code better, just looks better, and easier to read…. but cobble needs to be Cobble… if your trying to have it add from its previous value, you can not have it change its variable…
tonkku107 #12
Posted 14 May 2013 - 10:24 AM
Try indenting (Wont make your code better, just looks better, and easier to read…. but cobble needs to be Cobble… if your trying to have it add from its previous value, you can not have it change its variable…
How i make it to count it? It just says 0
And can I make it on terminate say that?
LBPHacker #13
Posted 14 May 2013 - 10:32 AM
Okay… Big chages coming…
Spoiler
--[[ Cobble Turtle ]]--

-- * Cleaned up a bit and commented

local mined = 0
local amount = false

-- * read amount again and again until we get a real number
write("How much cobblestone you want: ")
repeat
    amount = tonumber(read())
    -- * let the user know that we need a number
    if not amount then
        write("Seriously: ")
    end
until amount

-- * verify the amount
print("Amount set to " .. tostring(amount))

repeat
    -- * turtle.dig returns false if no block was dug
    --   so we won't increase mined if that's the case
    -- * actually no need of turtle.compare here - I assume
    --   nothing will spawn in front of the turtle apart
    --   apart from cobblestone
    -- * by the way, turtle.compare would mess up things
    --   if there was no cobble in slot 1
    if turtle.dig() then
        mined = mined + 1
    end
    sleep(0.2)
until amount == mined
Ask if something is not clear.
tonkku107 #14
Posted 14 May 2013 - 10:34 AM
Okay… Big chages coming…
Spoiler
--[[ Cobble Turtle ]]--

-- * Cleaned up a bit and commented

local mined = 0
local amount = false

-- * read amount again and again until we get a real number
write("How much cobblestone you want: ")
repeat
	amount = tonumber(read())
	-- * let the user know that we need a number
	if not amount then
		write("Seriously: ")
	end
until amount

-- * verify the amount
print("Amount set to " .. tostring(amount))

repeat
	-- * turtle.dig returns false if no block was dug
	--   so we won't increase mined if that's the case
	-- * actually no need of turtle.compare here - I assume
	--   nothing will spawn in front of the turtle apart
	--   apart from cobblestone
	-- * by the way, turtle.compare would mess up things
	--   if there was no cobble in slot 1
	if turtle.dig() then
		mined = mined + 1
	end
	sleep(0.2)
until amount == mined
Ask if something is not clear.
sleep 1.5 is the minium without "crashing" the program
LBPHacker #15
Posted 14 May 2013 - 10:37 AM
sleep 1.5 is the minium without "crashing" the program
Nope. Since sleep yields the coroutine of the computer, you won't get "too long without yielding" even with a sleep(0).
tonkku107 #16
Posted 14 May 2013 - 10:39 AM
sleep 1.5 is the minium without "crashing" the program
Nope. Since sleep yields the coroutine of the computer, you won't get "too long without yielding" even with a sleep(0).
Well… Ok!
How about the -1 (Turtle will keep mining until inventory is full / terminated)
LBPHacker #17
Posted 14 May 2013 - 10:42 AM
How about the -1 (Turtle will keep mining until inventory is full / terminated)
Oh I knew I forgot something…
New code:
Spoiler
--[[ Cobble Turtle ]]--

-- * Cleaned up a bit and commented

local mined = 0
local amount = false

-- * read amount again and again until we get a real number
write("How much cobblestone you want: ")
repeat
    amount = tonumber(read())
    -- * let the user know that we need a number
    if not amount then
        write("Seriously: ")
    else
        if amount < 1 then
            write("Maybe something larger than 0: ")
            -- * I set amount to false here on purpose
            amount = false
        end
    end
until amount

-- * verify the amount
print("Amount set to " .. tostring(amount))

repeat
    -- * turtle.dig returns false if no block was dug
    --   so we won't increase mined if that's the case
    -- * actually no need of turtle.compare here - I assume
    --   nothing will spawn in front of the turtle apart
    --   apart from cobblestone
    -- * by the way, turtle.compare would mess up things
    --   if there was no cobble in slot 1
    if turtle.dig() then
        mined = mined + 1
    end
    sleep(0.2)
until amount == mined
tonkku107 #18
Posted 14 May 2013 - 10:45 AM
How about the -1 (Turtle will keep mining until inventory is full / terminated)
Oh I knew I forgot something…
New code:
Spoiler
--[[ Cobble Turtle ]]--

-- * Cleaned up a bit and commented

local mined = 0
local amount = false

-- * read amount again and again until we get a real number
write("How much cobblestone you want: ")
repeat
	amount = tonumber(read())
	-- * let the user know that we need a number
	if not amount then
		write("Seriously: ")
	else
		if amount < 1 then
			write("Maybe something larger than 0: ")
			-- * I set amount to false here on purpose
			amount = false
		end
	end
until amount

-- * verify the amount
print("Amount set to " .. tostring(amount))

repeat
	-- * turtle.dig returns false if no block was dug
	--   so we won't increase mined if that's the case
	-- * actually no need of turtle.compare here - I assume
	--   nothing will spawn in front of the turtle apart
	--   apart from cobblestone
	-- * by the way, turtle.compare would mess up things
	--   if there was no cobble in slot 1
	if turtle.dig() then
		mined = mined + 1
	end
	sleep(0.2)
until amount == mined
-1 works and i wanted it to do it

How about putting the cobble into a chest behind it when inv is full and at the end
LBPHacker #19
Posted 14 May 2013 - 10:48 AM
Oh! Just realized that you want it to mine infinitely if the given amount is -1! Hold on…
tonkku107 #20
Posted 14 May 2013 - 10:55 AM
Oh! Just realized that you want it to mine infinitely if the given amount is -1! Hold on…
Why to hold on? It works already
tonkku107 #21
Posted 15 May 2013 - 10:43 AM
LPBHacker, Can i make it drop it to a chest without hoppers?
LBPHacker #22
Posted 15 May 2013 - 10:45 AM
O'course. Make the turtle face towards the chest, select the proper slot, and turtle.drop().
tonkku107 #23
Posted 15 May 2013 - 10:52 AM
O'course. Make the turtle face towards the chest, select the proper slot, and turtle.drop().
Ok, so that's good. but how can i check for full inv?
LBPHacker #24
Posted 15 May 2013 - 11:01 AM
Option 1: Check the modulo of mined. When mined % 1024 is 0, the inventory sure is full. Keep in mind that if the user is a noob, he can put things into the turtle's inventory - this solution can't predict that. (1024; 16 slots multiplied by 64 cobblestones in a stack)
Option 2: (Not sure how much time would it take) Check turtle.getItemSpace after every mined cobblestone. Like:
local full = true
for i = 1, 16 do
    full = (turtle.getItemSpace(i) == 0) and full
end
-- full is now true if there is literally no more space in the inventory
if full then
    dropItems() -- this is a function I'm gonna explain later
end

So yeah… dropItems()… Where is the chest? Next to the turtle?
tonkku107 #25
Posted 15 May 2013 - 11:18 AM
Option 1: Check the modulo of mined. When mined % 1024 is 0, the inventory sure is full. Keep in mind that if the user is a noob, he can put things into the turtle's inventory - this solution can't predict that. (1024; 16 slots multiplied by 64 cobblestones in a stack)
Option 2: (Not sure how much time would it take) Check turtle.getItemSpace after every mined cobblestone. Like:
local full = true
for i = 1, 16 do
	full = (turtle.getItemSpace(i) == 0) and full
end
-- full is now true if there is literally no more space in the inventory
if full then
	dropItems() -- this is a function I'm gonna explain later
end

So yeah… dropItems()… Where is the chest? Next to the turtle?
behind so:

local full = true
for i = 1, 16 do
	full = (turtle.getItemSpace(i) == 0) and full
end
if full then
	turtle.right()
	turtle.right()
	dropItems()
	turtle.right()
	turtle.right()
end
cobble:16: attempt to call nil
LBPHacker #26
Posted 15 May 2013 - 11:43 AM
Of course it's nil. I said I'll explain it later, but couldn't do that without knowing where the chest is.
function dropItems()
    turtle.turnRight()
    turtle.turnRight()
    for i = 1, 16 do
        turtle.select(i)
        turtle.drop()
    end
    turtle.turnRight()
    turtle.turnRight()
end
tonkku107 #27
Posted 15 May 2013 - 12:15 PM
Of course it's nil. I said I'll explain it later, but couldn't do that without knowing where the chest is.
function dropItems()
    turtle.turnRight()
    turtle.turnRight()
    for i = 1, 16 do
        turtle.select(i)
        turtle.drop()
    end
    turtle.turnRight()
    turtle.turnRight()
end
oh, ok! Then the code starts to be pretty much done! ( can someone make a better "gui")
tonkku107 #28
Posted 16 May 2013 - 07:33 AM
Code Updated! I would like some options…
Logs: y/n
chest: y/n
chest is at: right/left/back