ID:152030
 
I want to do this for day and night and maybe for tides rising and falling. Previously I ran into problems adding day and night because turfs that had a turf on them, for example a floor turf with a chair on the floor would not get changed when I did a for(var/turf/t in world), only the chair would change.

I was wondering as far as memopry usage goes, if there are any pros or cons to starting the day night cycle in turf/New() as I did below.

turf
New()
..()
if(z==1)
Daynight()
proc
Daynight()
if(icon=='dayturf.dmi')
icon='nightturf.dmi'
if(icon=='nightturf.dmi')
icon='dayturf.dmi'
spawn(50000)
Daynight()
That's a pretty bad way of doing it. If you really want to do it based on the z level, then you could do it like this:
turf/New()
//No need to call ..() here, as it has no default action.

//Make sure you spawn() off DayNight(), or else New()
//will never return and you'll be stuck in whatever
//proc creates the turf (which is especially bad if it's
//world/New().
if(z == 1) spawn() DayNight()

turf/proc/DayNight()
//Never use recursion for infinite loops, ever. It's an
//awful way to do it. A while loop with a constantly
//true value is much, much better.

while(1)
sleep(50000)
icon = (icon == 'dayturf.dmi') ? 'nightturf.dmi' : 'dayturf.dmi'


Personally, I would make it more like this:
//This is the length of a day, in ticks.
var/day_length = 50000

turf/var
//This tells the program that this turf should cycle its
//icon_state depending on the time of day.
cycle_state = 1

//These store the icon states for day and night, respectively.
day_state = "day"
night_state = "night"

New()
if(cycle_state && (day_state || night_state))
//If the turf is supposed to cycle its icon state
//AND one of the two variables storing the icon
//states have a value (because one could be a
//default icon_state), then call the DayNight() proc.
spawn() DayNight()

proc/DayNight()
while(1)
sleep(day_length)
icon_state = (icon_state == day_state) ? night_state : day_state

With this method, you would simply use the instance editor to set on the cycle_state variable for turfs on the first z-level, which is much nicer than having an if to determine that.
In response to Popisfizzy
Here's a trick.
If your using turfs, or something with a single state.
Make DAY the NORTH state, NIGHT the SOUTH state.
Then simply change the direction of all of the turfs.
This makes it a lot faster because the icons are already cached.