Jump to content




I have this program and i have this problem


  • You cannot reply to this topic
2 replies to this topic

#1 Adrikus

  • New Members
  • 2 posts

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

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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.

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.

View PostAdrikus, on 30 January 2016 - 09:55 PM, said:

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users