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

Writing To A File

Started by Firnagzen, 27 September 2013 - 07:09 PM
Firnagzen #1
Posted 27 September 2013 - 09:09 PM
Right, so here's some code snippets from a larger program I'm writing:
function load_cfg()
	local file = fs.open("digger.cfg", "r")
	local r = tonumber(file.readLine())
	local o = tonumber(file.readLine())
	file.close()
end

function write_cfg()
	local file = fs.open("digger.cfg", "w")
	file.writeLine(tostring(r))
	file.writeLine(tostring(o))
	file.close()
end


--Main body
if (shell.getRunningProgram()~="startup") then
	local args = {...}
	local r = tonumber(args[1])
		local o = tonumber(args[2])
	if not (r and o) then
		print("No arguments given!")
		return
	end
	local current_x = 0
	local current_y = 0
	local current_z = 0
	local current_f = 0
	local resume = false
	print(r, o)
	write_cfg()
	fs.copy(fs.getName(shell.getRunningProgram()), "startup")
else
	load_cfg()
	local resume = true
end

As should be reasonably obvious, I'm saving this program as some other file name. On running with parameters (eg. test 5 6), it gets those parameters as variables r and o, and then writes them to the config file. Additionally it copies itself to a startup program.

The problem is that the writing doesn't seem to work. While the print(r, o) returns the correct parameters, the digger.cfg gets written with
nil
nil
I can't figure out what I've done wrong here, could anyone help?
Lyqyd #2
Posted 27 September 2013 - 09:17 PM
Pass the variables to your config writing function. You're declaring them as local after the declaration of your function. They have no way of getting that variable's value unless you pass it to them.
Firnagzen #3
Posted 27 September 2013 - 09:30 PM
… Ah, crap. Thanks.

I always thought local variables were local to a program. Oh well.

Could I simply not declare them as local, because I need the r and o values for quite a few functions?

EDIT: Oh, I get it. I have to declare them as local at the top of the program, right? So:

local r = 0
local o = 0
local resume = false


function load_cfg()
	local file = fs.open("digger.cfg", "r")
	local r = tonumber(file.readLine())
	local o = tonumber(file.readLine())
	file.close()
end

function write_cfg()
	local file = fs.open("digger.cfg", "w")
	file.writeLine(tostring(r))
	file.writeLine(tostring(o))
	file.close()
end


--Main body
if (shell.getRunningProgram()~="startup") then
	local args = {...}
	r = tonumber(args[1])
	o = tonumber(args[2])
	if not (r and o) then
		print("No arguments given!")
		return
	end
	print(r, o)
	write_cfg()
	fs.copy(fs.getName(shell.getRunningProgram()), "startup")
else
	load_cfg()
	resume = true
end
apemanzilla #4
Posted 27 September 2013 - 11:23 PM
… Ah, crap. Thanks.

I always thought local variables were local to a program. Oh well.

Could I simply not declare them as local, because I need the r and o values for quite a few functions?

EDIT: Oh, I get it. I have to declare them as local at the top of the program, right? So:

local r = 0
local o = 0
local resume = false


function load_cfg()
	local file = fs.open("digger.cfg", "r")
	local r = tonumber(file.readLine())
	local o = tonumber(file.readLine())
	file.close()
end

function write_cfg()
	local file = fs.open("digger.cfg", "w")
	file.writeLine(tostring(r))
	file.writeLine(tostring(o))
	file.close()
end


--Main body
if (shell.getRunningProgram()~="startup") then
	local args = {...}
	r = tonumber(args[1])
	o = tonumber(args[2])
	if not (r and o) then
		print("No arguments given!")
		return
	end
	print(r, o)
	write_cfg()
	fs.copy(fs.getName(shell.getRunningProgram()), "startup")
else
	load_cfg()
	resume = true
end

Local variables are local to their parent structures. So if it's defined in a function, they're local to said function. Same goes for loops, ifs, etc.
immibis #5
Posted 29 September 2013 - 08:18 PM
That means these variables:

function load_cfg()
        local file = fs.open("digger.cfg", "r")
        local r = tonumber(file.readLine())
        local o = tonumber(file.readLine())
        file.close()
end
are local to load_cfg.
apemanzilla #6
Posted 29 September 2013 - 09:44 PM
That means these variables:

function load_cfg()
        local file = fs.open("digger.cfg", "r")
        local r = tonumber(file.readLine())
        local o = tonumber(file.readLine())
        file.close()
end
are local to load_cfg.
And there's your problem!