Jump to content


Obsidia's Content

There have been 5 items by Obsidia (Search limited from 10-February 22)


By content type

See this member's

Sort by                Order  

#267877 [SOLVED] Problems with an "until" section

Posted by Obsidia on 28 June 2017 - 03:11 AM in Ask a Pro

View PostBomb Bloke, on 28 June 2017 - 02:53 AM, said:

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

View Postvalithor, on 28 June 2017 - 02:06 AM, said:

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



#267874 [SOLVED] Problems with an "until" section

Posted by Obsidia on 28 June 2017 - 01:37 AM in Ask a Pro

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?



#258162 Print out if theres a wrong amount of items in a specific slot after checking it

Posted by Obsidia on 19 August 2016 - 07:11 PM in Ask a Pro

View PostBomb Bloke, on 18 August 2016 - 11:26 AM, said:

Alternatively:

print("Please place 20 items in slot 1, 10 in slot 2, and then press enter.")					

while true do
	local event, key = os.pullEvent("key")
	
	if key == keys.enter then  -- http://www.computercraft.info/wiki/Keys_%28API%29
		if turtle.getItemCount(1) ~= 20 then
			print("I see "..turtle.getItemCount(1).." in slot 1 but require 20.")
		elseif turtle.getItemCount(2) ~= 10 then
			print("I see "..turtle.getItemCount(2).." in slot 2 but require 10.")
		else
			break
		end
	end
end

There are many ways to skin a cat, of course, but these examples should give you some ideas as to your options.

Regarding functions...


Ive used your way for my current program but I get an error every time it doenst meets the requirements in this exact loop.

http://pastebin.com/HixVbgAa -- Sorry, that the comments are in german but I can translate them if necessary.

while true do
local event, key = os.pullEvent("key")
if key == keys.enter then
  if turtle.getItemCount(1) < 61 then
   print("Ich sehe "..turtle.getItemCount(1).." Items in Slot 1, benöitge aber 61!")
  elseif turtle.getItemCount(2) < 52 then
   print("Ich sehe "..turtle.getItemCount(2).." Items in Slot 2, benöitge aber 52!")
  elseif turtle.getItemCount(3) < 20 then
   print("Ich sehe "..turtle.getItemCount(3).." Items in Slot 3, benöitge aber 20!")
  elseif turtle.getItemCount(4) < 1 then
   print("Ich sehe "..turtle.getItemCount(4).." Items in Slot 4, benöitge aber 1!")
  elseif turtle.getItemCount(5) < 2 then
   print("Ich Sehe "..turtle.getItemCount(5).." Items in Slot 5, benötige aber 2!")
  else
   break
  end
end
end

Thats how I currently use it. It works fine if its equal to or more than asked for but crashes with the error:
"Windows:94: Arguments must be the same length" each time it doenst meets the requirements



#258082 Print out if theres a wrong amount of items in a specific slot after checking it

Posted by Obsidia on 18 August 2016 - 08:02 AM in Ask a Pro

I basically want to "pause" the program till the right amount of items are in the right slots.
So this is an example of what im currently using(Im pretty sure everyone whos somewhat decent at coding while facepalm himself after seeing my attempt. lol)

print("20 items in slot 1")		
print("10 items in slot 2")

while true do
event, key = os.pullEvent("key")
if key == 28 then
if turtle.getItemCount(1) == 20 then
if turtle.getItemCount(2) == 10 then
break()
end
end
end

And well.. it looks retarded but it works. sort of.
How can I get it to print if there are wrong amount of items in a specific slot?
just adding "elseif" below the turtle.getItemCount lines will just fuck up the code and will stop it from working

Oh, and whats the symbol for "unequal to"? The wiki says its "~=" but it doesnt seem to understand it neither does just "~" work


And one more, can I create a function to easy it out what to.. for example select? I mean that you could write
select(5)
so it takes the number between the () as a variable and puts it in individually in to your function?
local function select()
turtle.select(x)	 --- And puts it in there individually
end



#257840 Trying to understand Tables

Posted by Obsidia on 11 August 2016 - 02:30 PM in Ask a Pro

Im currently trying to understand Tables work on my own but I cant figure out how to use them. :D

Ive just wrote a simple adder to test it out

sum = 0	
print("Press 'E' to add a digit, 'X' to exit!")
print("Current count: "..sum)

local Table = {10,20,30,40,50}		   [color=#008000]---  The Table with the list of digits I want to use  [/color]

local function Blue()  
term.setTextColor( colors.blue )
end
local function White()	
term.setTextColor( colors.white )
end

while true do
  loca event, key = os.pullEvent("key")
	if key == keys.e then
	sum = sum + 1
	print("Current count: "..sum)
	elseif key == keys.x then
	break
	end

  if sum == Table then				[color=#008000] ---  This where I need help[/color]
  Blue()
  print("---- E to add, X to exit! ---")
  end
end



So basically I want it to take a look at the digits in the table and if its one of them it should print out the options again
but currently its doing nothing at all. :P

Tables are used to shorten Code as far as I understood, but how do I use it?
What part of tables did I missunderstand or is my thought of what it is and what its used for completly wrong?