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

String expected, got nil

Started by Brod8362, 08 March 2016 - 07:17 PM
Brod8362 #1
Posted 08 March 2016 - 08:17 PM
–SOLVED–

I've been trying to create some customizability features for my operating system. However, reading the data from the file isn't working properly. I have no idea why.
The data in the file 'tcolor' is just colors.gray .
The error is window:179:string expected, got nil which means I'm putting in something that isn't a color. Even though it is…

Spoiler

function getInfo()
tb = fs.open("/ppc/oss/tcolor","r")
sb = fs.open("/ppc/oss/scolor","r")
TData = {}
local tline = tb.readLine()
table.insert(TData,line)
line = tb.readLine()
tb.close()
tbcolor = TData[1]
SData = {}
local sline = sb.readLine()
table.insert(SData,line)
line = sb.readLine()
sb.close()
sbcolor = SData[1]
end




function draw()
img = paintutils.loadImage("/ppc/systemi")
paintutils.drawImage(img,1,1)
paintutils.drawLine(7,1,26,1,tbcolor)
term.setCursorPos(1,1)
tbcolor = TData[1]
term.setBackgroundColor(sbcolor)
print("@Start")
term.setBackgroundColor(tbcolor)
term.setCursorPos(8,1)
print("Title Bar")
end

function interact()
local event, button, x, y = os.pullEvent("mouse_click")
if x <= 6 and y == 1 then
shell.run("/ppc/start")
else
interact()
end
end

getInfo()
draw()
interact()
Edited on 08 March 2016 - 08:36 PM
KingofGamesYami #2
Posted 08 March 2016 - 08:21 PM
Anything read from a file is a string. Strings aren't colors.

"colors.grey" is not colors.grey
Edited on 08 March 2016 - 07:22 PM
Creator #3
Posted 08 March 2016 - 08:24 PM
Numbers are colors. If the file contains a number, just `tonumber(sbcolor)` it.

If it is a color name, just use the table: `colors[sbcolor]`.
Brod8362 #4
Posted 08 March 2016 - 08:37 PM
Anything read from a file is a string. Strings aren't colors.

"colors.grey" is not colors.grey

It is supposed to be a string.

Let me elaborate. I was using colors in the form of colors.xxx before and it was working fine. (No quotes)
Wouldn't just reading from a file with that string in it work fine too?

Unless I'm a complete idiot…
Edited on 08 March 2016 - 07:45 PM
valithor #5
Posted 08 March 2016 - 08:57 PM
Anything read from a file is a string. Strings aren't colors.

"colors.grey" is not colors.grey

It is supposed to be a string.

Let me elaborate. I was using colors in the form of colors.xxx before and it was working fine. (No quotes)
Wouldn't just reading from a file with that string in it work fine too?

Unless I'm a complete idiot…

Whenever you write something like

colors.grey

What that is saying is look under the key "grey" in the table colors, so whenever there are quotes around it, it is treating it as a string instead of a table reference. Since it is being read from a file it is a string, and must be converted via loadstring (lua 5.1) or load (lua 5.2) back into its table reference. Since tonumber basically just calls loadstring, it can be used to convert the table reference back into the number it originally represented.
Brod8362 #6
Posted 08 March 2016 - 09:05 PM
Anything read from a file is a string. Strings aren't colors.

"colors.grey" is not colors.grey

It is supposed to be a string.

Let me elaborate. I was using colors in the form of colors.xxx before and it was working fine. (No quotes)
Wouldn't just reading from a file with that string in it work fine too?

Unless I'm a complete idiot…

Whenever you write something like

colors.grey

What that is saying is look under the key "grey" in the table colors, so whenever there are quotes around it, it is treating it as a string instead of a table reference. Since it is being read from a file it is a string, and must be converted via loadstring (lua 5.1) or load (lua 5.2) back into its table reference. Since tonumber basically just calls loadstring, it can be used to convert the table reference back into the number it originally represented.

Oh ok. So I am a complete idiot. As evident in this:
How would I use loadstring to turn my string into a usable value?
KingofGamesYami #7
Posted 08 March 2016 - 09:16 PM
It's usually easier to place the table index in the file. For example:


grey

and using it:


local file = fs.open( "filename", "r" )
local nColor = colors[ file.readAll() ] or colours[ file.readAll() ] or colors.white --#if it's a valid color or colour, use that.  Otherwise, use white
file.close()
Brod8362 #8
Posted 08 March 2016 - 09:36 PM
It's usually easier to place the table index in the file. For example:


grey

and using it:


local file = fs.open( "filename", "r" )
local nColor = colors[ file.readAll() ] or colours[ file.readAll() ] or colors.white --#if it's a valid color or colour, use that.  Otherwise, use white
file.close()

Thank you alot. This did exactly what I was expecting!