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

[error] Error that doesn't exist

Started by Bossman201, 12 June 2012 - 06:47 PM
Bossman201 #1
Posted 12 June 2012 - 08:47 PM
Here's my code, error message "bios:206: [string "recipe"]:9: ')' expected:
function craft(rItem[1], rItem[2], rItem[3], rItem[4], rItem[5], rItem[6], rItem[7], rItem[8], rItem[9])
print(" - - -")
print("|" ..  rItem[1] .. "|" .. rItem[2] .. "|" .. rItem[3] .. "|")
print(" - - -")
print("|" ..  rItem[4] .. "|" .. rItem[5] .. "|" .. rItem[6] .. "|")
print(" - - -")
print("|" ..  rItem[7] .. "|" .. rItem[8] .. "|" .. rItem[9] .. "|")
print(" - - -")
end

Line 9 is on the function declaration.
MysticT #2
Posted 12 June 2012 - 09:22 PM
You can't use [ or ] in variable names, so the function declaration is wrong. I think a table would be better to do that:

function craft(rItem)
print(" - - -")
print("|" ..  rItem[1] .. "|" .. rItem[2] .. "|" .. rItem[3] .. "|")
print(" - - -")
print("|" ..  rItem[4] .. "|" .. rItem[5] .. "|" .. rItem[6] .. "|")
print(" - - -")
print("|" ..  rItem[7] .. "|" .. rItem[8] .. "|" .. rItem[9] .. "|")
print(" - - -")
end
Then you just need to make a table and use it as an argument to the function call:

local t = {}
t[1] = "..."
t[2] = "..."
...
t[9] = "..."
craft(t)
Bossman201 #3
Posted 12 June 2012 - 09:38 PM
I'm confused. Your code looks exactly like what I did, except you pass the entire table/array over to the function instead of values.
MysticT #4
Posted 12 June 2012 - 11:09 PM
Yes, and it does exactly the same. It just looks better something like:

craft(t)
than:

craft(var1, var2, var3, var4, var5, var6, var7, var8, var9)
You can do it if you want, but don't use [ and ] in variable names, cause it means you are trying to index the variable. You must do it like this:

function craft(rItem1, rItem2, rItem3, rItem4, rItem5, rItem6, rItem7, rItem8, rItem9)
  print(" - - -")
  print("|" ..  rItem1 .. "|" .. rItem2 .. "|" .. rItem3 .. "|")
  print(" - - -")
  print("|" ..  rItem4 .. "|" .. rItem5 .. "|" .. rItem6 .. "|")
  print(" - - -")
  print("|" ..  rItem7 .. "|" .. rItem8 .. "|" .. rItem9 .. "|")
  print(" - - -")
end