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

Problem changing slots with a variable

Started by konoa, 28 February 2013 - 09:08 PM
konoa #1
Posted 28 February 2013 - 10:08 PM
Hello everyone, I have been playing with computercraft, very new to everything, and learning lua has been fun so far, and not too difficult as I already have a bit of programming experience myself.

I made one of my first scripts, a simple thing to ask me for a number, then build a floor out of whatever material I give it. After realizing i needed it to get rid of stuff in its inventory it picks up to function correctly i attempted to add a section that switches to the last free space in the inventory, drops the item, then switches back to the slot it was on.

I am sure it is some kind of logical error, the compiler says there is an error on line 18, and that is it missing a number, which made no sense to me.
I have not searched for any pre-made floor making programs becaue I want to start making things myself. I was using a few premade scripts but i couldnt understand them all that well becaue they were not commented at all and were way to long to figure out, along with me not knowing lua' syntax. I searched for an answer but dont know exactly how to phrase the problem, or dont know the problem well enough to phrase it at all? Anyway heres my code, its rather short so i hope its not too hard to figure out where i went wrong.


term.clear()
term.setCursorPos(1,1)

local length

print("Please put block to build with in slot 1")
print("type in number of blocks to build out to: ")

--variable inputs
local length = read()
local count
local slot = 1
local lastSlot
print("Please Type in the last empty slot")
print("slot 1 is top left, slot 16 is bottem right")
lastSlot = read()
count = turtle.getItemCount(slot)

--floor laying loop
for i=1, length do
  turtle.digDown()
  turtle.placeDown()
  turtle.forward()
  turtle.select(lastSlot)
  turtle.drop(1)
  turtle.select(slot)
	if count<1 then
	  turtle.select(slot+1)
	  slot = slot+1
	end
end
term.clear()
Lyqyd #2
Posted 01 March 2013 - 06:50 AM
Split into new topic.

The problem is that read() returns a string and turtle.select() requires a number. Try using `lastSlot = tonumber(read())` to convert the input into a number.
konoa #3
Posted 01 March 2013 - 02:28 PM
Ahh that makes sense, lua simplified variable types, im so used to defining what was a string and what wasnt i just assumed lua did all conversions for me.
Dlcruz129 #4
Posted 01 March 2013 - 03:27 PM
Also, variables don't need to be defined before you use them. Just a tip, though, programs work fine either way.
oeed #5
Posted 01 March 2013 - 03:31 PM
Ahh that makes sense, lua simplified variable types, im so used to defining what was a string and what wasnt i just assumed lua did all conversions for me.

Remember to use code tags in future.
konoa #6
Posted 01 March 2013 - 05:53 PM
Ahh that makes sense, lua simplified variable types, im so used to defining what was a string and what wasnt i just assumed lua did all conversions for me.

Remember to use code tags in future.

Edited in the code tags. I think i was going to but forgot. Even if I already have the problem solved.


Thanks for all the help everyone, I think I got it working now, or will when the server's back up.
Engineer #7
Posted 03 March 2013 - 03:07 PM
You could detect the first empty slot >


for i = 16, 1, -1 do
   if turtle.getItemCount(i) == 0 then
      lastSlot = i 
      break
   end
end

Just a thought
Left4Cake #8
Posted 03 March 2013 - 04:43 PM
Ahh that makes sense, lua simplified variable types, im so used to defining what was a string and what wasnt i just assumed lua did all conversions for me.

My understanding is that Lua functions strings to numbers automatically but computercraft functions don't.
ChunLing #9
Posted 03 March 2013 - 06:05 PM
Some operations tonumber strings automatically, others (like concatenation) tostring numbers, and yet others don't do either. The actual functions tend to be in the last category, while various operators tend to be in one of the first two, but it varies by operator/function. If you don't know for a fact that a given function/operator will work with what you give it, then don't assume it will.