ID:727076
 
(See the best response by .screw.)
Code:
regeneration2()
set background = 1
if(!initialized)
sleep(100)
spawn() regeneration2()
return
if(immortality)
immortality-=0.1
if(immortality<0)
immortality=0
if(!client)
sleep(50)
if(!EN[13])
sleep(300)
spawn()regeneration2()
return
if(!pk || !client)
sleep(10)
stunned-=1
kstun-=1
move_stun -= 10
cc-=10
attackbreak-=40
//Jon Added
paper_bomb_explode -= 1
else
if(kstun)
kstun-=0.10
if(stunned)
stunned-=0.10
//Jon Added
if(paper_bomb_explode)
paper_bomb_explode -= 0.1
if(cc)
cc--
if(attackbreak)
attackbreak-=2
if(move_stun)
--src.move_stun
if(stunned<0)
src.stunned=0
if(kstun<0)
src.kstun=0
if(move_stun<0)
src.move_stun=0
if(cc<0)
cc=0
if(attackbreak<0)
attackbreak=0
//Jon Added
if(paper_bomb_explode < 0)
paper_bomb_explode = 0

sleep(1)
spawn()regeneration2()


THat proc for some reason is taking hella cpu usage when i check it with .profile using dreamdaemon

regeneration2() is an infinite with barely any delay. Therefore, your game will eventually crash due to running out of system stack space.

I cannot imagine regeneration2() needing to be infinitely looped. Try seeking an alternative method. Perhaps, only regenerate a player when damage has occurred. But with any route you take, this procedure needs to be vastly changed.
Yea, I just dont know how to do that
If you're looking at the "real time" value in the profiler (assuming .profile in DD works the same as the profiler in DS) I'm pretty sure that counts time spent on sleep() calls. If that's what you're seeing, it's not really that the CPU usage is high. The proc doesn't do much so it shouldn't use much CPU time even if you're calling it every 0.1 seconds. You can make this change:

// change this:
sleep(100)
spawn() regeneration2()

// to this:
spawn(100) regeneration2()

Which should get rid of the sleep time that's counting towards some of the CPU time measurements.
Allright I'll try that That, Great Appreciation!
In response to .screw
Also, if you look at the total CPU time or self CPU time, those shouldn't be counting time spent sleeping. The self CPU time of this proc should be small, provided there aren't a million mobs running this proc.

.screw wrote:
regeneration2() is an infinite with barely any delay. Therefore, your game will eventually crash due to running out of system stack space.

He's always using spawn() when calling regeneration2() again, so it won't make the stack grow. Each time the proc executes it reaches the end and is popped off the stack.

It is being called every 0.1 seconds but it's only doing simple math. That, by itself, shouldn't be causing "hella" CPU usage.

Try seeking an alternative method. Perhaps, only regenerate a player when damage has occurred. But with any route you take, this procedure needs to be vastly changed.

I do agree with that. Not only does it look like there's a better way to trigger regeneration, this proc seems to handle a lot more than just regeneration. There's a lot of cryptic code in there (a month from now will you remember what cc-=10 or if(!EN[13]) do or why they're there?). There's nothing wrong with that exactly, but if you put them each in a proc that has a descriptive name, it'll help you remember.
In response to GodBreaker
Best response
GodBreaker wrote:
Yea, I just dont know how to do that

Only call this procedure when needed. I presume you have multiple players calling your previous procedure, thus bogging down your system. You also need to make sure regeneration2() breaks when needed. Example: If when immortality reaches 0 and regeneration is no longer needed, do: while(immortality)
mob
var/regenerating // if you're regenerating
proc
regeneration2(var/delay) // if regenerating takes a long time, make sure the delay is something feasible
if(regenerating) return
regenerating = 1
spawn() // allows the game to continue to run
while(variable_here)
/*
set this to a variable that'll stop this procedure.
you do not need to continue looping if there is no
need to regenerate if it isn't needed

*/

immortality = max(0, immortality-0.1)

if(!pk || !client) // so not a client?
stunned = max(0, stunned-1)
kstun = max(0, kstun-1)
move_stun = max(0, move_stun-10)
cc = max(0, cc-10)
attackbreak = max(0, attackbreak-40)
// jon added?
paper_bomb_explode = max(0, paper_bomb_explode-1)
else
stunned = max(0, stunned-0.1)
kstun = max(0, kstun-0.1)
move_stun = max(0, move_stun--)
cc = max(0, cc--)
attackbreak = max(0, attackbreak-2)
// jon added?
paper_bomb_explode = max(0, paper_bomb_explode-0.1)

sleep(delay) // set the delay to whatever is needed

regenerating = 0
At first glance; are you repeatedly spawning more of the procedure off? You don't seen to set Initialized = 1 anywhere, so I imagine that;

regeneration2()
set background = 1
if(!initialized)
sleep(100)
spawn() regeneration2()
return

This happens. And it spawns another copy of regeneration2, which will also evaluate if(!initialized) to false and spawn again, and again...

Edit: A bunch of people replied in the time it took me to fetch a sandwich. They're all quite correct, though. A better way of doing this would be .screw's.
Oh allrighty thanks guys!