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

fs api help

Started by The_Awe35, 23 March 2013 - 04:11 PM
The_Awe35 #1
Posted 23 March 2013 - 05:11 PM
I have a program, and on the startup, I want it to check witha file to see different things, such as :
if it is running
if it is in the middle of executing code

actually, I think that it. I want to do this by, when it starts the the code, it will say if it is executing that code from a variable in another file, or if it is idle.
Now, the problem is the fs api. I have no idea how to use it, and I have found little help to help me here. Mostly its the path it wants. I don't know the correct syntax to put the path as. Also, I want to edit variables in the other file, and then read them upon startup. As I will have multiple variables, how could I do this without mixing up the variables?

A little more info in the program:
I am making a strip mining program, with the turtle having one large main branch, and stemming out, with pneumatic pipes sending the junk out, and sending more pipes and torches to it. I want to control it with a computer remotely, telling how much to expand it, or to just stop.

Any suggestions on how to do this better would be much appreciated.
Spongy141 #2
Posted 30 March 2013 - 05:30 PM
You won't use fs API to load a variable from an other file, since you can't, the most you could do is have a different file for every variable you want, such as

file = fs.open("file","r") -- "file" [inside the ()] is the file name, in this case the file with the specificity variable you want, the file MUST exist.
local {Variable} = file.readLine()
This only works if you want to have a file for every variable that you will use. Or you might want to use

os.loadAPI("FILENAME")
This will allow you to load a multiple variable from an other file.
Then just simply use the variables, but I usually get errors that way, so I suggest making a different file for every variable with fs API.
Unit158 #3
Posted 30 March 2013 - 05:41 PM
loadstring() might help you.
It can be a bit mean to people who do not understand meta tables very well though.

Also, I am not sure why you would want to use a file, and not just send code over rednet?

I am not exactly sure what you are trying to do.
Bubba #4
Posted 30 March 2013 - 06:33 PM
Well neither of you have really hit on the mark with loading variables from files (and also, metatables have nothing to do with this). You can use textutils.serialize/textutils.unserialize to make storing variables in files easier, but there are literally infinite ways to do so.

What textutils.serialize does is convert a table into string form, meaning that its easy to save a large number of variables to a single file. textutils.unserialize reverses the operation. Here's an example:

--serializing
local programStatus = {
  running = true;
  programName = "/testProg";
  other_info = 5;
}

local f = fs.open("save_info", "w")
f.write(textutils.serialize(programStatus))
f.close()

And unserializing it:

local f = fs.open("save_info", "r")
local content = f.readAll()
f.close()
content = textutils.unserialize(content) --It's a table again
print(content["running"]) --Prints "true"

If you only need to save one variable to a file, you could just use normal comparison like so:

local f = fs.open("save_info", "w")
f.write("running:true")
f.close()

--And then on your program ending you could do this
local f = fs.open("save_info", "w")
f.write("running:false")
f.close()


local f = fs.open("save_info", "r")
local content=f.readAll()
f.close()
if content=="running:true" then
  print("The program is running!")
else
  print("The program is not running.")
end

Another thing you could do is just check that a file exists:

local f = fs.open(".currentProgram", "w")
f.close()

--And on the close
fs.delete(".currentProgram")


if fs.exists(".currentProgram") then
  print("It's running!")
end

If you actually want a boolean value and not a string representation of it, tostring and loadstring will serve your purposes.

local fileContent = "true" --Pretend I saved the boolean to a file and loaded it alread with f.readAll() or something
local bool_value = loadstring(fileContent)
print(type(bool_value)) --Outputs "boolean"