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

Help "slot number 0 out of range"

Started by MigFort, 31 December 2014 - 01:12 AM
MigFort #1
Posted 31 December 2014 - 02:12 AM
Hi guys and girls,
I am doing a program named wood that basically cut trees and plant them back. Their is no refuel part for now.

When I try to run it, the error " turtle:22:slot number 0 out of range ". I have no idea how to fix it. Thank you for helping me.

pastebin: http://pastebin.com/wgn5YTS4

--Wood
--------------------
--| slot | item	|
---------+----------
--|  1   | gravel  |
--|  2   | wood	|
--|  3   | sand	|
--|  4   | stone   |
--|  5   | sapling |
--|  6   | slab	|
--|  16  | fuel	|
--------------------
turtle.select(4) --stone
if turtle.compareDown() then
  go()
  turtle.select(3) --sand
  if turtle.compareDown() then
	turtle.turnLeft()
	cut()
	plant()
	turtle.turnRight()
  else
	turtle.select(6) --slab
	if turtle.compareDown() then
	  cut()
	  plant()
	  turtle.turnRight()
	else
	  turtle.select(4) --stone
	  if turtle.compareDown() then
		turtle.turnRight()
	  end
	end
  end
end
  
  
function go() --avance
print("going")
turtle.select(1) --gravel
turtle.forward()
while turtle.compareDown() do
  turtle.forward()
end
end
function plant()
print("planting")
if not turtle.detect() then
  turtle.select(5) --sapling
  turtle.place()
end
end
function cut()
print("cutting")
turtle.select(2) --wood
if turtle.compare() then
  turtle.dig()
  turtle.forward()
  while turtle.detectUp() do
   turtle.digUp()
   turtle.up()
  end
  while not turtle.detectDown() do
   turtle.down()
  end
  turtle.back()
end
if not turtle.detect() then
  plant()
end
end
Edited on 31 December 2014 - 05:24 AM
Cycomantis #2
Posted 31 December 2014 - 07:27 AM
I will test this when I get home and have access to Minecraft. The only thing that jumps out at me as being off is the order of your code. Its important to remember that Lua reads from top to bottom and if it hasnt read it then it does not exist.

This means that you first should decalre any Variables / Functions before you try to use them.

Based on this I would lay out your code something like this and see if you still get the error.

--Wood
--------------------
--| slot | item |
---------+----------
--|  1   | gravel  |
--|  2   | wood |
--|  3   | sand |
--|  4   | stone   |
--|  5   | sapling |
--|  6   | slab |
--|  16  | fuel |
--------------------

local function go() --avance
print("going")
turtle.select(1) --gravel
turtle.forward()
while turtle.compareDown() do
  turtle.forward()
end
end

local function plant()
print("planting")
if not turtle.detect() then
  turtle.select(5) --sapling
  turtle.place()
end
end

local function cut()
print("cutting")
turtle.select(2) --wood
if turtle.compare() then
  turtle.dig()
  turtle.forward()
  while turtle.detectUp() do
   turtle.digUp()
   turtle.up()
  end
  while not turtle.detectDown() do
   turtle.down()
  end
  turtle.back()
end
if not turtle.detect() then
  plant()
end
end

turtle.select(4) --stone
if turtle.compareDown() then
  go()
  turtle.select(3) --sand
  if turtle.compareDown() then
		turtle.turnLeft()
		cut()
		plant()
		turtle.turnRight()
  else
		turtle.select(6) --slab
		if turtle.compareDown() then
		  cut()
		  plant()
		  turtle.turnRight()
		else
		  turtle.select(4) --stone
		  if turtle.compareDown() then
				turtle.turnRight()
		  end
		end
  end
end


It also would probably be a good idea to localize your functions as I have done.
Edited on 31 December 2014 - 06:27 AM
Bomb Bloke #3
Posted 31 December 2014 - 07:37 AM
Note that if you don't localise your variables - including your function pointers - they'll remain loaded into memory when your script ends. They go into the global scope, which isn't cleared until the computer shuts down.

If you attempt to call a function before you define it, the only reason the script wouldn't error out with an "attempt to call nil" is because you defined a global version of that function at a time you ran a previous revision of your script.

Re-ordering things to ensure the functions are defined before they're called, as Cyco has done, should ensure that the functions you're attempting to run are "up to date".

This tutorial is well worth a read.
MigFort #4
Posted 01 January 2015 - 08:13 AM
Tank you for your help! It is now running! (with a few bug i need to fix, but it is still running)