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

Why Wont The Background Refresh?

Started by makerimages, 20 November 2013 - 07:20 AM
makerimages #1
Posted 20 November 2013 - 08:20 AM
Hey, got this code

Spoiler

os.loadAPI("OsOne/Apis/DesignUtil");
background=DesignUtil.getDesign()
term.setTextColor(colors.lightGray)
local cols = {1, 2, 4, 8, 16, 32, 64, 512, 1024, 2048, 4096, 8192, 16384, 32768}
local selector={
row1="v";
row2="^";
x=11;
y=9;
}
function getSelection(x)
if x>=11 and x<13 then
  DesignUtil.setDesign(0)
  background=DesignUtil.getDesign()
end
if x>=13 and x<15 then
  DesignUtil.setDesign(1)
--background=DesignUtil.getDesign()
  paintutils.drawImage(background,1,1)
end
end
function detectClick(xPos,yPos)
if yPos==9 then
  selector.x=xPos;
  getSelection(xPos)
end
end
function printSelector()
term.setBackgroundColor(colors.gray)
term.setCursorPos(selector.x,selector.y-1)
print(selector.row1)
term.setCursorPos(selector.x,selector.y+1)
print(selector.row2)

end
while true do
paintutils.drawImage(background,1,1)
term.setCursorPos(2,4)
print("Choose a color and name your computer, you will be able to personalize more later.")
term.setCursorPos(10,8)
term.setBackgroundColor(colors.gray)
print("							  ")
term.setCursorPos(10,9)
term.setBackgroundColor(colors.gray)
print("							  ")
term.setCursorPos(10,10)
term.setBackgroundColor(colors.gray)
print("							  ")
term.setBackgroundColor(DesignUtil.getColor())
term.setCursorPos(11,9)

for i,v in ipairs(cols) do
  term.setBackgroundColor(v)
  term.write("  ")
end
printSelector();
  term.setBackgroundColor(DesignUtil.getColor())
sleep(1)
  local event, button, X, Y = os.pullEvent("mouse_click")
  detectClick(X,Y)
end


and this is the DesignUtil Api
Spoiler

local fileColorConfig=nil
local backGround=nil

local fileData = {}


function getDesign()

fileColorConfig=fs.open("OsOne/Prefrences/Design.prefrence","r");
local line = fileColorConfig.readLine()
repeat
table.insert(fileData,line)
line = fileColorConfig.readLine()
until line == nil
fileColorConfig.close()


if fileData[1]=="0" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/whiteBack.nfp") end
if fileData[1]=="1" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/orangeBack.nfp") end
if fileData[1]=="2" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/darkPinkBack.nfp") end
if fileData[1]=="3" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/lightBlueBack.nfp") end
if fileData[1]=="4" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/goldenBack.nfp") end
if fileData[1]=="5" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/limeBack.nfp") end
if fileData[1]=="6" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/pinkBack.nfp") end
if fileData[1]=="9" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/cyanBack.nfp") end --because 8 is topbar color and 7 is button BG
if fileData[1]=="a" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/purpleBack.nfp") end
if fileData[1]=="b" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/blueBack.nfp") end
if fileData[1]=="c" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/brownBack.nfp") end
if fileData[1]=="d" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/greenBack.nfp") end
if fileData[1]=="e" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/redBack.nfp") end


fileColorConfig.close()
return backGround;
end
function setDesign(design)
fileColorConfig=fs.open("OsOne/Prefrences/Design.prefrence","w");
fileColorConfig.write(design.."")
fileColorConfig.close()
end
function getColor()
fileColorConfig=fs.open("OsOne/Prefrences/Design.prefrence","r");
local fileDataI={}
local line = fileColorConfig.readLine()
repeat
table.insert(fileDataI,line)
line = fileColorConfig.readLine()
until line == nil
fileColorConfig.close()
if fileDataI[1]=="0" then return colors.white end
if fileDataI[1]=="1" then return colors.orange end
if fileDataI[1]=="2" then return colors.magenta end
if fileDataI[1]=="3" then return colors.lightBlue end
if fileDataI[1]=="4" then return colors.yellow end
if fileDataI[1]=="5" then return colors.lime end
if fileDataI[1]=="6" then return colors.pink end
if fileDataI[1]=="9" then return colors.cyan end --because 8 is topbar color and 7 is button BG
if fileDataI[1]=="a" then return colors.purple end
if fileDataI[1]=="b" then return colors.blue end
if fileDataI[1]=="c" then return colors.brown end
if fileDataI[1]=="d" then return colors.green end
if fileDataI[1]=="e" then return colors.red end
end

When I click in the program, the click is identified, and getSelection is called, but the bg wont change. what could be thae cause and the fix?
Lyqyd #2
Posted 21 November 2013 - 10:52 AM
You commented out the other getDesign call, so it looks like it will only ever use one background image.
makerimages #3
Posted 21 November 2013 - 11:30 AM
Even with it uncommented, the issue remains there.
Bomb Bloke #4
Posted 21 November 2013 - 05:19 PM
In the DesignUtil API, the "fileData" table is not local to the getDesign() function in which it is referenced, nor does that function clear it. This means that its contents never reset: fileData[1] will always be what was in the "Design.prefrence" file when the script launched, while fileData[#fileData] will hold the value you're actually interested in.
makerimages #5
Posted 22 November 2013 - 01:44 PM
Well, did this:

local fileColorConfig=nil
local backGround=nil

local fileData = {}


function getDesign()

fileColorConfig=fs.open("OsOne/Prefrences/Design.prefrence","r");
local line = fileColorConfig.readLine()
repeat
table.insert(fileData,line)
line = fileColorConfig.readLine()
until line == nil
fileColorConfig.close()


if fileData[#fileData]=="0" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/whiteBack.nfp") end
if fileData[#fileData]=="1" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/orangeBack.nfp") end
if fileData[#fileData]=="2" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/darkPinkBack.nfp") end
if fileData[#fileData]=="3" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/lightBlueBack.nfp") end
if fileData[#fileData]=="4" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/goldenBack.nfp") end
if fileData[#fileData]=="5" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/limeBack.nfp") end
if fileData[#fileData]=="6" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/pinkBack.nfp") end
if fileData[#fileData]=="9" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/cyanBack.nfp") end --because 8 is topbar color and 7 is button BG
if fileData[#fileData]=="a" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/purpleBack.nfp") end
if fileData[#fileData]=="b" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/blueBack.nfp") end
if fileData[#fileData]=="c" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/brownBack.nfp") end
if fileData[#fileData]=="d" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/greenBack.nfp") end
if fileData[#fileData]=="e" then backGround=paintutils.loadImage("SystemResources/GraphicsResources/backgrounds/redBack.nfp") end


fileColorConfig.close()
return backGround;
end
function setDesign(design)
fileColorConfig=fs.open("OsOne/Prefrences/Design.prefrence","w");
fileColorConfig.write(design.."")
fileColorConfig.close()
end
function getColor()
fileColorConfig=fs.open("OsOne/Prefrences/Design.prefrence","r");
local fileDataI={}
local line = fileColorConfig.readLine()
repeat
table.insert(fileDataI,line)
line = fileColorConfig.readLine()
until line == nil
fileColorConfig.close()
if fileDataI[#fileData]=="0" then return colors.white end
if fileDataI[#fileData]=="1" then return colors.orange end
if fileDataI[#fileData]=="2" then return colors.magenta end
if fileDataI[#fileData]=="3" then return colors.lightBlue end
if fileDataI[#fileData]=="4" then return colors.yellow end
if fileDataI[#fileData]=="5" then return colors.lime end
if fileDataI[#fileData]=="6" then return colors.pink end
if fileDataI[#fileData]=="9" then return colors.cyan end --because 8 is topbar color and 7 is button BG
if fileDataI[#fileData]=="a" then return colors.purple end
if fileDataI[#fileData]=="b" then return colors.blue end
if fileDataI[#fileData]=="c" then return colors.brown end
if fileDataI[#fileData]=="d" then return colors.green end
if fileDataI[#fileData]=="e" then return colors.red end
end

changes color, then crashes at what I belive to be line 56 of the app in the op (term.setBackgroundColor(DesignUtils.getColor()) what is the fix? (error says expected nr
Bomb Bloke #6
Posted 22 November 2013 - 04:22 PM
That command is on line 57. You call the exact same thing a few lines up without issue, so I doubt that's the bit that's crashing. Please provide the exact error.
makerimages #7
Posted 23 November 2013 - 09:45 AM
excact error unvisible fully, seems to reboot to startup, that calss selectID which also crashes, will look further soon.
Bomb Bloke #8
Posted 23 November 2013 - 06:27 PM
Ah, I see - you're checking the content of "fileDataI[#fileData]" in your getColor() function.

Loosely put, #fileData gets you the number of entries in the fileData table. Because the fileData and FileDataI tables won't often be the same (because you keep filling the former forever, but reset the latter to empty every time you call the function that uses it), "fileDataI[#fileData]" will usually be nil. Meaning that function doesn't return anything and your attempt to change the background colour errors out.

I don't see why you bother with the tables at all, and I suspect you don't fully understand what your file-reading code does. Is there any need to try to maintain the history of colour changes?