ID:1978909
 
A modified version of mine, allowing the addition of multiple loops going on at once.


In world/New() use global.AddLoop(id,time) to add in a loop.

Use LoopAdd(id) on a atom/movable to add it to a loop.

Use LoopRemove(id) on a atom/movable to remove it to a loop.

Override updateLoop() as needed.

#define UPDATE_LOOP_SLEEP world.tick_lag

var/list/updateLoop[] = new

world
New()
..()
for()
sleep(UPDATE_LOOP_SLEEP)
for(var/loop in global.updateLoop)
var/UPDATE_LOOP/Loop = global.updateLoop[loop]
Loop.updateLoop()

proc/AddLoop(id,time)
global.updateLoop[id] = new/UPDATE_LOOP(time,id)

UPDATE_LOOP
var
list
updateLoop = list()
max_tick = 0
tick = 0
id = ""
proc/updateLoop()
if(!--tick)
tick = max_tick
for(var/object in src.updateLoop)
object:updateLoop(id)
New(time,ID){id = ID; max_tick = time; tick = max_tick}

atom/movable/proc
updateLoop(id)
LoopAdd(id)
var/UPDATE_LOOP/Loop = global.updateLoop[id]
if(Loop)Loop.updateLoop |= src
LoopRemove(id)
var/UPDATE_LOOP/Loop = global.updateLoop[id]
if(Loop)Loop.updateLoop -= src
Example on how to use it.

mob
Login()
src.LoopAdd("Movement")
updateLoop(id)
switch(id)
if("Movement")
//Movement code here.

World
New()
global.AddLoop("Movement",10)
..()
Fun fact. If you have something that has LoopAdd() in its New(), and it's placed on the map, you'll get runtime errors.

REDUNANCY, F*** YEAH.
In response to Rushnut
Rushnut wrote:
Fun fact. If you have something that has LoopAdd() in its New(), and it's placed on the map, you'll get runtime errors.

REDUNANCY, F*** YEAH.

Why would someone do that tho? That isn't how this is used.
Well the primary use of this is to have movement tied to AI. In a smaller game the mobs might already be on the map, which will cause the above issue.
In response to Rushnut
Rushnut wrote:
Well the primary use of this is to have movement tied to AI. In a smaller game the mobs might already be on the map, which will cause the above issue.

spawn
Huh?
In response to Rushnut
Rushnut wrote:
Well the primary use of this is to have movement tied to AI. In a smaller game the mobs might already be on the map, which will cause the above issue.

This can be used for many more things, maybe you want to keep track of their hunger, or anything that could be passive.
This is well written. I've noticed that alot of what drives CPU in DM can be related to sleeping though.
I tend to use recursive loops, that works alot better in my observance.

var/looping=0
proc
Loop()
if(!looping)
looping=1//prevents the loop from running multiple times
spawn(wait)looping=0;.()

As an example. What I've found is that over time especially and with things like AI- it works much, much better.

With this you also don't have to worry about how many loops you have running at any one time.