ID:158917
 
Hi DL
This is an example of how to use the (goto) command and how to label code.
restart // You can label anything at all just by typing whatever you want in front of your code
if(Damage >= 0)
M.HP -+ Damage
else
goto restart //this tells it to go to restart which you labeled at the top

//another example would be

one
if(Damage >= 0)
M.HP -+ Damage
goto one
You'll find that most of the time (all the time), goto is actually a sign of bad coding. While() is a much better system.
ReaperRoflCopter wrote:
This is for a friend
Hi DL

Don't use the developer forums as a method of private communication to your friends... they're not meant for that. If you want to show your friend something, then... just show it to your friend, essentially?

This is an example of how to use the (goto) command and how to label code.

Yeah, a very good example of when NOT to use goto. In fact the cases where goto actually should really be used are very rare enough that most people should probably never use it.
Instead of goto, you should teach your friend to use proper looping constructs that don't lead into messy and convoluted spaghetti code, like while() and for().
In response to Kaioken
Thank you for the constructive criticism. But i should also say, i posted it in the dev how to because i knew someone would read it , thus letting others follow. Could you give me an example of how and where you could use the.

while()
//and
for()
In response to ReaperRoflCopter
//while() for health regeneration
while(HP>MHP)
HP++
sleep(1)

//for() usually used in AI code
for(var/mob/M in view(8,src))
if(M.client)
Attack(M)
In response to Ripiz
That way your health regeneration will stop forever once you're at full health. If you're looking for a passive way to regen, that won't work, but it's a good use of while() nonetheless.
In response to Ripiz
A better description of their uses would be:

1) Use while() when you want to continue doing something until some condition changes.
2) Use do while() when you want to do something at least once, and continue doing it until some condition changes.
3) Use for() loop when you want to repeat something a relatively fixed number of times.
4) Use for() list when you want to search through a list of some sort.

So, for an example of each, using the theme of a mob AI:

mob/ED-209
var/isScrapMetal = 0
proc/AI()
//while()
while(!isScrapMetal)
//for() list:
for(var/mob/M in oview(src))
if(M.weapon != null)
threaten(M)
sleep(10)

proc/threaten(var/mob/M)
view(src) << "[src]: Please put down your weapon. You have 20 seconds to comply."
//for() loop:
for(var/v = 15, v >= 5, v -= 5)
sleep(50)
//I considered putting a check to see if M had dropped its weapon
//but that just didn't make sense
if(!(M in view(src)))
return
view(src) << "[src]: You have [v] seconds to comply."
//final countdown, for() loop again:
for(var/v = 4, v >= 0, v--)
sleep(10)
if(!(M in view(src)))
return
view(src) << "[src]: [v]..."
view(src) << "[src]: I am now authorized to use physical force."
//pew pew
shoot(M)

proc/shoot(var/mob/M)
//do while()
do
dir = get_dir(src,M)
flick("shoot", src)
M.hurt(20,src)
while(M in view && M.alive)


Actually, that's not a very good example of a do while(). It's a bit hard to think of an example of where to use one off the top of my head, they're not very common.
In response to Garthor
Thanks heaps i appreciate your help.