Jump to content




A less Than or Greater than a variable?


  • You cannot reply to this topic
8 replies to this topic

#1 Zilarrezko

  • Members
  • 5 posts

Posted 17 October 2012 - 05: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.

#2 JoshhT

  • New Members
  • 64 posts
  • LocationAustralia

Posted 17 October 2012 - 05: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.

#3 Zilarrezko

  • Members
  • 5 posts

Posted 17 October 2012 - 05: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!

#4 Zoinky

  • Members
  • 144 posts
  • LocationWellington, New Zealand

Posted 17 October 2012 - 06:10 AM

Change
print("supply "..i" is low")
to
print("supply"..i.." is low")
I think? :D/>

#5 JoshhT

  • New Members
  • 64 posts
  • LocationAustralia

Posted 17 October 2012 - 06: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.

#6 Zilarrezko

  • Members
  • 5 posts

Posted 17 October 2012 - 06: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/>

#7 stilldabomb

  • New Members
  • 110 posts

Posted 17 October 2012 - 07:35 AM

 Zilarrezko, on 17 October 2012 - 06:57 AM, said:

--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


#8 Zilarrezko

  • Members
  • 5 posts

Posted 17 October 2012 - 08:08 PM

 stilldabomb, on 17 October 2012 - 07:35 AM, said:

 Zilarrezko, on 17 October 2012 - 06:57 AM, said:

--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!

#9 stilldabomb

  • New Members
  • 110 posts

Posted 18 October 2012 - 01:39 AM

 Zilarrezko, on 17 October 2012 - 08:08 PM, said:

 stilldabomb, on 17 October 2012 - 07:35 AM, said:

--snip--

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users