Jump to content




[error] Error that doesn't exist


  • You cannot reply to this topic
3 replies to this topic

#1 Bossman201

  • New Members
  • 92 posts

Posted 12 June 2012 - 07: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.

#2 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 12 June 2012 - 08: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)


#3 Bossman201

  • New Members
  • 92 posts

Posted 12 June 2012 - 08: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.

#4 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 12 June 2012 - 10: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






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users