ID:140132
 
Code:
mob/proc/AI()
for(var/mob/M in view(5))//within 5 steps
if(!M.key) return //dont attack if not player
else
if(get_dist(src,M) > 1)//if farther than one
step_to(src,M)
else
src.attack()


mob/proc/attack(mob/M)
if(src.str-M.def <=0)
view(2,src)<<"[M] has dodged [src]'s attack!"
sleep(5)
else
var/crit=rand(1,15)
var/critdamage=(src.str-M.def)*3
var/damage=(src.str-M.def)+rand(-M.lvl,M.lvl)
if(crit==1)
M.hp-=critdamage
M<<"[src] critically hit you for [critdamage]"
M.deathcheck()
else
M.hp-=damage
M<<"[src] hit you for [damage]"
M.deathcheck()


mob
New()//when a mob is created
sleep(20)
if(!src.key)//if it's not human
AI()
..()
else
..()

mob
icon='Enemies.dmi'
CircleDemon
icon_state="CircleDemon"
hp = 50
str = 7
def = 10
lvl=10
gold=40


Problem description:
This is a quick piece of code i put together in a couple minutes trying to pursue a slightly usable AI code(which i never could do) but the mobs just sit there and dont move :(
First, view(5) defaults to view(usr,5), and usr should not be used in procs. You should change it to oview(src,5).

Second, return is the wrong thing to have in that loop. You want "continue" to make it skip to the next mob in the list, rather than return which ends the entire proc.

Third, you don't have a loop there. You need to wrap the whole body of the AI() proc in a while(src) or something of that sort.

Fourth, after making AI() into an infinite loop, you'll want to use spawn() to call it in a separate thread (just "spawn() AI()" instead of "AI()") so it doesn't prevent New() from finishing.

Fifth, you aren't passing an argument to attack(). It should be attack(M) when you call it.
In response to Garthor
well now i get this error for every monster on the map

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: AI (/mob/proc/AI)
usr: CircleDemon (/mob/CircleDemon)
src: CircleDemon (/mob/CircleDemon)
call stack:
CircleDemon (/mob/CircleDemon): AI()
CircleDemon (/mob/CircleDemon): New(the turf (8,11,1) (/turf))

here is what it is after reading what you told me.

mob/proc/AI()
while(src)
for(var/mob/M in oview(src,5))//within 5 steps
if(!M.key) continue//dont attack if not player
else
if(get_dist(src,M) > 1)//if farther than one
step_to(src,M)
else
src.attack(M)


mob/proc/attack(mob/M)
if(src.str-M.def <=0)
view(2,src)<<"[M] has dodged [src]'s attack!"
sleep(5)
else
var/crit=rand(1,15)
var/critdamage=(src.str-M.def)*3
var/damage=(src.str-M.def)+rand(-M.lvl,M.lvl)
if(crit==1)
M.hp-=critdamage
M<<"[src] critically hit you for [critdamage]"
M.deathcheck()
else
M.hp-=damage
M<<"[src] hit you for [damage]"
M.deathcheck()


mob
New()//when a mob is created
sleep(20)
if(!src.key)//if it's not human
spawn() AI()
..()//continue on with normal New() (create and whatnot)
else
..()
In response to Scarymonkeys623
That would be because you didn't put anything INSIDE the while() block.
In response to Garthor
Garthor wrote:
That would be because you didn't put anything INSIDE the while() block.

i never got the hang of using while, thats why i prefer to stick with for, what should i put in while? i put for in before but that caused the game to crash 1 second after it starts
In response to Scarymonkeys623
You didn't put anything in your while(), as Garthor said. You need to intend right behind it.

while(statement)
DoThis
DoThat
After While


In your case, you'll be looping for ever, so you really should put a delay in it, else it will inevitably lock up.
In response to Emasym
thank you so much, you just reminded me that i forgot all about putting a delay in there XD, i took for() out of while() because it kept crashing every time i ran it since i forgot to add the delay


edit: well now it keeps crashing again :(
In response to Scarymonkeys623
You know, with "delay", we mean "sleep()", right?

while(src)
sleep(rand(20,60))
for(var/mob/M in oview(src,5))
// do stuff
In response to Emasym
i might not be a good coder but im not an imbecile :( its just i used to code long ago but quit for reasons and just starting back up. i know what you ment by delay and i have added on the sleep already, and i found the problem already and i never would of without you guys, thank you so much :)