world name = "Solus Text RPG System" New() ..() spawn(100) Save_Player_Ticker() world/proc Save_Players() for(var/mob/M in world) if(M.watcher == 0) var/savefile/F = new("saves/[M.key].sav") M.Write(F) //saves the rest M << "Your player file was saved." Save_Player_Ticker() Save_Players() spawn(autosave_time) Save_Player_Ticker()
Another example, *slightly* more complicated. The trashman proc, which helps keep the islands of Survival cleaner.
proc/Trashman() world.Repop() var/amount_of_trash = 0 for(var/obj/O in world) if(istype(O,/obj/weed) || istype(O,/obj/wild_lettuce) || istype(O,/obj/clover)) amount_of_trash += 1 for(var/obj/O in world) if(amount_of_trash >= 801) if(istype(O,/obj/weed) || istype(O,/obj/wild_lettuce) || istype(O,/obj/clover)) amount_of_trash -= 1 del(O) spawn(time_crop_growth) Trashman()
The next proc is not only probably shabby for efficiency, but it doesn't work either :P, so if you have any help for it, let me know.
/* proc/BasicAttack() var/enemynotfound = 0 var/enemyattacked = 0 for(var/mob/M in oview(5)) if(istype(M,/mob/pc)) step_to(src,M) else enemynotfound = 1 for(var/mob/M in oview(1)) if(istype(M,/mob/pc)) if(enemyattacked == 0) view() << "[src] attacks [M]" M.hp -= rand(1,2) if(M.hp <= 0) M.Die() if(prob(20)) view() << "[M] counter-attacks [src]!" src.hp -= rand(1,2) if(src.hp <= 0) src.Die() enemyattacked = 1 if(enemynotfound == 0) step_rand(src) world << "Basic:" spawn(20) BasicAttack()*/
Ok, I think I can stop now.
This isn't strictly true. Although it's not as efficient as directly saying something like this...
...it takes less space. Sometimes there's also really no way around a loop because you just don't know how many items you're working with.
Some of the (minor) inefficiency can be taken care of by a process known as unrolling a loop, where you basically take a section inside the loop and repeat it N times. This is really only simple to do in certain cases, and in most places you'd use loops in BYOND, it's a waste of time to even try.
You should avoid misusing loops, but a loop isn't something you can reasonably avoid in any situation that calls for one. And either the situation calls for a loop or it doesn't; there's not much of a gray area.
On the contrary, you get the best bang for your buck out of longer loops--particularly if you do optimizations or unroll a loop as I mentioned.
Lummox JR