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

[SOLVED] Problems with an "until" section

Started by Obsidia, 28 June 2017 - 12:37 AM
Obsidia #1
Posted 28 June 2017 - 02:37 AM
Hello,

Currently working with something that requires to ask the user to put "x" amount of "x" into a specific slot before the code continues. If that isn't the case it is supposed to wait for a keystroke for example before checking again.

I tried around for 2 hours now and finally gave up.

Here are examples of what I have tried:

repeat - until

local data = turtle.getItemDetail()
turtle.select(1)
if data.name == "minecraft:sapling" then
print("Correct!")
else
repeat
print("Not a sapling.")
sleep(1)
until data.name == "minecraft:sapling"
end

In theory it should stop as soon as you put saplings in the selected slot, shouldnt it!?

while true loop if its unequal to

local data = turtle.getItemDetail()
if data.name == "minecraft:sapling" then
  print("#currently there are: ", data.count, " saplings in slot .")
elseif data.name ~= "minecraft:sapling" then
  while true do
   term.clear()
   term.setCursorPos(1,1)
   print("---------------------------------------")
   print("--pls put saplins in slot 1---")
   print("---------------------------------------")
   sleep(5)
	if data.name == "minecraft:sapling" then
	 break
	end
  end
end

Same for this one. This one is the basic version of what I tried to use. Shouldnt it check the slot again and again?
Instead without the "term.clear()" it would just keep spamming the text even if saplings are in the selected slot!?

Does anybody know what I am doing wrong?
Edited on 28 June 2017 - 01:58 AM
valithor #2
Posted 28 June 2017 - 03:06 AM
It is spamming the text because you are never rechecking the slot. You get the value of the slot initially with local data = turtle.getItemDetail(), but you never update the data variable with the new information from the slot, so lets say the slot had dirt in it. If you never tell it to recheck the slot, data will always contain the information about the dirt that was in the slot.

Example:

local data = turtle.getItemDetail() --# get a initial value of the slot
while data.name~="minecraft:sapling" do --# running the while loop while data.name does not equal "minecraft:sapling"
  data = turtle.getItemDetail() --# getting the new information about the slot
  sleep(5) --# sleep for 5 seconds so we are not spamming
end
Edited on 28 June 2017 - 01:21 AM
Bomb Bloke #3
Posted 28 June 2017 - 03:53 AM
Because turtle.getItemDetail() returns nil on empty slots, and you can't index into nil to get a "name" key, a safer loop would look like:

local data = turtle.getItemDetail()

while not (data and data.name == "minecraft:sapling") do
	print("Gimme a sapling")
	os.pullEvent("turtle_inventory")
	data = turtle.getItemDetail()
end
Obsidia #4
Posted 28 June 2017 - 04:11 AM
Because turtle.getItemDetail() returns nil on empty slots, and you can't index into nil to get a "name" key, a safer loop would look like:

local data = turtle.getItemDetail()

while not (data and data.name == "minecraft:sapling") do
	print("Gimme a sapling")
	os.pullEvent("turtle_inventory")
	data = turtle.getItemDetail()
end
It is spamming the text because you are never rechecking the slot. You get the value of the slot initially with local data = turtle.getItemDetail(), but you never update the data variable with the new information from the slot, so lets say the slot had dirt in it. If you never tell it to recheck the slot, data will always contain the information about the dirt that was in the slot.

Example:

local data = turtle.getItemDetail() --# get a initial value of the slot
while data.name~="minecraft:sapling" do --# running the while loop while data.name does not equal "minecraft:sapling"
  data = turtle.getItemDetail() --# getting the new information about the slot
  sleep(5) --# sleep for 5 seconds so we are not spamming
end

Thanks to both of you!
Works like a charm!
I assumed (for some reason) that it would update itself the way I did it. :D/>