ID:159774
 
Hey, so in my game i have a global proc, i havnt really used byond in awhile so my coding is getting rusty, anyways the proc is for a farming portion. it changes the ground that is watered to unwatered. the part of the proc is..


/*  for(var/turf/dirt/check in world)
if(check.icon_state=="DirtWatered")
check.icon_state="DugDirt"*/


The problem is, is this is causing big lag spikes :/.

Even if none of the dirt is watered. Im assuming its gonna check all turf either way, so... my question is...

Can i do this another way? without the spike :).






narrowed it down to this part by commenting sections, without this in i dont get my lag :P.
sleep() procedure.

It'll make it last longer, but it won't do all at once, without giving the computer a time to breath.
When that code runs, you loop through and check every /turf/dirt in the game. If you have a lot of /turf/dirts, it can take awhile to run.

First, look to see if there's a different way of approaching the problem that involves less things to loop through. One idea that might work is to have a global list that contains all the turfs that have been watered. Whenever a verb (or other action) causes a turf to get wet, add it to that list at the same time. Then you'll have a much smaller number of items to loop through in the first place.

Next, try not to run intensive loops any more often than necessary. I'm not saying that this is the case here, but as an example, if a loop is being run once every ten seconds, could you get by with once every minute or more?

Last, if you have a long-lasting loop that absolutely has to get run, you can throw in a call to sleep(0) or possibly sleep(-1). When execution reaches the call to sleep(), it will pause to check if any other actions need doing, like moving players around, and help deal with the lag. The loop will take a little longer to complete though, so all the turfs won't be checked in the exact same instant.
In response to Jon88
Thanks, your a life saver.

Problem was, i have a map of 500x500 with 20 z layers.. for dungeons.

Lol.


anyways it lag for a moment cus im guessing it had to scan threw every turf just to see if it was dirt or not.

So i made a list, everytime i watered a turf. it sent it to the list, and the proc just eliminates the turfs within the list. no searching. no lag. thanks.