ID:268110
 
Lummox Jr. wrote:
In more ways than one! icon_states() is by no means a fast proc. You'd do better to cache its return value and keep reusing that.

Lummox JR

How exactly would I go about doing that?

Resonating_Light
Resonating_Light wrote:
Lummox Jr. wrote:
You'd do better to cache its return value and keep reusing that.

How exactly would I go about doing that?
var/list/icon_states_cache = list /* Associated list of icon_states
indexed by icon */


proc/GetStates(Icon)
var/states = icon_states_cache[Icon]
if(!states) // Icon is not in the cache
states = icon_states(Icon)
icon_states_cache[Icon] = states // store it for later
return states

turf/grass
icon = 'grass.dmi'
New()
..()
icon_state = pick(GetStates(icon))
In response to Shadowdarke
Hmm, lets see now. We can use:

> var/list/icon_states_cache = list /* Associated list of icon_states
> indexed by icon */

>
> proc/GetStates(Icon)
> var/states = icon_states_cache[Icon]
> if(!states) // Icon is not in the cache
> states = icon_states(Icon)
> icon_states_cache[Icon] = states // store it for later
> return states
>
> turf/grass
> icon = 'grass.dmi'
> New()
> ..()
> icon_state = pick(GetStates(icon))
>


Or:

<code> turf/grass New() icon_state = "[rand[1,4]]"</code>

Hmm...
In response to Foomer
I was answering the question about how to cache the icon_states list. Personally, I'd go with the random number scheme in this case as well. That doesn't mean that caching is not a useful thing to know for other cases.
In response to Foomer
Foomer wrote:
 turf/grass
New()
icon_state = "grass[rand[1,4]]"


No need for seperate .dmi files now :P
In response to Shadowdarke
Thanks, this really could come in useful! Though I'm not sure where I'll end up using it.

Resonating Light