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

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

Started by Obsidia, 18 August 2016 - 07:02 AM
Obsidia #1
Posted 18 August 2016 - 09:02 AM
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
Edited on 18 August 2016 - 07:13 AM
supernicejohn #2
Posted 18 August 2016 - 12:16 PM
Not familiar at all with the turtle api but here is what I think you wanted to do:

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 and turtle.getItemCount(2) == 10 then -- use the logic operator "and" to include both statements in a single if statement
         break
      else
         --print stuff here :)/>
      end
   end
end

And the second part, if you want to use YourFunctionName( x ) then you need to define that the function should take a variable like, "local function YourFunctionName( VariableName )", where VariableName will be used in the function.

Also the ~= operator should work… interesting if it doesn't.

Hope this helped! :)/>
Bomb Bloke #3
Posted 18 August 2016 - 12:26 PM
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...
Obsidia #4
Posted 19 August 2016 - 08:11 PM
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
CoderPuppy #5
Posted 19 August 2016 - 08:53 PM
That's because of the "ö" in the output, you could replace it with "\246" to get it to work in CC (that's only in newer versions of CC).
Edited on 19 August 2016 - 07:07 PM
KingofGamesYami #6
Posted 19 August 2016 - 09:03 PM
To expand upon the error, the line that's erroring is line 94 in the window API,

parent.blit( tLine.text, tLine.textColor, tLine.backgroundColor )
term.blit requires text, textColor, and backgroundColor to be the same length.
Some non-English characters (such as ö) are counted as more than one for the length. This issue was fixed in ComputerCraft 1.76:
Changelog said:
New Features in ComputerCraft 1.76:
* Ported to Minecraft 1.8
* Added Ender Modems for cross-dimensional communication
* Fixed handling of 8-bit characters. All the characters in the ISO 8859-1 codepage can now be displayed
* Added some extra graphical characters in the unused character positions, including a suite of characters for Teletext style drawing
* Added support for the new commands in Minecraft 1.8 to the Command Computer
* The return values of turtle.inspect() and commands.getBlockInfo() now include blockstate information
* Added commands.getBlockInfos() function for Command Computers
* Added new "peripherals" program
* Replaced the "_CC_VERSION" and "_MC_VERSION" constants with a new "_HOST" constant
* Shortened the length of time that "Ctrl+T", "Ctrl+S" and "Ctrl+R" must be held down for to terminate, shutdown and reboot the computer
* textutils.serialiseJSON() now takes an optional parameter allowing it to produce JSON text with unquoted object keys. This is used by all autogenerated methods in the "commands" api except for "title" and "tellraw"
* Fixed many bugs
Edited on 19 August 2016 - 07:03 PM