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

Unexpected Symbol error

Started by TehNoiseBomb, 13 April 2016 - 10:06 PM
TehNoiseBomb #1
Posted 14 April 2016 - 12:06 AM
So, I have finally completed a farming program that should be easy to expand and edit as needed, until I tried running it and now have a syntax error I don't know how to fix. Yes, I looked at other threads and couldn't narrow down what my issue is being caused by, now onward then…

The error code I am getting is as follows:

farm9x9:12: bios:14: [string "refuel"]:8: unexpected symbol

Here is the pastebin of the farm9x9 (main) file in question: http://pastebin.com/mQkvff7G
Here is the pastebin of the refuel file: http://pastebin.com/z3dmLu3L

I've tried changing the way that I have the fuel level passed into the "function" file with no change, so help please?

Raw Code:
Spoiler

--@Author: Zach Combs aka TehNoiseBomb
--@Version: 1.2
local inputs = {...}
local crop, seed, fullyGrown, sleepTime --variables passed in (inputs)
local moveLine = assert(loadfile("moveLine")) --Load the moveLine code as a variable that can be called as a function
local nextRow = assert(loadfile("nextRow")) --Load the nextRow code as a variable that can be called as a function
local moveHome = assert(loadfile("moveHome")) --Load the moveHome code as a variable that can be called as a function
local getSeed = assert(loadfile("getSeed")) --Load the getSeed code as a variable that can be called as a function
local itemDump = assert(loadfile("itemDump")) --Load the itemDump code as a variable that can be called as a function
local refuel = assert(loadfile("refuel")) --Load the refuel code as a variable that can be called as a function
local east = true --boolean flag determining if the turtle is facing east(true) or west(false) -> You can check in F3 mode, when placed the turtle is facing the same way as the player
local seedData = getSeed() --Get the data about the seed in the turtle's inventory
local fuelLevel = turtle.getFuelLevel() --Get the amount of fuel the turtle has
--Assign passed in variables here
--'k' is the number, 'v' is the variable
for k, v in pairs(inputs) do
if k == 1 then crop = v end
if k == 2 then seed = v end
if k == 3 then fullyGrown = v end
if k == 4 then sleepTime = v end
end
print("Working on crop: ", crop)
print("Working with seed: ", seed)
--While the turtle has fuel, repeat the following steps:
-- Print the amount of fuel the turtle has
-- Reset the turning flag to true
-- Get how much fuel the turtle has
-- If the turtle has seeds in it's inventory:
--  Move through the first row and get how many seeds the turtle has in it's inventory
--  Starting in the first row, repeat eight times:
--   Move to the next row on the right and get set the new orientation to the east slag
--   Move through the row and get how many seeds the turtle has in it's inventory
--  Move to the home position
--  Move to the chest, dump excess inventory and transfer the seeds to the first slot in the turtle's inventory
--  Move to the fuel chest and refuel
--  Get how much fuel the turtle has
-- Sleep for the passed in amount of time
while fuelLevel > 0 do
print("Fuel left... ", fuelLevel)
east = true
fuelLevel = turtle.getFuelLevel()
if seedData.count > 0 then
  seedData = moveLine(crop, seed, fullyGrown)
  for i = 0, 7 do
   east = nextRow(east)
   seedData = moveLine(crop, seed, fullyGrown)
  end
  moveHome()
  seedData = itemDump(seedData)
  fuelLevel = refuel(fuelLevel)
end
os.sleep(sleepTime)
end

Edit: added link to refuel pastebin (my bad)
Edited on 13 April 2016 - 10:58 PM
Bomb Bloke #2
Posted 14 April 2016 - 12:46 AM
The file in question is "refuel", not the one you're displaying here. Check around line eight within that script.
Dragon53535 #3
Posted 14 April 2016 - 12:49 AM
Like BB said, the error is in your refuel file. That's kinda why assert gave you that error. Loadfile returned nil, and the error message. Assert saw that loadfile returned an error message, and promptly displayed it on screen.
TehNoiseBomb #4
Posted 14 April 2016 - 01:00 AM
The file in question is "refuel", not the one you're displaying here. Check around line eight within that script.

Fixed the post, http://pastebin.com/z3dmLu3L

Like BB said, the error is in your refuel file. That's kinda why assert gave you that error. Loadfile returned nil, and the error message. Assert saw that loadfile returned an error message, and promptly displayed it on screen.

I remember reading that and forgot about it, thanks for refreshing my memory on it.
Bomb Bloke #5
Posted 14 April 2016 - 01:07 AM
Mind you, I can see a "refuel" script elsewhere in your pastebin account, but I can't see how it'd generate this error. Beats me as to whether it matches the one you're actually using…?

(Edit: Ah, I got ninja'd, and it apparently is.)

One problem I can see with it is that refuel attempts to loadfile refuel. That'll spark an infinite loop which'll probably lead to the system shutting down. I'm not entirely sure why you're doing this, either; I see you're trying to assign what refuel() returns to fuelLevel at some point, but refuel() doesn't actually return anything, so….

You can also pack down this sort of thing:

local inputs = {...}
local crop, seed, fullyGrown, sleepTime --variables passed in (inputs)

.
.
.

for k, v in pairs(inputs) do
 if k == 1 then crop = v end
 if k == 2 then seed = v end
 if k == 3 then fullyGrown = v end
 if k == 4 then sleepTime = v end
end

… like so:

local crop, seed, fullyGrown, sleepTime = ...
Edited on 13 April 2016 - 11:08 PM
TehNoiseBomb #6
Posted 14 April 2016 - 01:12 AM
Mind you, I can see a "refuel" script elsewhere in your pastebin account, but I can't see how it'd generate this error. Beats me as to whether it matches the one you're actually using…?

(Edit: Ah, I got ninja'd, and it apparently is.)

One problem I can see with it is that refuel attempts to loadfile refuel. That'll spark an infinite loop which'll probably lead to the system shutting down. I'm not entirely sure why you're doing this, either; I see you're trying to assign what refuel() returns to fuelLevel at some point, but refuel() doesn't actually return anything, so….

You can also pack down this sort of thing:

local inputs = {...}
local crop, seed, fullyGrown, sleepTime --variables passed in (inputs)

.
.
.

for k, v in pairs(inputs) do
if k == 1 then crop = v end
if k == 2 then seed = v end
if k == 3 then fullyGrown = v end
if k == 4 then sleepTime = v end
end

… like so:

local crop, seed, fullyGrown, sleepTime = ...

I saw your problem, i copy pasted the wrong file, oops. The correct one: http://pastebin.com/z3dmLu3L

I also did not know you could use:
local crop, seed, fullyGrown, sleepTime = ...
I'm a programmer but I haven't worked much with Lua so I'm still learning the syntax.
Dragon53535 #7
Posted 14 April 2016 - 02:04 AM
Does the error still occur?
TehNoiseBomb #8
Posted 14 April 2016 - 02:06 AM
Does the error still occur?
If you mean the issue BB brought up, that was only a mistake on my part when putting the code up on pastebin. The issue is persisting.
Dragon53535 #9
Posted 14 April 2016 - 02:46 AM
If it's the same file, then line 8 should have a problem. The only thing I notice on line 8 that could possibly cause an error if the lua interpreter is stupid is the comma in the comment.
Bomb Bloke #10
Posted 14 April 2016 - 03:00 AM
He's actually changed that file a couple of times now (eg, attempting to implement that suggestion I'd made above re the assignments from …). Is it really still giving the same error, verbatim?
TehNoiseBomb #11
Posted 14 April 2016 - 03:33 AM
I fixed that error and another, now I am getting a

getSeed:17: attempt to index ? (a nil value)
. This is occuring in a loop through the turtle's inventory. For some reason I can't get it to break out of the loop like I expected it to do when I return seeds: (seed is the string unlocalized name passed into the function)
Spoiler

for i = 1, 15 do
if turtle.getItemDetail(i).name == seed then --ERROR
  seeds["slot"] = i
  seeds["count"] = turtle.getItemDetail(i).count
  seeds["name"] = turtle.getItemDetail(i).name
  seeds["damage"] = turtle.getItemDetail(i).damage --This is included to preserve all the data from the selected item
  return seeds
end
end
Edited on 14 April 2016 - 01:34 AM
Bomb Bloke #12
Posted 14 April 2016 - 03:38 AM
Make sure turtle.getItemDetail() is returning a table before you attempt to treat it like one.

local curSlot = turtle.getItemDetail(i)
if curSlot and curSlot.name == seed then  --# if curSlot is non-nil, then index in for the "name" key and compare that to the "seed" variable...
 ...

Note that assigning the return value avoids calling "turtle.getItemDetail(i)" multiple times when once suffices.
TehNoiseBomb #13
Posted 14 April 2016 - 03:46 AM
Make sure turtle.getItemDetail() is returning a table before you attempt to treat it like one.

local curSlot = turtle.getItemDetail(i)
if curSlot and curSlot.name == seed then  --# if curSlot is non-nil, then index in for the "name" key and compare that to the "seed" variable...
...

That worked, now I just have to find a logic error somewhere, my program is working but seems to be looping after it prints the first "Fuel left… " line.

Note that assigning the return value avoids calling "turtle.getItemDetail(i)" multiple times when once suffices.

That was exactly what I was going for.

Thanks for the help by the way.
Edited on 14 April 2016 - 01:46 AM
TehNoiseBomb #14
Posted 14 April 2016 - 03:58 AM
OK now I'm getting another error in getSeed the first time I call it from farm9x9:
local seedData = getSeed(seed) --Get the data about the seed in the turtle's inventory
farm9x9 code

ERROR: getSeed:18: index expected, got nil


for i = 1, 15 do
if turtle.getItemDetail(i) and turtle.getItemDetail(i).name == seed then
  seeds["slot"] = i --> line 18
  seeds["count"] = turtle.getItemDetail(i).count
  seeds["name"] = turtle.getItemDetail(i).name
  seeds["damage"] = turtle.getItemDetail(i).damage --This is included to preserve all the data from the selected item
  return seeds
end
end
getSeed code

It seems like the error is referring to the "index" as i because before this code runs, the table should be blank with that like being the first entry into it
Dragon53535 #15
Posted 14 April 2016 - 05:12 AM
That doesn't look like it could cause that error, however like BB said, consolidate those turtle.getItemDetail calls into one. It will very much speed up your program as you don't have to run a function more than one time.

Show the rest of your code by the way.


for i = 1, 15 do
  local currSlot = turtle.getItemDetail(i) --#One function call, the ENTIRE table that's returned gets saved to this. Which means that we don't have to call getItemDetail more than once
  if currSlot and currSlot.name == seed then
	seeds.slot = i --#seeds.slot and seeds["slot"] are the same. You just can't use spaces and such.
	seeds.count = currSlot.count
	seeds.name = currSlot.name
	seeds.damage = currSlot.damage
	return seeds
  end
end
Edited on 14 April 2016 - 03:14 AM
Bomb Bloke #16
Posted 14 April 2016 - 05:56 AM
Heck, easier again to just return the perfectly good table turtle.getItemDetail() returned, directly:

for i = 1, 15 do
  local currSlot = turtle.getItemDetail(i)
  if currSlot and currSlot.name == seed then
        currSlot.slot = i
        return currSlot
  end
end

As for your latest error, that'd indicate that you never defined "seeds" to be a table - though the above code removes any such need initialise it at all.
TehNoiseBomb #17
Posted 14 April 2016 - 01:06 PM
Heck, easier again to just return the perfectly good table turtle.getItemDetail() returned, directly:

for i = 1, 15 do
  local currSlot = turtle.getItemDetail(i)
  if currSlot and currSlot.name == seed then
		currSlot.slot = i
		return currSlot
  end
end

As for your latest error, that'd indicate that you never defined "seeds" to be a table - though the above code removes any such need initialise it at all.

Seems like it's working now, thanks for all the help!