var
mainUpdateLoop
mainUpdateLoop = new()
list
newWorldLoops = list("movement" = 0)
proc
addUpdater(var/name,var/updater)
if(name in mainUpdateLoop.updateLoops)
var/updateLoop/UL = mainUpdateLoop.updateLoops[name]
UL.updateLoop |= updater
#ifdef DEBUG
else
CRASH("Update Loop Crash. Tried to add \"[updater:type]\" to a loop that doesn't exist: \"[name]\".")
#endif
removeUpdater(var/name,var/updater)
if(name in mainUpdateLoop.updateLoops)
var/updateLoop/UL = mainUpdateLoop.updateLoops[name]
UL.updateLoop -= updater
#ifdef DEBUG
else
CRASH("Update Loop Crash. Tried to remove \"[updater:type]\" to a loop that doesn't exist: \"[name]\".")
#endif
createLoop(var/name,var/speed)
if(!(name in mainUpdateLoop.updateLoops))
var/updateLoop/UL = new()
UL.name = name
UL.iterationSpeed = speed
mainUpdateLoop.updateLoops[name] = UL
pauseLoop(var/name)
if(name in mainUpdateLoop.updateLoops)
var/updateLoop/UL = mainUpdateLoop.updateLoops[name]
UL.pause =! UL.pause
#ifdef DEBUG
else
CRASH("Update Loop Crash. Tried to pause a loop that doesn't exist: \"[name]\".")
#endif
forceUpdate(var/name)
if(name in mainUpdateLoop.updateLoops)
var/updateLoop/UL = mainUpdateLoop.updateLoops[name]
UL.delay = 0
#ifdef DEBUG
else
CRASH("Update Loop Crash. Tried to force update a loop that doesn't exist: \"[name]\".")
#endif
mainUpdateLoop
var
list
updateLoops = new/list()
New()
mainUpdateLoop = src
for(var/v in newWorldLoops)
createLoop(v,newWorldLoops[v])
..()
update()
proc
update()
spawn for()
for(var/updater in updateLoops)
var/updateLoop/UL = updateLoops[updater]
UL.iterate()
sleep world.tick_lag
updateLoop
var
list
updateLoop = new/list()
iterationSpeed = 0
pause = 0
delay = 0
name = ""
proc
iterate()
if(!pause)
if(!delay)
delay = iterationSpeed
update()
else
delay --
update()
for(var/updater in updateLoop)
if(hascall(updater,"update"))
updater:update()
#ifdef DEBUG
else
CRASH("Update Loop Crash. Tried to call iterate on \"[updater:type]\", in \"[name]\".")
#endif DEBUG
Quick rundown of what this does:
At the start of your world, a mainUpdateLoop is created, which continuously loops through all of the updateLoops you create, and iterates them. Iterating them means that it checks everything within the updateLoop, and runs the update() function for it.
The advantages to using a large single updateloop like this, is that it runs a lot lot quicker than having lots of individual loops. So for example, if you have your mob's AI in a loop, which randomly moves them around, and you have 1000 mobs, it will be significantly slower running than if you have one loop, which checks 1000 mobs and lets them run their movement.
To make it work, call createLoop(var/name,var/speed) to create a loop and it'll automatically get added to the mainUpdateLoop. Then call addUpdater(var/name,var/updater) to add anything to the loop, but make sure that whatever you add has the update() proc defined for it. You can force update a loop using forceUpdate(var/name), and remove things from loops using removeUpdater(var/name,var/updater). Finally you can pause and unpause loops using pauseLoop(var/name). newWorldLoops is a list in which you can define lists spawned on the creation of a new world, this is handy because it stops runtime errors if you have atoms initialized in the map editor.
Have fun, and play around as much as you can!
I apologise if I'm bad at explaining, but I talk to Kaiochao a lot, and he's bad at explaining. So blame him.