ID:157082
 
i have code for a recurring proc, And it's essentially working yet not at the same time(at some point in time it comes back with a bunch of errors during runtime)

I'm wondering if theres any way(short of neglecting the infinite loop check, and potentially creating a mass of errors in the future..)

        LASER_REGEN()
src.health += round(((src.maxhealth-src.health)/src.maxhealth)*50)//add a percentage of the health
if(src.health>=src.maxhealth-10)
src.health=src.maxhealth
src.regenerating=0
return
if((src.health/src.maxhealth)*100>10) //percentage of the health is above 10
src.PercentageSet()//run yadayada proc
sleep(25)//two and a half seconds later

src.LASER_REGEN()//call it again..? Not sure how else to loop it.

return
src.regenerating=0//if all else fails, for whatever reason, stop regeneration.
return


Any constructive critism is welcomed, because i would rather walk away with something that i can use for future procs than a bundle of code from someone >~< so dont just fix it, please explain it at least a little...

Secondary question; Is there any good tutorials for Embedded Expressions?
Instead of sleep(25), you could use spawn(25). Stuff executed in spawn() will allow the rest of the current proc to finish before it goes.
In response to Skyspark
Wouldn't letting the rest of the proc finish just let it continue without delay?
Don't call the proc recursively (a proc calling itself). Using recursion for looping results in the stack overflowing and the whole thing crashing. Instead, you need to wrap the entire thing in a while() loop.

LASER_REGEN()
while(src.regenerating)
health += 10 //whatever
sleep(25)


As for a tutorial on embedded expressions... they are precisely the same as expressions anywhere else. What are you having an issue with?
In response to Garthor
Well, I wanted to put an icon in the stat panels and im not sure how to do that, so in the "status" stat panel it would show roughly

Current equipped weapon : [icon of wep. here] Name Of Weapon.

"Infinite loop suspected--switching proc to background.
If it is not an infinite loop, either do 'set background=1' or set world.loop_checks=0.
proc name: LASER REGEN (/obj/proc/LASER_REGEN)
usr: DarkNinjaPrograms (/mob)
src: the laserfence (/obj/Outdoors/laserfence)
call stack:
the laserfence (/obj/Outdoors/laserfence): LASER REGEN()
the laserfence (/obj/Outdoors/laserfence): damage(11)
DarkNinjaPrograms (/mob): attack()"

Should it be set to background, or did i do something wrong

revised code
        LASER_REGEN()
while(src.regenerating)
if(src.health>=src.maxhealth-10)
src.health=src.maxhealth
world<<"TOMAX"
src.regenerating=0
if((src.health/src.maxhealth)*100>10) //percentage of the health is above 10
src.health += round(((src.maxhealth-src.health)/src.maxhealth)*50)//add a percentage of the health
world<<"+ #"
world<<"HP: [src.health]"
src.PercentageSet()//run yadayada proc
sleep(25)//two and a half seconds later
return
mob/proc
regen()
//do your actions
src.hp+=rand(1,10)
if(src.hp>=src.maxhp)//cahnge it to what you want
src.hp=src.maxhp
else
spawn(delay)
src.regen()

so basically this will keep doing your action of increasing ´hp till the hp is fully filled
In response to DarkNinjaPrograms
To show an icon on a statpanel, show an obj.

As for the infinite loop, that shouldn't be happening, unless PercentageSet() is doing something strange.
In response to Karffebon
That has nothing to do with this at all. Thanks.
In response to Garthor
i acidentaly pressed tab and space so it posted before i finished writing now it's edited
In response to Garthor
It simply is a proc to see what percentage the health is at, and changing the icon state to the appropiate icon for the amount of health it has(cracks in windows, The laser part disapating, etc).. I was showing the obj but it wasnt showing the icon for some reason
In response to Karffebon
In that case: using spawn() to avoid overflowing the stack when using recursion is a band-aid fix at best, and incredibly silly. As I've already said, a proper looping construct, like while(), should be used.
In response to DarkNinjaPrograms
Well, the only reason you should be getting the "infinite loop suspected" error is if the regenerating variable is getting reset to 1 during each iteration.

I suppose a more straightforward fix is to tell you that the second if() really needs to be else if(), or it's going to run into both of those and cause issues.