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

Can't set colors in specific program?!?! help!

Started by Lewisk3, 17 February 2016 - 02:17 AM
Lewisk3 #1
Posted 17 February 2016 - 03:17 AM
So i run this code,

term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
and i works fine, as expected. but! when i run this SAME CODE in another program i get the fallowing error

window:140: bios.lua:80: bad argument: string expected, got nil.

So, here is the program that it errors on,
Spoiler

--==> WinPOP v1.0, 2/16/16 <==-
-- ](c)Lewisk3-Corpz - 2016 [--

function init()
--==> Hiding POPWindows table
popwindows = {}
selectedwindow = ''
end
function getWindowByID(id)
for _, v in pairs(popwindows) do
  if(v.id == id)then
   return v
  end
end
end
function setContentPos(winid,xx,yy)
  local startX = getWindowByID(winid).x
  local startY = getWindowByID(winid).y
  term.setCursorPos(startX + xx, startY + yy)
end
getContentPos = term.getCursorPos
function getWindowPos(winid)
return getWindowByID(winid).x, getWindowByID(winid).y
end
function newWindow(idenity,size,colors,requestfocus)
local win = {
  x = size[1],
  y = size[2],
  px = 0,
  py = 0,
  mx = size[3],
  my = size[4],
  tc = colors[1],
  bc = colors[2],
  tbc = colors[3],
  ttc = colors[4],
  id = idenity,
  drawn = false,
  title="New Window",
  contents = {},
  dragable = true,
  draging = false,
  draw=function(self)
   if(self.x ~= self.px or self.y ~= self.py)then
	term.current().setVisible(false)
					  -->>>>>>>>>> HERE IT ERRORS <<<<<<<<<--
	term.setBackgroundColor(colors.black)
	term.setTextColor(colors.white)
	term.clear()
					 ------------------------------------------------------------------
	term.setCursorPos(self.x,self.y)
	term.setBackgroundColor(self.tbc)
	term.write(string.rep(" ",self.mx))
	term.setCursorPos(self.x+1, self.y)
	term.setTextColor(self.ttc)
	term.write(self.title)
	term.setCursorPos((self.mx+self.x),self.y)
	--term.setBackgroundColor(colors.red)
	--term.setTextColor(colors.white)
	write("X")
	paintutils.drawFilledBox(self.x,self.y+1,(self.mx)+self.x,(self.my)+self.y,self.bc)
	self.px = self.x
	self.y = self.py
	term.current().setVisible(true)
   end
  end,
  update=function(self, ev)
   if(selectedwindow ~= self.id and #popwindows <= 1)then
	selectedwindow = self.id
   end
   if(selectedwindow == self.id)then
	for _,v in pairs(self.contents) do
	 v.content(ev)
	end
	-- draging and closing
	if(self.dragable)then
	 if(ev[1] == "mouse_drag" and not self.draging)then
	  if(ev[3] >= self.x and ev[3] <= self.x+self.mx)then
	   self.x = ev[3]
	   self.y = ev[4]
	   self.draging = true
	  end
	 elseif(self.draging and ev[1] == "mouse_up")then
	  self.draging = false
	 end
	 if(self.draging)then
	  self.x = ev[3]
	  self.y = ev[4]
	 end
	end
	if(ev[1] == "mouse_click")then
	 if(ev[3] == self.x+self.mx )then
	  self:Dispose()
	 end
	end
	self:draw()
   end
  end,
  Dispose=function(self)
   local loops = 0
   for _, v in pairs(popwindows) do
	if(v.id == self.id)then
	 term.current().setVisible(false)
	 term.setBackgroundColor(colors.black)
	 term.setTextColor(colors.white)
	 term.clear()
	 popwindows[loops] = nil
	 term.current().setVisible(true)
	end
	loops = loops + 1
   end
  end,
  setTitle=function(self,ntitle)
   self.title=ntitle
  end,
  clearContents = function(self)
   self.contents = {}
  end,
  addContent=function(self,CONTENT)
   self.contents[#self.contents+1] = {}
   self.contents[#self.contents+1].content = CONTENT
  end,
  setDragable=function(self,ndragable)
   dragable = ndragable
  end,
  render=function(self)
   for _, v in pairs(popwindows) do
	if(v.id == self.id)then
	 error("Window already rendered.")
	end
   end
   popwindows[#popwindows+1] = self
  end,
}
if(requestfocus)then selectedwindow = win.id end
return win
end

Any help is very much appreciated, thank you.
Edited on 17 February 2016 - 02:18 AM
KingofGamesYami #2
Posted 17 February 2016 - 03:34 AM
That code cannot generate that error. I would expect term.write to error like that, if given an invalid argument.
Bomb Bloke #3
Posted 17 February 2016 - 04:27 AM
The window API doesn't check that you're setting valid values when you call setTextColour/setBackgroundColour, so attempting to draw after choosing invalid values causes this sort of problem.

In this case, colors.black and colors.white are nil, as the current scope has the colors API overridden by this line:

function newWindow(idenity,size,colors,requestfocus)
Lewisk3 #4
Posted 17 February 2016 - 05:35 AM
The window API doesn't check that you're setting valid values when you call setTextColour/setBackgroundColour, so attempting to draw after choosing invalid values causes this sort of problem.

In this case, colors.black and colors.white are nil, as the current scope has the colors API overridden by this line:

function newWindow(idenity,size,colors,requestfocus)
OHHH man i feel soo stupid for not seeing that. thanks man you're a life saver :)/>
Lewisk3 #5
Posted 17 February 2016 - 06:12 AM
I have another problem….

_G.old = {}
_G.winpop = {}
_G.old.setBackgroundColor = _G.term.setBackgroundColor
_G.term.setBackgroundColor = function(color)
  _G.winpop.bcolor = color
  _G.old.setBackgroundColor(color)
end
_G.term.getBackgroundColor = function()
  return _G.winpop.bcolor
end
_G.old.setTextColor = _G.term.setTextColor
_G.term.setTextColor = function(color)
  _G.winpop.tcolor = color
  _G.old.setTextColor(color)
end
_G.term.getTextColor = function()
  return _G.winpop.tcolor
end
getting a java vm error on line 6.

thanks for the help, in advanced.
Edited on 17 February 2016 - 05:14 AM
Bomb Bloke #6
Posted 17 February 2016 - 01:01 PM
Quick, shot in the dark guess: you aren't executing this code more than once per CC-system reboot, are you? Because that'd lead to recursive function calling resulting in an index out of bounds error.
Lewisk3 #7
Posted 17 February 2016 - 11:45 PM
Quick, shot in the dark guess: you aren't executing this code more than once per CC-system reboot, are you? Because that'd lead to recursive function calling resulting in an index out of bounds error.

Just checked, only calling it once at the initialize function :/
Lyqyd #8
Posted 18 February 2016 - 12:01 AM
Let's see the rest of the code.
Lewisk3 #9
Posted 18 February 2016 - 06:48 AM
Let's see the rest of the code.

ok,

Spoiler
--==> WinPOP v1.0, 2/16/16 <==-
-- ]©Lewisk3-Corpz - 2016 [--

function init()
popwindows = {}
selectedwindow = ''
-- Setting up special GUI tracking functions
_G.old = {}
_G.winpop = {}
_G.old.setBackgroundColor = _G.term.setBackgroundColor
_G.term.setBackgroundColor = function(color)
  _G.winpop.bcolor = color
  _G.old.setBackgroundColor(color)
end
_G.term.getBackgroundColor = function()
  return _G.winpop.bcolor
end
_G.old.setTextColor = _G.term.setTextColor
_G.term.setTextColor = function(color)
  _G.winpop.tcolor = color
  _G.old.setTextColor(color)
end
_G.term.getTextColor = function()
  return _G.winpop.tcolor
end

end
function getWindowByID(id)
for _, v in pairs(popwindows) do
  if(v.id == id)then
   return v
  end
end
end
function setContentPos(winid,xx,yy)
  local startX = getWindowByID(winid).x
  local startY = getWindowByID(winid).y
  term.setCursorPos(startX + xx, startY + yy)
end
getContentPos = term.getCursorPos
function getWindowPos(winid)
return getWindowByID(winid).x, getWindowByID(winid).y
end

function redraw()
local loops = 0
for _, v in pairs(popwindows) do
  if(not v.closed)then
   v:draw(false)
  else
   popwindows[loops] = nil
  end
  loops = loops + 1
end
end
function newWindow(idenity,size,wincols,requestfocus)
local win = {
  x = size[1],
  y = size[2],
  px = 0,
  py = 0,
  mx = size[3],
  my = size[4],
  tc = wincols[1],
  bc = wincols[2],
  tbc = wincols[3],
  ttc = wincols[4],
  id = idenity,
  drawn = false,
  title="New Window",
  contents = {},
  dragable = true,
  draging = false,
  closed = false,
  clear=function(self,redr)
   local bkg = colors.black --]] term.getBackgroundColor()
   local bkt = colors.white --]] term.getTextColor()
   paintutils.drawFilledBox(self.x,self.y,
	(self.mx)+self.x,(self.my)+self.y+2,colors.black)
   if(redr)then redraw() end
  end,
  draw=function(self,always)
   if( (self.x ~= self.px or self.y ~= self.py) or always)then
	term.current().setVisible(false)
	term.setCursorPos(self.x,self.y)
	term.setBackgroundColor(self.tbc)
	term.write(string.rep(" ",self.mx))
	term.setCursorPos(self.x+1, self.y)
	term.setTextColor(self.ttc)
	term.write(self.title)
	term.setCursorPos((self.mx+self.x),self.y)
	term.setBackgroundColor(colors.red)
	term.setTextColor(colors.white)
	write("X")
	paintutils.drawFilledBox(self.x,self.y+1,(self.mx)+self.x,(self.my)+self.y,self.bc)
	self.px = self.x
	self.y = self.py
	--term.setBackgroundColor(bkg)
	--term.setTextColor(bkt)
	term.current().setVisible(true)
   end
  end,
  update=function(self, ev)
	for _,v in pairs(self.contents) do
	 v.content(ev)
	end
	-- draging and closing
	if(self.dragable)then
	 if(ev[1] == "mouse_drag" and not self.draging)then
	  if(ev[3] >= self.x and ev[3] <= self.x+self.mx and ev[4] >= self.y and ev[4] <= self.y+self.my )then
	   self.draging = true
	  end
	 end
	 if(self.draging and ev[1] == "mouse_up")then
	  self.draging = false
	 end
	 if(self.draging and ev[4] > 1 and ev[3] > 2 )then
	  self.x = ev[3]
	  self.y = ev[4]
	 end
	end
	if(ev[1] == "mouse_click")then
	 if(ev[3] == self.x+self.mx )then
	  self:Dispose()
	 else
	  selectedwindow = self.id
	  self:draw(true)
	 end
	end
  end,
  Dispose=function(self)
   local loops = 0
   for _, v in pairs(popwindows) do
	if(v.id == self.id)then
	 term.current().setVisible(false)
	 redraw()
	 self.closed = true
	 term.current().setVisible(true)
	end
	loops = loops + 1
   end
  end,
  setTitle=function(self,ntitle)
   self.title=ntitle
  end,
  clearContents = function(self)
   self.contents = {}
  end,
  addContent=function(self,CONTENT)
   self.contents[#self.contents+1] = {}
   self.contents[#self.contents+1].content = CONTENT
  end,
  setDragable=function(self,ndragable)
   dragable = ndragable
  end,
  render=function(self)
   for _, v in pairs(popwindows) do
	if(v.id == self.id)then
	 error("Window already rendered.")
	end
   end
   popwindows[#popwindows+1] = self
  end,

}
if(requestfocus)then selectedwindow = win.id end
return win
end
function update(ev)
local loops = 0
for _, v in pairs(popwindows) do
  if(not v.closed)then
   v:draw(false)
   v:update(ev)
  else
   popwindows[loops] = nil
  end
  loops = loops + 1
end
end
Edited on 18 February 2016 - 05:48 AM
HPWebcamAble #10
Posted 18 February 2016 - 08:14 AM
Let's see the rest of the code.
ok,

In CCEmuRedux at least, nothing happens when I run that
Lewisk3 #11
Posted 18 February 2016 - 08:45 AM
Let's see the rest of the code.
ok,

In CCEmuRedux at least, nothing happens when I run that

thats because its an api ( A very buggy API that im prob never going to publish, its just for learning experience)
heres the code that will run the API - (Make sure to have the api saved as "winpop")

os.loadAPI("winpop")
winpop.init()
local mywin = winpop.newWindow("MyWin1",{5,2,20,12},{colors.white,colors.lightGray,colors.gray,colors.blue},false)
local win2 = winpop.newWindow("MyWin2",{30,2,10,6},{colors.black,colors.white,colors.blue,colors.white},false)
mywin:render()
win2:render()
while true do
winpop.update({os.pullEvent()})
end