ID:2334596
 
(See the best response by Nadrew.)
This works fine as it allows the mob to move on its own in random directions
mob/living/monster/butterfly
icon = 'Butterfly.dmi'
icon_state = "butterfly_white"
New()
AI()

mob/living/proc/AI()
spawn while(src)
start
walk_rand(src,10)
sleep(10)
goto start

BUT no matter what I use I can't seem to get the AI() proc to stop when the mob dies.

I have this for the death part
mob
proc
DeathCheck() //check to see if an attack is deadly
if (hp <= 0) //if the defender's HP is low enough...
world << "[src] dies!" //give the death messaging
src.icon_state = "dead" //Also trying to find a replacement for this but ill save it for another time
src.dead = TRUE

Before you say it I have tried using
mob/living/monster/butterfly
icon = 'Butterfly.dmi'
icon_state = "butterfly_white"
New()
if(src.dead == FALSE)
AI()

As well as trying to while(src.dead == FALSE) instead of if but nothing has worked so far and I'm at a loss.
Best response
You don't need 'goto' inside of while, that's incredibly redundant. Using while(!dead) would terminate the loop, you'd also want an
if(dead) break


In the loop to make sure it breaks out of the loop early if need.
In response to Nadrew
Nadrew wrote:
You don't need 'goto' inside of while, that's incredibly redundant. Using while(!dead) would terminate the loop, you'd also want an
> if(dead) break
>

In the loop to make sure it breaks out of the loop early if need.

I see the while(src) was already making the loop and spawn was also pointless...(I think).

Thank you for the help so far but where would I use while(!dead)?
Instead of while(src), you could use while(!dead), that way the loop would only run when the NPC is alive. When the NPC comes back to life you'd simply call AI() again after setting dead back to true.

If the monsters won't be respawning, you'd just delete them and not worry about the loop.
In response to Nadrew
mob
var
dead = FALSE

mob
proc
DeathCheck() //check to see if an attack is deadly
if (hp <= 0) //if the defender's HP is low enough...
world << "[src] dies!" //give the death messaging
src.icon_state = "dead"
src.dead = TRUE

mob/living/monster/butterfly
icon = 'Butterfly.dmi'
icon_state = "butterfly_white"
New()
AI()

mob/living/proc/AI()
while(!dead)
walk_rand(src,10)
sleep(10)


Currently what I have but the mob continues to move even after DeathCheck() ...I hate bothering other people for help
You want step_rand() here and not walk_rand(), walk_rand() loops on its own and has to be terminated with walk(ref,null)
In response to Nadrew
step_rand() seems to make the mob follow a direction a longer then walk_rand() is there a way to change that?

Better explanation would be
step_rand() and the mob will go south east a few tiles then change to south for a few tiles in the loop

walk_rand() and the mob seems to go a different direction 90% of the time
They're both on a fairly limited number generator, most people pick the direction themselves.

var/list/direction_list = list(NORTH,SOUTH,EAST,WEST)
while(!dead)
var/step_direction = pick(direction_list)
step(src,step_direction)
// and so on.