ID:1348685
 
BYOND Version:499.1201
Operating System:Windows 7 Pro 64-bit
Web Browser:Chrome 28.0.1500.95
Applies to:DM Language
Status: Open

Issue hasn't been assigned a status value.
This is the code I use to programatically remove blank icon_states from icons at run-time

proc/remove_blanks(icon/a)
//removes any icon states that are not labeled
var/icon/ic = icon()
var/icon/m = icon(a, moving = 1)
var/icon/n = icon(a, moving = 0)
for(var/s in icon_states(m))
if(s != "")
ic.Insert(icon(m, s), s, moving = 1)
for(var/s in icon_states(n))
if(s != "")
ic.Insert(icon(n, s), s, moving = 0)
return ic


This does indeed do what it is supposed to do, remove all blank icon states and return the icon without the blanks. The only problem is, if the icon to return only contains one icon_state, icon.Width() returns 0 when it clearly is not!

Not sure why.
Are you certain the result does in fact contain any states? If it doesn't have any, there's no width.
In response to Lummox JR
Yep, I double checked to make sure there was at least one icon_state after remove_blanks() was called. I know it works because it does indeed output an icon that I am using, I added a fall-back in my code to just assume "32x32" for a .Scale() call right after (my icons are 16x16) when Width() returns 0.
You know, another option is not to start with a blank icon at all. Rather than starting with a blank icon and inserting, you could start will null and just make a single-state copy on the first match.

That doesn't eliminate the fact of a bug, but it does provide a tidy workaround.
Could you show me how to do that? I don't understand. I am creating icons from scratch at run-time. There is nothing to start with.
You're pruning icon states that are non-blank by using Insert(); the icon you're inserting is what you're starting with. So instead of starting blank and inserting the first match info that, start with null and make a copy of that one single state, and then insert subsequent matches into that.
The icon I'm starting with is a really large tile-sheet or sprite-sheet (a png file). I'm not really sure how I would apply your concept to that situation without using Insert.
You'll still be inserting, just not the first time. The first time you would insert, you just take what you would have inserted and that will become your new icon. It's just like if you're adding values to a list: the list doesn't need to be initialized until the first value is added.
I see, so do a null check, and use master = icon(cropped) if master is null instead of master.Insert(cropped) But how would you name the first icon state without insert?
Ah yes, I had forgotten naming would be a problem.