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

Making A Function Broke My Script

Started by frostthejack, 23 September 2013 - 08:30 PM
frostthejack #1
Posted 23 September 2013 - 10:30 PM
so im working on this program for redstone in motion to power a quarry or a digging machine just in general and i made a working program you tell it how many blocks to dig and which direction and press enter

here is the basic program

http://pastebin.com/nEBySJB3


local down = 0
local up = 1
local north = 2
local south = 3
local west = 4
local east = 5
if
fs.exists("count")
then
h = fs.open("count", "r")
count = h.readLine("count")
h.close()
h = fs.open("distance", "r")
distance = h.readLine("distance")
h.close()
h = fs.open("direction", "r")
direction = h.readLine("direction")
h.close()

else
while true do
print("How far shall i dig master?")
local distance = read()
distance = tonumber(distance)
if
type(distance) == "number"
then
h = fs.open("distance", "w")
h.write(distance)
h.close()
break
else
print("I am terribly sorry sir but that is not a number")
end
end
while true do
print("what direction")
direction = read()
if
(direction == "down") or
(direction == "up") or
(direction == "north") or
(direction == "south") or
(direction == "west") or
(direction == "east")
then
h = fs.open("direction", "w")
h.write(direction)
h.close()
print("You have 10 seconds to get off the rig")
sleep(10)
break
else
print("not a direction")
end
end

h = fs.open("count", "w")
h.write("0")
h.close()
count = 0
end



if
count == distance
then
print("quarry done")
fs.delete("count")
else
drive = peripheral.wrap("top")
drive.move(direction, false, false)
count = count + "1"
h = fs.open("count", "w")
h.write(count)
h.close()
end

that code works as intended

however after finishing this i decided to make functions to clean up the code make it look nicer and now it wont work properly it seems like count and distance are set to the exact same variable or something because it just see's it as done. if i change it to count >= distance it gives a strange error.

http://pastebin.com/0znM4b1c


--this is a simple redstone in motion
--quarry script and can be used
--for many purposes
--your startup file should read
-- first line
-- sleep(4)
--second line
-- shell.run("this script")
-- you can reduce the sleep time but
-- i dont recomend anything less then 2

-- these are for direction
local down = 0
local up = 1
local north = 2
local south = 3
local west = 4
local east = 5
-- function to read from file
function variableRead(fileName, variableName)
h = fs.open(fileName, "r")
variableName = h.readLine(fileName)
h.close()
end
--function to save to file
function variableSave(fileName, variable)
h = fs.open(fileName, "w")
h.write(variable)
h.close()
end
-- lets check if we are already doing something
if
fs.exists("count")
then
variableRead("count", count)
variableRead("distance", distance)
variableRead("direction", direction)

else
-- looks like we are not doing anything yet
-- what do we want to do
while true do
-- how many blocks to dig
print("How far shall i dig master?")
local distance = read()
distance = tonumber(distance)
if
type(distance) == "number"
then
variableSave("distance", distance)
break
else
-- a number was not entered
print("I am terribly sorry sir but that is not a number")
end
end
while true do
-- which direction we going
print("what direction")
direction = read()
if
-- list of choices
(direction == "down") or
(direction == "up") or
(direction == "north") or
(direction == "south") or
(direction == "west") or
(direction == "east")
then
variableSave("direction", direction)
print("You have 10 seconds to get off the rig")
sleep(10)
break
else
-- entered an incorrect direction
print("not a correct direction")
end
end
--create the count file then continue
variableSave("count", "0")
end


-- check count file to see how many
-- times we have moved in chosen
-- direction
if
count == distance
then
-- done with moving delete count file
-- to start over
print("quarry done")
fs.delete("count")
else
-- we are not done move in direction
-- then add one to count and start over
drive = peripheral.wrap("top")
drive.move(direction, false, false)
count = count + "1"
variableSave("count", count)
end



any help would be greatly appreciated
Lyqyd #2
Posted 23 September 2013 - 10:43 PM
I'm really not sure what your variableRead function is supposed to do, but I can tell you that variables don't work the way that you think they do. You should return the value read from the file and set the variable from that where you call the function. You can't pass a "variable name" to a function, unless you're passing it a table index or some other weird way of doing things.
frostthejack #3
Posted 24 September 2013 - 03:51 PM
I just called it variable read to make it easy for me to remember what it does is it opens a file determined by fileName then reads it and sets it as the variable determined by variableName.

so this

function variableRead(fileName, variableName)
h = fs.open(fileName, "r")
variableName = h.readLine(fileName)
h.close()
end
variableRead("distance", distance)


should be the same as


h = fs.open("distance", "r")
distance = h.readLine("distance")
h.close()

but for some reason it does not appear to be working like that
Yevano #4
Posted 24 September 2013 - 04:26 PM
I just called it variable read to make it easy for me to remember what it does is it opens a file determined by fileName then reads it and sets it as the variable determined by variableName.

so this

function variableRead(fileName, variableName)
h = fs.open(fileName, "r")
variableName = h.readLine(fileName)
h.close()
end
variableRead("distance", distance)


should be the same as


h = fs.open("distance", "r")
distance = h.readLine("distance")
h.close()

but for some reason it does not appear to be working like that

Does the file store multiple vars, or does it store many and the second parameter you give it determines which is read? Either way, you're not using the file handle correctly. readLine doesn't take any arguments, and just reading one line would get whatever is at the top of the file, and not whatever var you want to access (if that's what you're trying to do).

Read http://www.computerc...o/wiki/Fs_(API) and http://www.computerc...fo/wiki/Fs.open.
Lyqyd #5
Posted 24 September 2013 - 10:00 PM
I just called it variable read to make it easy for me to remember what it does is it opens a file determined by fileName then reads it and sets it as the variable determined by variableName.

so this

function variableRead(fileName, variableName)
h = fs.open(fileName, "r")
variableName = h.readLine(fileName)
h.close()
end
variableRead("distance", distance)


should be the same as


h = fs.open("distance", "r")
distance = h.readLine("distance")
h.close()

but for some reason it does not appear to be working like that

No, it shouldn't be. You can't pass a variable name. To turn that into a function, you'd do this:


function readFile(filename)
  local retval
  local handle = fs.open(filename, "r")
  if handle then
    retval = handle.readLine()
    handle.close()
  end
  return retval
end

distance = readFile("distance")

See how we return the value, and put that value into a variable where we call the function? Note also that readLine() doesn't accept any arguments, so it will ignore whatever you pass it. I'm not sure what you thought h.readLine("distance") would do.
frostthejack #6
Posted 25 September 2013 - 01:01 PM
ah i have always but the name of the file in the readfile spot its how i was taught.

and i see i need to set the variable outside of the function that makes sense thanks guys you have been loads of help
frostthejack #7
Posted 25 September 2013 - 03:19 PM
well even with that function it still doesnt work

here is the current code
http://pastebin.com/0znM4b1c

i replaced the function for read with the one you gave my lyqyd and the function appears to work but when it gets to the if count == distance i dont know what happens but it either skips it or it is reading them as the same number and it is just saying hey im done and it quits out

if i change count == distance to count >= distance i get a attempt to compare __le on nill and nill which tells me it isnt reading from the files in the first place

again any help would be greatly appreciated but if what im trying to do is not possible it wont be the end of the world
Lyqyd #8
Posted 25 September 2013 - 05:42 PM
The first time you run it, they will both be nil. You don't define count before that point on first run, and you define distance as local to the loop you create it in. After the initial run, you'll need to use tonumber() to convert the values read from the file from strings to numbers.
frostthejack #9
Posted 25 September 2013 - 06:20 PM
oh so i need to remove local so the rest of the program will be able to read it because since the first time it read them as both nill it deletes the count file and restarts ill give that a try
frostthejack #10
Posted 27 September 2013 - 01:09 PM
I cant believe it was all because of a simple mistake using local in a loop then calling it outside the loop. I feel stupid. any way this post can be closed now thank you so much for you assistance