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

turtle.select program not working.

Started by Zivel, 21 October 2012 - 10:27 PM
Zivel #1
Posted 22 October 2012 - 12:27 AM
Why is this not working:


print("Which slot?")
local x = read()
turtle.select(x)

I have identified this as the problem in a more complex bit of code I have but for the life of me I cannot get this to work. It should ask "Which slot?", I enter a number and it selects that slot…

It is giving me turtle:18: Expected number.

I am just beginning in programming and I have been trying to figure this out for a while now. I know it is something simple, please point it out to me.

Thanks
cant_delete_account #2
Posted 22 October 2012 - 12:58 AM
Use tonumber(read())
turtle.select requires a number, not a string, and read() returns a string, tonumber makes a string into a number.
Zivel #3
Posted 22 October 2012 - 01:09 AM
Thank you very much!

I was wondering if it was trying to read it as a string and not an integer.

Just for interest sake and my knowledge. How come this works:


print("enter something")
local x = read()
print(x)
x=x+5
print(x)

This will use the read() input as an integer. Proven by the result of it adding 5 to it and giving me the correct answer. This is where I got messed up and thought there was something else wrong. Why does the first code not automatically read it as an integer but the code above does? What is the difference?
remiX #4
Posted 22 October 2012 - 01:52 AM
Thank you very much!

I was wondering if it was trying to read it as a string and not an integer.

Just for interest sake and my knowledge. How come this works:


print("enter something")
local x = read()
print(x)
x=x+5
print(x)

This will use the read() input as an integer. Proven by the result of it adding 5 to it and giving me the correct answer. This is where I got messed up and thought there was something else wrong. Why does the first code not automatically read it as an integer but the code above does? What is the difference?

Because you printing it as a string and adding it as a string :)/>/> This isn't reading it as a number, but as a string. Some functions need a number input such as turtle.select()
Zivel #5
Posted 22 October 2012 - 02:25 AM
Legends!
Lyqyd #6
Posted 22 October 2012 - 03:33 AM
Because you printing it as a string and adding it as a string :)/>/> This isn't reading it as a number, but as a string. Some functions need a number input such as turtle.select()

This is not accurate. The arithmetic operation (adding five) won't work with a string either, but it will automatically attempt a tonumber() on non-string values before giving up. In this way, the string is converted to a number, is increased by five, and the resulting number is stored back in the variable.
remiX #7
Posted 22 October 2012 - 08:57 AM
Because you printing it as a string and adding it as a string :)/>/> This isn't reading it as a number, but as a string. Some functions need a number input such as turtle.select()

This is not accurate. The arithmetic operation (adding five) won't work with a string either, but it will automatically attempt a tonumber() on non-string values before giving up. In this way, the string is converted to a number, is increased by five, and the resulting number is stored back in the variable.

Interesting, didn't know that.
Zivel #8
Posted 25 October 2012 - 07:09 AM
I have another noob issue I hope you can help me with, I hope you guys dont mind me posting in the same thread rather than make a new one….

I have this:

os.loadAPI("myAPIs/move")
os.loadAPI("myAPIs/inve")
local slot = 1
move.p(8)

APIs I made are:

function com()
turtle.select(slot)
while not turtle.compareTo(slot+1) do
   slot=slot+1
end
turtle.select(slot+1) 
slot=slot+1
end

and:

os.loadAPI("myAPIs/inve")
function f(x)
   for i = 1, x do
	  while not turtle.forward() do
		 turtle.dig()
	  end
   end
end
function p(x)
   for i = 1, x do
	  if turtle.getItemCount(slot) == 1
	  then
		 inve.com()
	  end
	  f(1)
	  turtle.digDown()
	  turtle.placeDown()
   end
end

Now if I use them all in one function and take out the API system it works fine ^_^/>/> but as soon as I split them into API's and try to load them it fails to recognise the local slot = 1 and give me a "Expected number" error… god I am sick of Expected number errors.

Whats going on? I am loving learning this stuff and are trying to teach myself but I have to come to you when google and my random guessing just dont work sorry.
ChunLing #9
Posted 25 October 2012 - 10:08 AM
local slot = 1 is, well, local. Anything that is loaded/interpreted before a local is declared will not get the address where the local will be stored. And I doubt that APIs can use anything outside their own files that isn't explicitly passed to them, an API loads as global, and probably gets interpreted before anything in the program using it, including globals in the function.

The solution is simple, make it so that your API functions are passed slot 1 as an argument, and return a value that can be used to update the local slot one (multiple returns means you don't have to give up any other return values, though you don't have any others yet).

A note, you'll also need to fix that "while not turtle.compareTo(slot+1) do slot=slot+1 end " bit, that's really not going to work. Maybe just add "and slot < 16" to it, but I'm not clear on what it's supposed to do. But what it does now isn't good.
Zivel #10
Posted 26 October 2012 - 05:57 AM
Thank you, I will look into the passing of arguments from one function to another, thanks for the info.

The "while not turtle.compareTo(slot+1) do slot=slot+1 end" part is my attempt (works so far) to get the turtle to scan its inventory for items that are the same as its current slot, moving up a step each time it finds one thats its not. I was hoping to be able to say that if slot == 17 then slot = 1 to make it cycle through… maybe creating an endless loop that will need to be remedied or going your way and saying if slot == 17 then 'stop bloody working….'

thanks again for your help, this community is great.
ChunLing #11
Posted 26 October 2012 - 07:26 AM
Okay, for that kind of thing use "slot = math.fmod(slot,16)+1". But use an extra condition in your while loop so that your turtle doesn't sit there just running over its slots until it gets a hit, because if it runs into something it doesn't have in inventory, it's going to sit there forever (actually not, it will probably generate a "too long without yielding" error, but I guess that doesn't make it do anything useful either).