InspectUp() doesn't seem to work the way i think it should? help

Started by Noodlecode, Jan 05, 2021, 02:46 AM

Previous topic - Next topic

Noodlecode

Im working on my first project and I am stuck.
what Im trying to have do is have a turtle inspect the block above it and then do something based on the block so cobble stone would be move forward and dirt would be turn left then forward.
when using inspectUp in lua it reads back true and the name of the block from what I think is a table? i could be wrong. I thought this might be an easy project but at this point in heckin stuck. iv done a few ver. of the program and nothing seems to be working when it read the block above it it only read true not the block type. if I have it print it prints true then some random information that changes.
heres what im currently trying to use. can someone point in the right direction on how to work on this?
please note that the code is really messed up now from trying to fix it. I now know why people back up different version of things.

while true do
local Up = { ["minecraft:cobblestone"] = true}
success, data = turtle.inspectUp()

if Up == false then

    turtle.back()
    turtle.turnRight()
  else
 turtle.forward()
end
end

Lupus590

Is that the whole code?

In that snippet, you capture the turtle.inspectUp in two global variables and then don't use them. You also have a variable Up that always is truthy in the one place that it is used in the if statement.

Also, your indentation is a little inconsitent.

Noodlecode

Quote from: Lupus590 on Jan 05, 2021, 08:02 AMIs that the whole code?

In that snippet, you capture the turtle.inspectUp in two global variables and then don't use them. You also have a variable Up that always is truthy in the one place that it is used in the if statement.

Also, your indentation is a little inconsitent.
z
Yes its whats left of the whole thing i messed it all up. I didnt want any to think i was asking questions with out trying to figure stuff out

Inksaver

while true do
local Up = { ["minecraft:cobblestone"] = true}
success, data = turtle.inspectUp()

if Up == data.name then

turtle.back()
turtle.turnRight()
else
turtle.forward()
end
end
This works, but be warned the turtle will continue to repeat the forward or back/right commands indefinitely.
You were on the right lines, but just needed to compare the data.name of the block above (eg. "minecraft:cobblestone") to the Up table value.

I am guessing you want to make the turtle move to follow a pattern of blocks. As a stating point the following simpler function may help:

local function move()
local moved = false -- return value
local success, data = turtle.inspectUp()
if data.name == "minecraft:cobblestone" then
turtle.back()
turtle.turnRight()
moved = true
elseif data.name == "minecraft:dirt" then
turtle.forward()
moved = true
end

return moved
end


while move() do end -- will only continue while block above is dirt/cobble
Sent from a computer using a keyboard and mouse.