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

Error using Lua's unpack() and shell.run()

Started by SavinaRoja, 27 July 2012 - 06:45 PM
SavinaRoja #1
Posted 27 July 2012 - 08:45 PM
I am working on an API for operating large numbers of turtles, but am getting very hung up on this particular error. I am not new to programming, but I am only a couple days into using Lua. I am having issues with passing variable arguments to shell.run().

The relevant code is posted here: https://gist.github....471fcc771d3b375

I place this in the correct API folder to make it accessible to my machines, then enter the game and attempt the following.
  1. Boot turtle, start swarm.worker()
  2. Boot computer, start swarm.queen("tunnel", "4")
The queen method completes successfully, but the worker method gives the error
swarm:121: attempt to index ? (a nil value)
I am not sure what is going wrong. The interactive lua prompt has no problems with either
shell.run(unpack{"tunnel", "4"})
or
shell.run("tunnel", unpack({4}))
I'm sure the trouble is with my own unfamiliarity with Lua, I would greatly appreciate help and education.
OmegaVest #2
Posted 27 July 2012 - 08:52 PM
I don't think you NEED to use unpack there. Unless it is already in a table/string. Just put down the parameters with commas separating them.



shell.run("tunnel", 4)
SavinaRoja #3
Posted 27 July 2012 - 08:58 PM
As it is meant to support arguments of a variable quantity, I would like to use a method suitable to arbitrary table length.
MysticT #4
Posted 27 July 2012 - 09:54 PM
You can't use shell from an api, because shell is not really an api. You can use os.run., but it won't find the programs, it needs the absolute path.
SavinaRoja #5
Posted 27 July 2012 - 10:15 PM
You can't use shell from an api, because shell is not really an api. You can use os.run., but it won't find the programs, it needs the absolute path.
I'll give this a try, I hadn't considered that.
SavinaRoja #6
Posted 27 July 2012 - 11:20 PM
SOLVED - The problem was essentially what MysticT said. Shell is not a true API and it can cause problems based on how it is called. The ultimate solution was to simply to implement the turtle functions within the API itself, or another complementary one. The code has been updated here http://pastebin.com/NJvbvTb3, where I basically customized the tunnel program that comes standard.