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

window:48: expected number

Started by westloar, 27 July 2015 - 06:42 PM
westloar #1
Posted 27 July 2015 - 08:42 PM
Hi,

I am aiming to change text color based on a variable using the following code:



if cnx[key][1] = "open" then
  term.setTextColor(colors.green)
else
  term.setTextColor(colors.red)
end
write(cnx[key][1])
term.setTextColor(colors.white)


I have had a look through the forums and have found similar errors, however they have all been related to incorrect capitlisation i.e. lightblue instead of lightBlue, or defining the color by string, i.e. "colors.green".

I have tested my code with the color number instead of color code and this works, however another part of the code will rely on a table of colors defined with their color code rather than number, so I would rather figure out why this does not work than translate everything to color numbers.

Any help or suggestions would be appreciated.

Cheers.
KingofGamesYami #2
Posted 27 July 2015 - 08:46 PM
That cannot be the entire code, because that would reference a table that doesn't exist.

Aside from that, unless you modified the colors table in some way, that shouldn't throw any errors.
westloar #3
Posted 27 July 2015 - 09:00 PM
As I said, when using the color numbers, instead of the color codes, it prints each value of cnx[key] in the correct color. the whole code is:



cnx = {}          -- create the matrix

colours = {"red","white","orange","magenta","light blue",
"yellow,"lime","pink","gray","light gray","cyan",
"purple","blue","brown","green"}

bankCounter = 1

for i=1,60 do
cnx[i] = {}     
cnx[i][1]="open"
    cnx[i][2]= "bank" .. bankCounter
    cnx[i][3]= colors[(i % 15)+1]
    cnx[i][4]= ""

if i % 15 == 0
bankCounter = bankCounter + 1
end 
end

function printArray()
    for i=1,60 do
       SelectRow(i)
    end
end

function SelectRow(key)
  if cnx[key][1] == "open" then
    term.setTextColor(colors.green)
  else
    term.setTextColor(colors.red)
  end
write(cnx[key][1].." ")
term.setTextColor(colors.white)
end 
flaghacker #4
Posted 27 July 2015 - 09:32 PM
Your declaration of the colours table overwrites the colours api. Rename your variable.
westloar #5
Posted 27 July 2015 - 10:31 PM
seems so simple now you say it!
Dragon53535 #6
Posted 27 July 2015 - 10:40 PM
You don't HAVE to rename your table but it is good practice. There's the colors api and colours api. Same exact thing, different spelling. Both work using the same, however overwriting one will NOT overwrite the other, but changing the contents of one will change the contents of the other. A quick rewrite of your table, so that the spelling is correct of each color, will allow it to change easier.

cnx = {}		  --# create the matrix
colours = {"red","white","orange","magenta","lightBlue",
"yellow","lime","pink","gray","lightGray","cyan",
"purple","blue","brown","green"} --# Correct word for lightBlue and lightGray
bankCounter = 1
for i=1,60 do
cnx[i] = {}	
cnx[i][1]="open"
	cnx[i][2]= "bank" .. bankCounter
	cnx[i][3]= colors[colours[(i % 15)+1]] --#Using the colours table you have defined, find the correct color name,
	--#then plug that into the colors table that holds the color codes. So if i % 15 == 0 then colours[1] find "red" then colors["red"]
	cnx[i][4]= ""
if i % 15 == 0
bankCounter = bankCounter + 1
end
end
function printArray()
	for i=1,60 do
	   SelectRow(i)
	end
end
function SelectRow(key)
  if cnx[key][1] == "open" then
	term.setTextColor(colors.green)
  else
	term.setTextColor(colors.red)
  end
write(cnx[key][1].." ")
term.setTextColor(colors.white)
end
Edited on 27 July 2015 - 08:42 PM
westloar #7
Posted 27 July 2015 - 11:29 PM
Your declaration of the colours table overwrites the colours api. Rename your variable.

So i've renamed the colors variable to colTab and replaced all instances of it and it's still throwing the same error when I switch to using color codes, yet it still works with color numbers

Not sure if I'm still overwriting the api, but I can't see how.
westloar #8
Posted 27 July 2015 - 11:36 PM
You don't HAVE to rename your table but it is good practice. There's the colors api and colours api. Same exact thing, different spelling. Both work using the same, however overwriting one will NOT overwrite the other, but changing the contents of one will change the contents of the other. A quick rewrite of your table, so that the spelling is correct of each color, will allow it to change easier.

So I have changed the name of the table, thankyou for pointing out the mistakes in the table as well, that's prevented a headache later on!

However, with the changed table name (changed to colTab) I'm still getting the same error :S
flaghacker #9
Posted 28 July 2015 - 12:42 AM
Dit you reboot the computer? That's needed to reset the environment…
KingofGamesYami #10
Posted 28 July 2015 - 12:49 AM
Did you reboot the computer? You were declaring it globally, so it's possible the variable is still changed.

Edit Ninja'd. Darn this slow internet…
Edited on 27 July 2015 - 10:49 PM
westloar #11
Posted 28 July 2015 - 12:49 AM
Cheers, that's fixed it, I've learnt a lot from this post, so thank you to all that contributed :)/>
flaghacker #12
Posted 28 July 2015 - 01:57 AM
Did you reboot the computer? You were declaring it globally, so it's possible the variable is still changed.

Edit Ninja'd. Darn this slow internet…

7 minutes delay? That's some really bad internet… I feel sorry for you :)/>
KingofGamesYami #13
Posted 28 July 2015 - 03:13 AM
7 minutes delay? That's some really bad internet… I feel sorry for you :)/>/>

It's not actually my internet, to be honest. It's the way my school monitors our internet - by sending everything through their server. Occasionally their server gets flooded, and my internet will go down for 5 or 10 minutes at a time. Because of this, I wasn't informed someone else posted in the topic before I did… and I also couldn't post mine until the internet started working again.
MKlegoman357 #14
Posted 28 July 2015 - 05:45 AM
To avoid these kind of problems you should be using local variables. They work faster, they don't override other APIs, they don't expose sensitive information to other programs.