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

Crafty turtle suddenly unable to craft()

Started by geddeth, 01 December 2015 - 07:42 AM
geddeth #1
Posted 01 December 2015 - 08:42 AM
I have a pesky problem which I cannot find the cause of, and right now I am inclined to call this a bug in CC. But please prove me wrong!

I see the problem in a Crafty Turtle with a particular program (see below). While the turtle works as intended initially, the bug will most often occur when I log back into the server after a while and start this particular program again. The error I am seeing is "attempt to call nil". I understand that this error most often occurs when you are trying to call a function which does not exist or which you have mistyped. But in this case, the function missing is turtle.craft(). Also, when I hop into the Lua interpreter and run turtle.craft() I see the same error! It even fails to tab-complete that command, as if has simply disappeared from the Turtle API.

The only way to fix this problem is to destroy the computer and place it again. If I have labels on it, this is a quick procedure. But it really annoys the h*ll out of me. I want to know what's going on! :P/>

This is on a Linux-based multiplayer server running 1.7.10 latest via Forge. No other mods are installed on the server or client. The server is running Creative. I have confirmed the bug with multiple minor versions of Forge, and with CC 1.74. I have also seen it with multiple clients across 2 different OS.
I should say that I have attempted to replicate this in singleplayer without success.

Here is a screenshot which demonstrates the bug:


And here is the program. It's mainly a straight copy of this thing from Rainbow Hyphen.
It simply combines his Fireworks launcher with two required input signals: one from HTTP and another from Redstone.
When this bug occurs, it is in the while loop at the end, at turtle.craft(1).

-- fw_maker
-- Put this in the Crafty Turtle that makes the fireworks.

-- Start this one first, then start fw_parts!

side = 'back'
delay = 0.2
queueid = 20

function repack()
  -- if something is in cell 4, move it to cell 11.
  turtle.select(4)
  turtle.transferTo(11)
  -- if something is in cell 8, move it to cell 10.
  turtle.select(8)
  turtle.transferTo(10)
  -- select cell 1
  turtle.select(1)
end

function sync()
  rs.setOutput(side, true)
  sleep(delay)
  rs.setOutput(side, false)
  sleep(delay)
  while not rs.getInput(side) do
	sleep(delay)
  end
  sleep(delay * 3)
  repack()
end

function getQueue()
  local handle = http.get("http://example.com/json/"..queueid)
  code = handle.getResponseCode()
  if (code == 200) then
	return true
  else
	return false
  end
end

function getMasterRS()
  return rs.getInput('front')
end

function goForLaunch()
  rsm = getMasterRS()
  qstatus = false
  if (rsm == true) then
	qstatus = getQueue()
  end
  return qstatus
end

-- START PROCEDURE
-- select cell 1
turtle.select(1)

while (true) do
  sync() -- firework star
  turtle.craft(1)
  sleep(delay)
  sync() -- firework!
  turtle.craft(1)
  while not (goForLaunch()) do
	sleep(delay*5)
  end
  turtle.place()
end

I hope someone has an idea about whats happening here.
Bomb Bloke #2
Posted 01 December 2015 - 09:04 AM
If breaking the turtle and replacing it fixes it, and it's occurring under CC 1.74 SMP, then it sounds like you've got a valid bug there. The one silly question I have is - are you ensuring that the same version of ComputerCraft is running on the client and server with each test?

For your reference, the turtle API code - this gets loaded along with the regular APIs when a turtle boots up, and the last thing it does is to check whether there's a crafting table equipped. If so, it generates turtle.craft() within the API; if not, it doesn't. So presumably something's preventing the turtle from detecting what's equipped on chunk load occasionally.

In the meantime, these days it's possible to have turtles unequip and then re-equip items, in code (and checks are performed as you do so to add/remove turtle.craft() as appropriate). I suggest playing around with turtle.equipLeft() and turtle.equipRight(), as you may be able to devise a workaround with them.
Dragon53535 #3
Posted 01 December 2015 - 11:42 PM
In the meantime, these days it's possible to have turtles unequip and then re-equip items, in code (and checks are performed as you do so to add/remove turtle.craft() as appropriate). I suggest playing around with turtle.equipLeft() and turtle.equipRight(), as you may be able to devise a workaround with them.

I've had this bug before myself, and this DOES work. problem is, that I have no idea how it happens to cause such a peripheral loss until re-equip.
Mind you ofc, i'm not OP, but this should fix his problem

local function craft(num)
  if not turtle.craft then
    turtle.equipLeft()
    turtle.equipLeft()
    turtle.equipRight()
    turtle.equipRight()
  end
  turtle.craft(num)
end
Bomb Bloke #4
Posted 01 December 2015 - 11:55 PM
You'd want to ensure that an empty slot is selected first, but that's the idea, yeah. :)/>
geddeth #5
Posted 02 December 2015 - 11:23 PM
Thanks very much for your feedback, this is a very interesting issue :)/>

I added Dragon's craft() method to my code and replaced the calls to the core turtle.craft() with that. Unfortunately, my turtle bugs out at the call to turtle.craft() in there instead.
I checked that it calls the equipLeft() and equipRight() methods as intended, but still no luck. So it confirms that turtle.craft == false when this bug occurs, but I have to do something else in order to "reset" the turtle I suppose. I'll dig around some more.

Furthermore, using peripheral.getType() I determined that my Crafty turtle is indeed missing a workbench on its left. I tried equipping one with turtle.equipLeft() but I get the response "Not a valid upgrade".
This happens with a new turtle as well, so not sure if it's really the cause of my issue.
Bomb Bloke #6
Posted 03 December 2015 - 07:16 AM
So it confirms that turtle.craft == false when this bug occurs,

Well, "nil" rather than "false", but yeah.

Furthermore, using peripheral.getType() I determined that my Crafty turtle is indeed missing a workbench on its left. I tried equipping one with turtle.equipLeft() but I get the response "Not a valid upgrade".

"Not a valid upgrade" means that whatever is in the turtle's currently selected inventory slot isn't something it can equip. What, if anything, was in there? A precise ID with turtle.getItemDetail() would be ideal. Do you get the same behaviour with other items (eg diamond picks)? Do you get the same behaviour with no item in the selected slot? Do you get the same behaviour in SSP?

When you checked the turtle's left side, are you certain you shouldn't've checked the right? The left/right sides are relative to the turtle when it's facing away from you - this is as opposed to with regular computers, where they're relative to the system facing towards you.