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

A less Than or Greater than a variable?

Started by Zilarrezko, 17 October 2012 - 04:32 AM
Zilarrezko #1
Posted 17 October 2012 - 06:32 AM
alright, so while writing a code (I'm a beginner so I'm not claiming that I know at all what I'm doing) for checking for at least a number (stored in variable Q) yet it doesn't work

Here's the function code…


local Q = read()
function checkSupply()
   for i= 1, 16 do
	  turtle.select(i)
	  if turtle.getItemSpace(i) < Q then
		 print("supply "..i" is low")
		 if i == 16 then
		    print("not enough supplies")
		    break
		 end
	  end
   end
end

(…If turtle.getItemSpace(i) < Q then…)
the computer returns an error when I try to test the function just to see if it would work, (attempt to compare string with number expected, got string)

Am I doing this wrong? should it be written a different way? Please let me know!

also, if anyone knows how to just make the whole program pause or end, at a certain piece of code. Please let me know as well.

Thanks for your time and hopefully your effort to facepalm as you reluctantly say I'm stupid and help me out.
JoshhT #2
Posted 17 October 2012 - 06:41 AM
Change:

local Q = read()

-- Change to
local Q =tonumber(read())

Also, from the wiki.

turtle.getItemSpace( slotNum ) - Counts how many remaining items you need to fill the stack in the given Slot			
turtle.getItemCount( slotNum ) - Counts how many items are in the given Slot			

It would seem you might want to be using .getItemCount() instead.
Zilarrezko #3
Posted 17 October 2012 - 06:58 AM
ah, interesting.

And yes! I've been working with turtle.getItemSpace() a lot, so I was trying to think that way.

Thanks man!
Zoinky #4
Posted 17 October 2012 - 07:10 AM
Change

print("supply "..i" is low")
to

print("supply"..i.." is low")
I think? :D/>/>
JoshhT #5
Posted 17 October 2012 - 07:13 AM
Well, let me break it down for you, because I think you might be a little confused here.

Your code is used to alert you to when a stack is getting low.
So in my mind, you'd want to get the number of items in the slot, not the number of items needed for that slot to be full.

The way you're doing it:
Q = like, say 50?
getItemSpace(), meaning, how many items/blocks until that stack is full. would need to be higher than 50, to return you a low stack.

The way I would do it.
Q = 10
getItemCount(). if that is lower than Q, the stack is low.

It's much easier to understand to be honest.

Ohh, and Zoinky noticed something I missed. Good job mate.
Zilarrezko #6
Posted 17 October 2012 - 07:57 AM
yeah I scrapped a whole bunch, and trust me, I got it haha.

I also noticed that it had an error with something like that. So I just scrapped that as well

it's now


local Q = tonumber (read())

function checkSupply()
   for i = 1,16 do
	  turtle.select(i)
	  print((i-1).." is < needed")
	  if turtle.getItemCount(i) >= Q then
		 break
	  end
		 if i == 16 then
			print("not enough supplies")
			break --still don't know how to halt code or pause or end the program yet
		 end
   end
end

Again, thanks a ton guys :D/>/>
stilldabomb #7
Posted 17 October 2012 - 08:35 AM

--still don't know how to halt code or pause or end the program yet

sleep(time) -- Pauses for <time> secconds
break -- Breaks a loop
return -- Stops any code after from being ran
Zilarrezko #8
Posted 17 October 2012 - 09:08 PM

--still don't know how to halt code or pause or end the program yet

sleep(time) -- Pauses for <time> secconds
break -- Breaks a loop
return -- Stops any code after from being ran

Thanks man! Everyone is very helpful ^.^ here, take all my gratitude!
stilldabomb #9
Posted 18 October 2012 - 02:39 AM
–snip–

Thanks man! Everyone is very helpful ^.^ here, take all my gratitude!
Your welcome :P/>/>