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

I have this program and i have this problem

Started by Adrikus, 30 January 2016 - 08:55 PM
Adrikus #1
Posted 30 January 2016 - 09:55 PM
Hi programmers, my name in game is Adrikus, i started to program recently in computerCraft, but i programmed before in Java, but lua is too complex for me, i make a function in lua that seeks a object in minecraft, here i put an example.

if you want search another object you can rename the variable.
——————————————————————————–
function searchObject()
local data;
local pos = -1;
for i = 1,16 do
data = turtle.getItemDetail(i).name;
if(data == "minecraft:wheat_seeds") then
pos = i
break;
end
return pos;

end
——————————————————————————–

And the turtle have problems with turtle.getItemDetail(), and this is that problem attempt to index ? (a nil value).

If you could help me would you do me happy!!!
Thanks
Lyqyd #2
Posted 31 January 2016 - 12:09 AM
If there's nothing in the slot, turtle.getItemDetail() won't return a table of information, so you can't get the value of the name key out of it. Check that turtle.getItemDetail() actually returns something before attempting to index it.
Bomb Bloke #3
Posted 31 January 2016 - 02:25 AM
For eg:

data = turtle.getItemDetail(i)
if data and data.name == "minecraft:wheat_seeds" then  -- If "data" is non-nil, and data.name matches string, then...

Lua's bright enough to bail from the comparison as soon as it determines the end result will be false, so it won't attempt to index into "data" (to get the "name" key) if it first finds that data is nil.

i programmed before in Java, but lua is too complex for me

Heh. Working in Java, I'm forever digging through documentation to get anything done; gotta read through half a billion class definitions to figure out how to implement a different text colour or something.

In Lua, there's like six datatypes and that's it. When you've got a handle on the basics you can pretty much just faceroll whatever code you want.