ID:262310
 
Code:
mob
Enemy
var/Target
Met
icon = 'Met.dmi'
icon_state = "Left"
dir = WEST
MaxHP = 10
HP = 10
New()
if(src)
sleep(10)
src.Search()
return
proc
Search()
if(src)
sleep(5)
for(var/mob/Player/M in oview(6))
src.Target = M
break
if(src.Target)
src.SMove()
return
else
sleep(10)
src.Search()
return
else
return
SMove()
if(src)
sleep(10)
if(Target:x>=src.x)
step(src,EAST)
src.icon_state = "Right"
src.odir = "EAST"
src.GravCheck()
else
step(src,WEST)
src.icon_state = "Left"
src.odir = "EAST"
src.GravCheck()
src.SShoot()
return
else
return
SShoot()
if(src)
sleep(5)
for(var/mob/Player/M in oview(6))
src.Target = M
break
if(src.Target)
var/Shot = new/obj/Enemy_Shots/Met(src.loc)
walk(Shot,src.dir,0)
spawn(10) src.Search()
return
else
spawn(10)
src.Search()
return
else
return


Problem description:
Thats just one of the many enemies in my game. After about..oo 15-20 mins the game crashes...I cant see what Im doing wrong!

Is there a runtime error Or does it just crash like freezing DreemSeeker? When does it happen? Like are enemys in certain locations or doing somthing? Have you checked it with debug messages to see what they do right before it crashes?
In response to Green Lime
The game freezes for the host, and crashes for the players.
If you have ever played MM, I have Sniper Joes, etc enemies doin' there thing...
You have infinite loops all over the place as none of the returns are never gotten to. What is happening is each time a new proc is called the original waits for the new proc to return (which because other procs are called and don't return either, it just runs in an infinite loop). you want to spawn() off the new proc or use while() loops.
In response to Nick231
Nick231 wrote:
You have infinite loops all over the place as none of the returns are never gotten to. What is happening is each time a new proc is called the original waits for the new proc to return (which because other procs are called and don't return either, it just runs in an infinite loop). you want to spawn() off the new proc or use while() loops.

I remember when I was new to the system (actually even a year or so afterward) I was having this problem. The most effecient way is to use while() but the best way to debug is to use spawn(). Spawn at the last possible moment for the best debug capabilities (you can notice that the proc has crashed and find out why) or spawn at the earliest moment for the best runtime (the new proc should spawn before the old one can crash, thus you get error output if necessary and the proc keeps running).


~Polatrite~
In response to Polatrite
So you want me to do this
while(src)
PROC()

?
In response to Polatrite
In the perticular case above doing spawn()proc() would stop the infinite loop, using spawn() also allows for proper profiling of the individual portions of the procs, whereas while() loops never return and you can't get an accurate measurement of their cpu.
In response to ITG Master
ITG Master wrote:
So you want me to do this
> while(src)
> PROC()
>

?

Sort of. That would call PROC() endlessly as long as src existed.

Search()
while(src)
sleep(5)
src.Target=locate(/mob/Player) in oview(6,src)
if(src.Target)
src.SMove()
else
sleep(10)

What this does is for as long as while(src) is true do everything within it, and once at the end repeat if src still exists.
In response to Nick231
mob
Enemy
var
Target = ""
proc
Search()
return
Met
icon = 'Met.dmi'
icon_state = "Left"
dir = WEST
New()
src.Search()
Search()
while(src)
sleep(5)
for(var/mob/Player/M in oview(6))
src.Target = M
break
if(src.Target)
goto SMOVE
else
sleep(10)

SMOVE
if(src.Target:x>=src.x)
step(src,EAST)
else
step(src,WEST)
goto SATTACK

SATTACK
var/Shot = new/obj/Enemy_Shots/Met
if(src.Target:x>=src.x)
walk(Shot,EAST,0)
else
walk(Shot,WEST,0)

thats my new code, now I get these run-time errors when the games starts

runtime error: Cannot read "".x
proc name: Search (/mob/Enemy/Met/Search)
usr: Met (/mob/Enemy/Met)
src: Met (/mob/Enemy/Met)
call stack:
Met (/mob/Enemy/Met): Search()
Met (/mob/Enemy/Met): New( (9,4,2) (/turf/Ice_Man_Turfs/Undense/D))
runtime error: Cannot read "".x
proc name: Search (/mob/Enemy/Met/Search)
usr: Met (/mob/Enemy/Met)
src: Met (/mob/Enemy/Met)
call stack:
Met (/mob/Enemy/Met): Search()
Met (/mob/Enemy/Met): New( (21,4,2) (/turf/Ice_Man_Turfs/Undense/C))
runtime error: Cannot read "".x
proc name: Search (/mob/Enemy/Met/Search)
usr: Met (/mob/Enemy/Met)
src: Met (/mob/Enemy/Met)
call stack:

And it goes on...
Ok, I re-created all my enemies like this, and now the game never crashes BUT after hosting for maybe i would say 15-30 mins It get jumpy...and laggy, I still dont think I have perfected it!

ALSO - I have lots of sound effects, just like the real game, and maybe thats lagging?
mob
Enemy
var
Target
proc
Search()
return
Met
icon = 'Met.dmi'
icon_state = "Left"
dir = WEST
MaxHP = 7
HP = 7
New()
spawn(10) src.Search()
..()
Search()
while(src)
sleep(5)
for(var/mob/Player/M in oview(6))
src.Target = M
break
if(src.Target)
goto SMOVE
return
else
sleep(10)

SMOVE
if(src)
sleep(10)
if(src.Target)
if(src.Target:x>=src.x)
step(src,EAST)
src.GravCheck()
else
step(src,WEST)
src.GravCheck()
goto SATTACK
else
sleep(10)

SATTACK
if(src)
sleep(5)
if(src.Target)
var/Shot = new/obj/Enemy_Shots/Met(src.loc)
if(src.Target:x>=src.x)
walk(Shot,EAST,0)
else
walk(Shot,WEST,0)