ID:174376
 
I have too many for loops but they are the only way I have found for searching through inventories and lists....is there an alternate way?
I have too many
for(var/obj/O in usr)
Depends what you want to do. If you want to find a specific item in an inventory, you can do things like <code>locate(/obj/item/cheese) in src</code>.

If you want to speed them up, you can make lists containing everything you want. For example, if you want to loop through all players in the world, normally you'd do something like:

<code>for (var/mob/player/P in world)</code>

But it's faster to loop through a smaller list than through the whole world, so you could make a "players" list and loop through that. For example, here's a "who" verb that uses this method:

<code>var/players[0] mob/player/New() ..() players += src mob/player/Del() players -= src ..() mob/verb/who() for (var/mob/player/P in players) src << "[P.name] ([P.key])"</code>
In response to Crispy
when the loop only involves players, use this in a global procedure loop
for(var/client/C in world)
it only looks for players
In response to FranquiBoy
FranquiBoy wrote:
when the loop only involves players, use this in a global procedure loop
for(var/client/C in world)
it only looks for players

Firstly, that won't work because clients aren't "in world". You need to use <code>for(var/client/C)</code> instead.

Secondly, that defeats the entire purpose of using lists to optimise loops anyway. It's better than <code>for(var/mob/M in world)</code>, but not by much.