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

Edit varrible in another file.

Started by 3ydney, 07 March 2013 - 06:17 PM
3ydney #1
Posted 07 March 2013 - 07:17 PM
How can I edit a varrible that is in another file by using os.loadAPI()?
SuicidalSTDz #2
Posted 07 March 2013 - 07:25 PM
Not entirely sure you can… I would write the info from the file to a table, edit the data, then insert it back into the table and send it back into the file… If that made sense.

Here is how you can insert all the lines of a file into a table using the fs library
f = fs.open("test", "r")
local data = {}
for line in f.readLine do
table.insert(data, line)
end
f.close()
Depending on what line your variable is on, you will need to index that part of the table (Print the contents of the table, then index it accordingly for testing purposes)
The rest is simple, edit the contents then re-write the new data into a new table and insert that table into the file.
theoriginalbit #3
Posted 07 March 2013 - 07:42 PM
As SuicidalSTDz said there is no real way to do it. Now yes you could use os.loadAPI and change a variable that it declares, but that is not a persistent change, so if you come back later it will be the old value. Code example:

os.loadAPI('fileName')
print(fileName.xPos)
fileName.xPos = 5
print(fileName.xPos)
however this method relies on the file 'fileName' to look like this

-- some code here?
xPos = 8
-- some code here?


Another way to do it is how Suicidal suggested by reading the file, changing your variable and then writing the file again. the code:

local h = fs.open('fileName', 'r')
local data = {}
for line in h.readLine do
  table.insert(data, line)
end
h.close()
data[1] = 5 -- where 1 is the line its on
h = fs.open('fileName', 'w')
for i = 1, #data do
  h.write( data[i]..'\n' )
end
h.close()
The file 'fileName' would need to look like so

-- more stuff can be here
8 -- the line number of this MUST match the one in code
-- more stuff can be here


The last method you could do is use a configuration file utility that anyone has made on the forums. just use the search to find one.
SuicidalSTDz #4
Posted 07 March 2013 - 08:06 PM
Way off topic: Why did I ever choose the name "SuicidalSTDz"? Sounds odd, I guess that's why I like it :P/>
On topic: I never even use os.loadAPI. This guy is all tables, strings, arrays, and variables baby!

EDIT: If you are using a one-line variable file, you could just use the io library to open the file like so:

local h = io.open('fileName', 'r')
local data = h:read()
h:close()
h = io.open('fileName', 'w')
--Write new data to file
h:close()
theoriginalbit #5
Posted 07 March 2013 - 08:15 PM
This guy is all tables, strings, arrays, and variables baby!
Which guy?
SuicidalSTDz #6
Posted 07 March 2013 - 08:16 PM
This guy is all tables, strings, arrays, and variables baby!
Which guy?
Muah! C'mon, you know this ;)/> (I like third person pov)

EDIT: I actually like Third Person Omniscient (I know about all your feelers!)
ChunLing #7
Posted 07 March 2013 - 09:19 PM
Is that a tentacle joke? Because I can assure you…um, that I do not in point of fact have tentacles. Yes, let's go with that.
SuicidalSTDz #8
Posted 07 March 2013 - 09:26 PM
Now why would I make silly tentacle jokes at 3:30 in the morning?
3ydney #9
Posted 07 March 2013 - 09:52 PM
Now why would I make silly tentacle jokes at 3:30 in the morning?

GO TO BED NOW!
SuicidalSTDz #10
Posted 07 March 2013 - 10:03 PM
Now why would I make silly tentacle jokes at 3:30 in the morning?

GO TO BED NOW!
Did I hurt your feelers too?
HA, it's 4:07 AM, i'm still going hard, and i'm using Comic Sans with size 36!
3ydney #11
Posted 07 March 2013 - 10:34 PM
HA, I CAN TYPE IN 48px ( I think its pixels )
SuicidalSTDz #12
Posted 08 March 2013 - 12:53 AM
HA, I CAN TYPE IN 48px ( I think its pixels )
Hmm, it would'nt let me for some raisin (Still looking for a Old World Blues fan ;)/>)