ID:141957
 
Code:
    proc
ai()
spawn(src.delaytime) while(src)
world<<"loop while1, (while(src)"
if(!src.target)
world<<"I have no target."
step(src,src.dir)
for(var/mob/m in world)
if(m.playing)
world<<"Found [m], targetting."
src.target=m
break
break
var/mob/n=src.target
while(n in world)
if(!n.playing)
world<<"Lost my target. restarting the procedure."
break
src.ai()
else
world<<"[n] is still alive. waiting my delay time to step towards him."
sleep(src.delaytime)
step_towards(src,src.target)


Problem description:

I'm probably doing something really really stupid, but it's gotten to the point where it freezes up when I try stuff >_> What am I doing? :/


--I also only started using the 'break' function for this, so perhaps I'm not using it correctly, but I suspect an infinite loop that I cannot spot >_>


Break ends the current loop; you're trying to do stuff after you end it for some reason. Why do you have 2 breaks, by the way?

Why are you looping through every mob to get a target when you can use locate()?

The big problem is probably just that you aren't sleeping where you should be, though. Sleep every time you have it do a loop.

Your src.ai() line will just call more procs and slow the program down (if you didn't break before it, at least >_>)

                   src.ai()



Efficiency note: while(src) is the same thing as plain old for(), but less efficient because it checks src every loop.




How break works: You can view break as the return of a loop in a sense. It completely ends the loop it's in.

mob/verb/Loop()
for()
break
world << "This won't be outputted because I ended the loop"

mob/verb/Loop2()
for()
for()
break
break //this does nothing because the above line ended the loop
break //this will end the first loop, though


You can also use labels to break loops higher up in the chain.

mob/verb/Label_Loop()
label:
for()
for()
break label //ends the label loop (the first one)