ID:158391
 
Is it possible to do something like '/icons/[fName].dmi'?

I've tried var/icon/I= new/icon(file("/icons/[fName].dmi"))

Which should work? It says in the help that icon()'s first arg is a file.

But at run-time I get runtime error: bad icon operation
The problem is that anything in single quotes (' ') is placed in your game's resource file (.rsc) cache at compile time. The filename can't contain a variable (e.g. 'icon[number].dmi') since [number] will only be resolved at runtime.

The way around this is to use fcopy_rsc() to copy a file to the cache at runtime:

var/F = fcopy_rsc("icons/[fname].dmi")
var/icon/I = icon(F, "state1")


Note the double quotes (" ") indicating this is a external file, not a cache file. Also, you can implicitly call fcopy_rsc() by passing an external filename to icon():
var/icon/I = icon("icons/[fname].dmi", "state1")

which does exactly the same thing.
In response to Hobnob (#1)
This will still work if I give the host files to someone else, who doesn't have that file, right?
In response to Sprin (#2)
Sprin wrote:
This will still work if I give the host files to someone else, who doesn't have that file, right?

Unless at some point in your code you reference all the icon files you want to access in single quotes, they won't be stored in the .rsc file and thus won't be loadable with just the host files.

One way around this would be to have a list somewhere with the icon names:
var/list/extra_icons = list('icon1.dmi', 'icon2.dmi', 'icon3.dmi')

You don't have to do anything with the list; as long as it exists, it will ensure that those icons are included in the .rsc file.