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

I'm trying to make a spiral mining program, but I'm running into problems

Started by Novakis, 03 April 2013 - 11:05 AM
Novakis #1
Posted 03 April 2013 - 01:05 PM
I am trying to write a program for my mining ship so it will do a spiral pattern, but I'm having trouble with making persistent variables. When I run the program, it says "miningProgram:18: attempt to index ? (a nil value)". Any help would be awesome!


function saveData(data, name)
local file = fs.open(name, "w")
file.write(textutils.serialize(data))
file.close()
end
function loadData(name)
local file = fs.open(name, "r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
local direction = loadData(directionS)
local movesDo = loadData(movesDoS)
local moves = loadData(movesS)
local switchDirection = loadData(swichDirectionS)
while true do
shell.run("quarry")
os.pullEvent("redstone")
shell.run("endQuarry")
while moves ~= movesDo do
  if direction == 1 then
   shell.run("forward")
  elseif direction == 2 then
   shell.run("right")
  elseif direction == 3 then
   shell.run("reverse")
  elseif direction == 4 then
   shell.run("left")
  end
  moves = moves + 1
  saveData(moves, "moves")
end
if direction == 4 then
  direction = direction - 3
else
  direction = direction + 1
end
if switchDirection then
  movesDo = movesDo + 1
  switchDirection = false
else
  switchDirection = true
end
moves = 0
saveData(direction, "direction")
saveData(movesDo, "movesDo")
saveData(moves, "moves")
saveData(switchDirection, "swichDirection")
end
Engineer #2
Posted 03 April 2013 - 01:09 PM
Are you sure endQuarry and Quarry exists?
Bubba #3
Posted 03 April 2013 - 01:11 PM
Are you sure endQuarry and Quarry exists?

If that were the case then his error would be "No such program exists".

Error is here I think:

local direction = loadData(directionS)
local movesDo = loadData(movesDoS)
local moves = loadData(movesS)
local switchDirection = loadData(swichDirectionS)

.You need to enclose the "directionS"/"movesDoS"/etc. in quotes because there is no variables that correlate with those.
TheArchitect #4
Posted 03 April 2013 - 01:17 PM
Did you type this by hand or copy-pasted it? Because if it is the latter, you're missing all the semicolons.
Bubba #5
Posted 03 April 2013 - 01:22 PM
Did you type this by hand or copy-pasted it? Because if it is the latter, you're missing all the semicolons.

… There is nowhere in the program that requires semicolons (Are you sure you're thinking of the right character? This -> ; <- is a semicolon)
Engineer #6
Posted 03 April 2013 - 01:36 PM
Did you type this by hand or copy-pasted it? Because if it is the latter, you're missing all the semicolons.

… There is nowhere in the program that requires semicolons (Are you sure you're thinking of the right character? This -> ; <- is a semicolon)
He means doing like so:

local t = {}
for i=1,5 do t[i] = {}; t[i][i] = i end

but to my testings you can always leav the semi column. Except for 'return end'
ForTheKremlin #7
Posted 03 April 2013 - 01:37 PM
Did you type this by hand or copy-pasted it? Because if it is the latter, you're missing all the semicolons.
Lua programming language does not require semicolons in any case that I know of. You're probably thinking of C++ or something similar.
TheArchitect #8
Posted 03 April 2013 - 01:38 PM
…do you mean I've been throwing semicolons in all over the place FOR NOTHING?

*throws keyboard on the floor and stomps out the door*
Bubba #9
Posted 03 April 2013 - 01:39 PM
There is only one place in Lua that requires semi-colons (in fact, even these are optional - you can use commas instead), and that is inside of tables.
Example:

local a_table = {
  var = 5;
  other=6;
  [3] = 1234;
}
PixelToast #10
Posted 03 April 2013 - 03:44 PM
…do you mean I've been throwing semicolons in all over the place FOR NOTHING?
yea, lua only requires a single whitespace character to seperate function calls
can be spaces, semicolons, newlines, tabs and there are some others
not in tables though, those require commas or semi colons (thanks bubba, didnt know that)
whitespaces are ignored in tables (methinks this is because it will allow nice table formatting)
Novakis #11
Posted 03 April 2013 - 05:20 PM
Are you sure endQuarry and Quarry exists?

If that were the case then his error would be "No such program exists".

Error is here I think:

local direction = loadData(directionS)
local movesDo = loadData(movesDoS)
local moves = loadData(movesS)
local switchDirection = loadData(swichDirectionS)

.You need to enclose the "directionS"/"movesDoS"/etc. in quotes because there is no variables that correlate with those.

I tried adding the quotes but it still gives me the same error except now it's: "miningProgram:9: attempt to index ? (a nil value)"

Here is the new code:

function saveData(data, name)
local file = fs.open(name, "w")
file.write(textutils.serialize(data))
file.close()
end
function loadData(name)
local file = fs.open(name, "r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
local direction = loadData("directionS")
local movesDo = loadData("movesDoS")
local moves = loadData("movesS")
local switchDirection = loadData("swichDirectionS")
while true do
shell.run("quarry")
os.pullEvent("redstone")
shell.run("endQuarry")
while moves ~= movesDo do
  if direction == 1 then
   shell.run("forward")
  elseif direction == 2 then
   shell.run("right")
  elseif direction == 3 then
   shell.run("reverse")
  elseif direction == 4 then
   shell.run("left")
  end
  moves = moves + 1
  saveData(moves, "moves")
end
if direction == 4 then
  direction = direction - 3
else
  direction = direction + 1
end
if switchDirection then
  movesDo = movesDo + 1
  switchDirection = false
else
  switchDirection = true
end
moves = 0
saveData(direction, "direction")
saveData(movesDo, "movesDo")
saveData(moves, "moves")
saveData(switchDirection, "swichDirection")
end
Bubba #12
Posted 03 April 2013 - 05:26 PM
Okay, well first off it looks like you're trying to open a file that does not exist on line 9. Check the name again and make sure that it matches EXACTLY. Lua is case sensitive.

Secondly, I would highly suggest changing your loadData function to something like this:

function loadData(name)
	local file = fs.open(name, "r")
	assert(file, "Oh noes! I can't open that file. It does not exist")
	local data = file.readAll()
	file.close()
	return textutils.unserialize(data)
end

This makes sure that it will give you a more specific error so that you can tell if you're trying to open a file that does not exist.

Another thing you should do is check that the file is a valid serialized table with something along the lines of this:

local directions = loadData("sDirections.txt")
assert(directions, "Oops? I guess that wasn't a valid serialized table.")

Just in case you are unaware of what assert does, it is essentially the same as this:

if not variable then
   error("This is an error message!")
end
Novakis #13
Posted 03 April 2013 - 05:40 PM
Okay, well first off it looks like you're trying to open a file that does not exist on line 9. Check the name again and make sure that it matches EXACTLY. Lua is case sensitive.

Secondly, I would highly suggest changing your loadData function to something like this:

function loadData(name)
	local file = fs.open(name, "r")
	assert(file, "Oh noes! I can't open that file. It does not exist")
	local data = file.readAll()
	file.close()
	return textutils.unserialize(data)
end

This makes sure that it will give you a more specific error so that you can tell if you're trying to open a file that does not exist.

Another thing you should do is check that the file is a valid serialized table with something along the lines of this:

local directions = loadData("sDirections.txt")
assert(directions, "Oops? I guess that wasn't a valid serialized table.")

Just in case you are unaware of what assert does, it is essentially the same as this:

if not variable then
   error("This is an error message!")
end

I entered your code, and it came up as it couldn't read the file. I thought that if it didn't exist that it would just make a clean slate, what do I have to do to make a valid serialized table?
Bubba #14
Posted 03 April 2013 - 05:54 PM
I entered your code, and it came up as it couldn't read the file. I thought that if it didn't exist that it would just make a clean slate, what do I have to do to make a valid serialized table?

You will make a new file if you are trying to open a file in "w" (write) mode, but "r" (read) mode will not create files. Valid serialized tables are made with textutils.serialize(table).

Here's an example of serializing:

local an_example = {
   ["key"] = "value";
   [1] = "test";
}
local str_an_example = textutils.serialize(an_example)
local f = fs.open("a_file", "w")
f.write(str_an_example)
f.close()
--Now we can open the file
local f = fs.open("a_file", "r")
local content = f.readAll()
f.close()
local new_table = textutils.unserialize(content) --This has the same key/value pairs as the 'an_example' table
Novakis #15
Posted 03 April 2013 - 06:38 PM
I entered your code, and it came up as it couldn't read the file. I thought that if it didn't exist that it would just make a clean slate, what do I have to do to make a valid serialized table?

You will make a new file if you are trying to open a file in "w" (write) mode, but "r" (read) mode will not create files. Valid serialized tables are made with textutils.serialize(table).

Here's an example of serializing:

local an_example = {
   ["key"] = "value";
   [1] = "test";
}
local str_an_example = textutils.serialize(an_example)
local f = fs.open("a_file", "w")
f.write(str_an_example)
f.close()
--Now we can open the file
local f = fs.open("a_file", "r")
local content = f.readAll()
f.close()
local new_table = textutils.unserialize(content) --This has the same key/value pairs as the 'an_example' table

Thank, I'll give it a try.
Dlcruz129 #16
Posted 03 April 2013 - 07:09 PM
…do you mean I've been throwing semicolons in all over the place FOR NOTHING?

*throws keyboard on the floor and stomps out the door*

Yep!
Novakis #17
Posted 05 April 2013 - 06:19 AM
I entered your code, and it came up as it couldn't read the file. I thought that if it didn't exist that it would just make a clean slate, what do I have to do to make a valid serialized table?

You will make a new file if you are trying to open a file in "w" (write) mode, but "r" (read) mode will not create files. Valid serialized tables are made with textutils.serialize(table).

Here's an example of serializing:

local an_example = {
   ["key"] = "value";
   [1] = "test";
}
local str_an_example = textutils.serialize(an_example)
local f = fs.open("a_file", "w")
f.write(str_an_example)
f.close()
--Now we can open the file
local f = fs.open("a_file", "r")
local content = f.readAll()
f.close()
local new_table = textutils.unserialize(content) --This has the same key/value pairs as the 'an_example' table

Ok, I change the code up a bit to make it so if one of the files doesn't exist it makes all the files with preset values:


if not fs.exists("direction") then
local f = fs.open("direction", "w")
f.write(textutils.serialize(1))
f.close()
f = fs.open("movesDo", "w")
f.write(textutils.serialize(1))
f.close()
f = fs.open("moves", "w")
f.write(textutils.serialize(0))
f.close()
f = fs.open("switchDirection", "w")
f.write(textutils.serialize(1))
f.close()
end
function saveData(data, name)
local file = fs.open(name, "w")
file.write(textutils.serialize(data))
file.close()
end
function loadData(name)
	    local file = fs.open(name, "r")
	    assert(file, "ZOMG, file not found!")
	    local data = file.readAll()
	    file.close()
	    return textutils.unserialize(data)
end
local direction = loadData("direction")
local movesDo = loadData("movesDo")
local moves = loadData("moves")
local switchDirection = loadData("swichDirection")
while true do
shell.run("quarry")
os.pullEvent("redstone")
shell.run("endQuarry")
while moves ~= movesDo do
  if direction == 1 then
   shell.run("forward")
  elseif direction == 2 then
   shell.run("right")
  elseif direction == 3 then
   shell.run("reverse")
  elseif direction == 4 then
   shell.run("left")
  end
  moves = moves + 1
  saveData(moves, "moves")
end
if direction == 4 then
  direction = direction - 3
else
  direction = direction + 1
end
if switchDirection == 1 then
  movesDo = movesDo + 1
  switchDirection = 0
else
  switchDirection = 1
end
moves = 0
saveData(direction, "direction")
saveData(movesDo, "movesDo")
saveData(moves, "moves")
saveData(switchDirection, "swichDirection")
end

The program makes the files with the values in them, but still reports that the files don't exist. Does unserialize require something special to view the values?
Bubba #18
Posted 05 April 2013 - 06:37 AM
Edit: What line is your program erroring on? I could see a few places that it might.

Well it looks like you open "direction" and add values to that but not to "moves", "movesDo", or "switchDirection". Try adding those in their too.

I also see that you use this bit of code for moving:

while moves ~= movesDo do
  if direction == 1 then
   shell.run("forward")
  elseif direction == 2 then
   shell.run("right")
  elseif direction == 3 then
   shell.run("reverse")
  elseif direction == 4 then
   shell.run("left")
  end

You'd be much better of using native turtle functions like so:

while moves ~= movesDo do
  if direction == 1 then
   turtle.forward()
  elseif direction == 2 then
   turtle.turnRight()
  elseif direction == 3 then
   turtle.turnRight()
   turtle.turnRight()
  elseif direction == 4 then
   turtle.turnLeft()
  end
Novakis #19
Posted 05 April 2013 - 06:51 AM
Edit: What line is your program erroring on? I could see a few places that it might.

Well it looks like you open "direction" and add values to that but not to "moves", "movesDo", or "switchDirection". Try adding those in their too.

I also see that you use this bit of code for moving:

while moves ~= movesDo do
  if direction == 1 then
   shell.run("forward")
  elseif direction == 2 then
   shell.run("right")
  elseif direction == 3 then
   shell.run("reverse")
  elseif direction == 4 then
   shell.run("left")
  end

You'd be much better of using native turtle functions like so:

while moves ~= movesDo do
  if direction == 1 then
   turtle.forward()
  elseif direction == 2 then
   turtle.turnRight()
  elseif direction == 3 then
   turtle.turnRight()
   turtle.turnRight()
  elseif direction == 4 then
   turtle.turnLeft()
  end

The error reads as: "miningProgram:24: ZOMG! File not found!" And I have it so it checks to see if direction exists then writes all the files needed, I've check them and they have the values I entered. Also this isn't a turtle: http://imgur.com/a/rw18L
Bubba #20
Posted 05 April 2013 - 07:13 AM
The error reads as: "miningProgram:24: ZOMG! File not found!" And I have it so it checks to see if direction exists then writes all the files needed, I've check them and they have the values I entered. Also this isn't a turtle: http://imgur.com/a/rw18L

Ah. Now I see. That's pretty neat!

But anyway, I think this is your problem

local switchDirection = loadData("swichDirection")

You spell switchDirection wrong. You're missing the t.
Novakis #21
Posted 05 April 2013 - 07:19 AM
The error reads as: "miningProgram:24: ZOMG! File not found!" And I have it so it checks to see if direction exists then writes all the files needed, I've check them and they have the values I entered. Also this isn't a turtle: http://imgur.com/a/rw18L

Ah. Now I see. That's pretty neat!

But anyway, I think this is your problem

local switchDirection = loadData("swichDirection")

You spell switchDirection wrong. You're missing the t.

OH JESUS CHRIST! That was it man, thank you for the excellent work! Is there some sort of survey that I can fill out to tell your employer how awesome you are?
Novakis #22
Posted 05 April 2013 - 07:41 AM
The error reads as: "miningProgram:24: ZOMG! File not found!" And I have it so it checks to see if direction exists then writes all the files needed, I've check them and they have the values I entered. Also this isn't a turtle: http://imgur.com/a/rw18L

Ah. Now I see. That's pretty neat!

But anyway, I think this is your problem

local switchDirection = loadData("swichDirection")

You spell switchDirection wrong. You're missing the t.

OH JESUS CHRIST! That was it, running just fine now. Thank you man for your exceptional work! If there is some sort of survey that I can fill out so your employer knows how well you are doing just send it to me.
Bubba #23
Posted 05 April 2013 - 07:41 AM
The error reads as: "miningProgram:24: ZOMG! File not found!" And I have it so it checks to see if direction exists then writes all the files needed, I've check them and they have the values I entered. Also this isn't a turtle: http://imgur.com/a/rw18L

Ah. Now I see. That's pretty neat!

But anyway, I think this is your problem

local switchDirection = loadData("swichDirection")

You spell switchDirection wrong. You're missing the t.

OH JESUS CHRIST! That was it man, thank you for the excellent work! Is there some sort of survey that I can fill out to tell your employer how awesome you are?

XD I wish there was. Glad I could help! :)/>