What is spawn() best used for? What are it's drawbacks? Are there alternatives in some cases?
Most often I use it to run things in the 'background'. I've read byond is single threaded, so things can only run in order regardless.
spawn(-1) proc_name()
I use that a lot for my infinite loops, or loops that run for a long time. I also use it when I want to run a proc passively, but I don't want to wait on it to finish before proceeding.
proc
spar(mob/user, mob/opp)
if(!timer)
timer = 20
spawn(-1) spar_loop(user, opp)
timer = 20
spar_loop(mob/user, mob/opp)
var/gaining = 0
while(user.timer)
rest_pos()
if(gaining)
sleep(20)
continue
sleep(10)
if(!opp)
break
if(opp.timer)
if(check_cap())
break
sparring = 1
gaining = 1
user.Null_Base += ((Gains/100 * user.BPMod)/1000)
sleep(rand(25, 50))
gaining = 0
sparring = 0
Like that.
Use spawn(2) in Process 1
Process 2 activates
Process 2 finishes, 1 tick used
Process 3 activates
Process 3 finishes, 2 ticks now used
Process 1 comes back in now that 2 ticks are used
Process 1 finishes
Process 4 activates
Process 4 finishes