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

Farming turtle being jerk

Started by hugeblank, 13 December 2015 - 05:04 AM
hugeblank #1
Posted 13 December 2015 - 06:04 AM
Ok, so it's not the turtle, it's the code. Which is me. I'm being a jerk to myself. I need help.

So here's the code:


125 function isWheat()
126 success, data = turtle.inspectDown()
127  if data[metadata] == 7 then
128    turtle.digDown()
129    for i = 1, 15 do
130	  seedget, isseed = turtle.getItemdetail(i)
131	  if isseed[name] == "minecraft:wheat_seeds" then
132	    turtle.select(i)
133	    seed = turtle.place()
134	    if seed == false then
135		  turtle.down()
136		  turtle.digDown()
137		  turtle.up()
138		  turtle.place()
139	    end
140	  end
141    end
142  end
143  lwcheck()
144 end

the turtle is telling me:

farm:127: attempt to index ? (a nil value)

what's wrong with it? I'm pretty sure I have everything right.
Bomb Bloke #2
Posted 13 December 2015 - 06:20 AM
data[metadata] is not the same thing as data.metadata (or data["metadata"], for that matter).

You would also ideally be checking that success is true before attempting to treat data as a table, but due to the way Lua handles strings you can technically get away with not bothering.

Ok, so it's not the turtle, it's the code. Which is me. I'm being a jerk to myself.

:)/>

You'd be amazed how many people fail to find bugs in their code simply because they're subconsciously too arrogant to believe they're in there in the first place. They don't get very far as programmers.
Link149 #3
Posted 13 December 2015 - 06:29 AM
data[metadata] is not the same thing as data.metadata (or data["metadata"], for that matter).

I just tested in-game, setting a table key-value pair with a nil index causes an error but trying to access a table with a nil index doesn't.

This errors:

local t = {[nil] = true}

This doesn't, but instead returns nil:

print(t[nil])

Is this weird or am I missing something?
Also, as I just showed, accessing a table with a nil index only returns nil. No errors.
So where is hugeblank's error coming from?
Bomb Bloke #4
Posted 13 December 2015 - 06:44 AM
Oh hey, good point. Attempt to index nil. That'd mean data's nil, but I can't think of a scenario where that'd occur, off the top of my head…

Looking closer, I can see an attempt to call nil on line 130 - it should be turtle.getItemDetail(), a function which provides only one return value (either the result or nil - it doesn't return a "success" bool first).

I'm fairly sure nothing's changed since turtle.inspect() has been introduced, but what the heck, try throwing in that "success" check anyway and see if it makes a difference:

if success and data[metadata] == 7 then
hugeblank #5
Posted 13 December 2015 - 07:09 AM
The error on 130 is fixed. So what you're saying about 127 is it's supposed tobe data.metadata?