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

This should be working, works when starts up.

Started by Gumball, 06 December 2014 - 01:27 AM
Gumball #1
Posted 06 December 2014 - 02:27 AM
Im doing an AppleOS rewrite, and it loads files so you can save preferences, like passwords and background colors, and the background color loader doesn't work, but it does at first.

It loads the background color when it first starts up, but when you change it using my GUI, then it just crashed, but when you reboot then the color is changed to what you want. It errors exactly:

window:57: Expected number
Press any key to continue

Its really weird, works at first but errors when changed.

Heres the code for the background color loader.

And this time, im not using the code block because its small and not that easy to read so:

CODE:

function prefLoader()
prefDefiner = fs.open("prefs","r")

bColor = prefDefiner:readLine(1)

if(bColor == "lime") then
bColor = colors.lime
elseif(bColor == "blue") then
bColor = colors.blue
elseif(bColor == "red") then
bColor = colors.red
end
prefDefiner:close()
end

Now heres the code for the color changer

if(X == 13 and Y ==5) then
prefs = fs.open("prefs","w")
file.write("lime")
prefLoader()

elseif(X == 14 and Y==5) then
prefs = fs.open("prefs","w")
prefs.write("red")
prefLoader()
end

And yes I tried adding the prefs.close() or the prefs:close(), cant tell the diff so I added both but still an error.

And the only time it errors is when I call term.clear() when I do term.setBackgroundColor(bColor) before it.

Could someone help pls?

Thanks!

-bluebird
Edited on 06 December 2014 - 01:29 AM
Bomb Bloke #2
Posted 06 December 2014 - 03:08 AM
When you use a colon to call an object's function, it passes a copy of the object to the function as the first argument.

For example, prefs:close() is the same as prefs.close(prefs), and prefDefiner:readLine(1) is the same as prefDefiner.readLine(prefDefiner,1).

Since in both of the above two cases the functions in concern happen to ignore all arguments passed to them, there's effectively no difference.

The window API is used by advanced computers pretty much every time you call a "term" function. Odds are it's complaining because bColor is nil when you run term.setBackgroundColor(bColor), and odds are bColor is nil because your prefLoader() function attempts to open your "prefs" file for read access while you've already got it open for write access.

I'd re-write the prefLoader() function along these lines:

local function prefLoader()
	local prefDefiner = fs.open("prefs","r")
	bColor = colors[prefDefiner.readLine()]
	prefDefiner.close()
	
	if not bColor then
		error("Unable to read a valid colour from the prefs file.")
	end
end

… and the colour changer along these lines:

if X == 13 and Y == 5 then
    bColor = "lime"
elseif X == 14 and Y == 5 then
    bColor = "red"
end

if type(bColor) == "string" then
    local prefs = fs.open("prefs","w")
    prefs.write(bColor)
    prefs.close()

    bColor = colors[bColor]
end
Edited on 06 December 2014 - 02:12 AM
Gumball #3
Posted 06 December 2014 - 05:54 AM
Alright, thanks!