This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Edit varrible in another file.
Started by 3ydney, 07 March 2013 - 06:17 PMPosted 07 March 2013 - 07:17 PM
How can I edit a varrible that is in another file by using os.loadAPI()?
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
The rest is simple, edit the contents then re-write the new data into a new table and insert that table into the file.
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.
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:
Another way to do it is how Suicidal suggested by reading the file, changing your variable and then writing the file again. the code:
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.
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.
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:
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()
Posted 07 March 2013 - 08:15 PM
Which guy?This guy is all tables, strings, arrays, and variables baby!
Posted 07 March 2013 - 08:16 PM
Muah! C'mon, you know this ;)/> (I like third person pov)Which guy?This guy is all tables, strings, arrays, and variables baby!
EDIT: I actually like Third Person Omniscient (I know about all your feelers!)
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.
Posted 07 March 2013 - 09:26 PM
Now why would I make silly tentacle jokes at 3:30 in the morning?
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!
Posted 07 March 2013 - 10:03 PM
Did I hurt your feelers too?Now why would I make silly tentacle jokes at 3:30 in the morning?
GO TO BED NOW!
HA, it's 4:07 AM, i'm still going hard, and i'm using Comic Sans with size 36!
Posted 07 March 2013 - 10:34 PM
HA, I CAN TYPE IN 48px ( I think its pixels )
Posted 08 March 2013 - 12:53 AM
Hmm, it would'nt let me for some raisin (Still looking for a Old World Blues fan ;)/>)HA, I CAN TYPE IN 48px ( I think its pixels )