Page 1 of 1

fg() and bg()

Posted: Fri Oct 07, 2011 10:39 pm
by Phoenix
fg() and bg(), until now, did not check to see if the colour inside them was a valid color in the color_table. If you used an invalid colour, you'd get an error such as this:
<./mudlet-lua/lua/GUIUtils.lua:651: attempt to index field '?' (a nil value)>
Needless to say, this was rather uninformative... I couldn't figure out, at all, what damned colour I was using that wasn't defined. :idea: Behold: New and improved, it writes to the error log TELLING you which it doesn't use.
Edited as per the following posts. Was using an assert with concat, now uses an if statement
Code: [show] | [select all] lua
function fg(colorName)
   if color_table[colorName] then
      setFgColor(color_table[colorName][1], color_table[colorName][2], color_table[colorName][3])
   else 
      error("fg: "..colorName.." is not a valid color!")
   end
end

function bg(colorName)
   if color_table[colorName] then
      setBgColor(color_table[colorName][1], color_table[colorName][2], color_table[colorName][3])
   else
      error("bg: "..colorName.." is not a valid color!")
   end
end

Re: fg() and bg()

Posted: Sat Oct 08, 2011 12:10 am
by Vadi
This has been improved in the latest code: https://github.com/Beliaar/mudlet-lua/b ... s.lua#L645

The color name isn't listed though because it always has to be concatenated, even if the assertion didn't hit (so it's not really efficient).

Re: fg() and bg()

Posted: Tue Oct 11, 2011 4:27 am
by Phoenix
A non-specific way to figure it out was already there - by looking up the exact lines, the meaning of the error was clear. However, this doesn't tell you what the error to search for in your script is, especially if you use fg() and bg() a lot in conjunction with links, normal echos, and such.

I'm wondering, would if color_table[colorName] then <code> else error() end be more efficient, as the concat wouldn't need to happen unless the if failed? Something like this:
Code: [show] | [select all] lua
function fg(colorName)
   if color_table[colorName] then
      setFgColor(color_table[colorName][1], color_table[colorName][2], color_table[colorName][3])
   else
      error("fg: ".. colorName.." is not a valid color!")
   end
end
From my tests, seems that this ought to be the same speed as an assert with no concatenation, or possibly even a bit faster (hard to tell).

edit - While I can see that yes, this should prove faster, as no concatenation is happening unless the 'else' hits, in practice I am unable to see any difference between the two methods.

At 20 seconds of code-crunching both ways (stripping out the setFgColor bit, just the if vs assert), I'm unable to see any difference. Both get around 22,500 iterations of code on the loop, before my 20 seconds of crunch expires. I'm not seeing how a concatenation makes any practical difference in speed on my laptop, though I know theoretically it should make some minor bit.

Re: fg() and bg()

Posted: Wed Oct 12, 2011 8:37 pm
by Denarii
Add this and the same changes for bg().

Re: fg() and bg()

Posted: Tue Jan 08, 2013 4:37 am
by Vadi
Thanks for the feedback. Next update after 2.1 will have bg() and fg() report the wrong color names used.