ID:144984
 
Code:
    Enemy_Hunt()
while(src)
for(var/mob/player/M in view(5))
step_towards(src,M)
if(prob(100))
Ki_Blast()
sleep(5)


Problem description:Okay here is a code that makes my enemy NPC's go after someone if they step into view of them. The only problem: it doesn't skip over the Ki_Blast proc when they aren't moving. How do I change it so it only shoots after it has stepped towards someone?

P.S. Ignore the 100 probability. I will change that later. I only made it such so that I could test it.

That for() loop would never work properly in the first place. It will keep looping through all the mobs in view and step towards them. How could this become a problem you ask? If you have 10 people in view, it will step towards one then towards another and it'll keep going til it stepped towards all 10 then do it again... So it may never even get to a person. You may want to give NPCs a target variable and have them step towards that.

mob
var
target=null
NPC
proc/Hunt()
while(src)
if(src.target!=null)
if(src.target in view(src))
step_towards(src,src.target)
if(prob(100))
src.KiBlast()
sleep(5)
else
src.target=null
else
for(var/mob/player/M in view(src))
src.target=M
break

This will check to see if NPC has a target, if it does and it's target is in it's view, it will step towards it. If the target isn't in view, it will pick a new target or just not do anything if there are no players around. If they don't have a target then it gets one for em.

Have fun.

-Exophus
In response to Exophus
I have one question...Is using if(src.target!=null) the same as if(src.target)? I thought it was but I could be mistaken...
In response to Kireis
Yes, I'm pretty sure it's the same. Not sure why I did that, heh. I've been sick the past few days so I might be doing a few unneeded things.

-Exophus
In response to Exophus
Okay now my code is:

Enemy_Hunt()
while(src)
if(src.target)
if(src.target in view(src))
step_towards(src,src.target)
if(prob(20))
src.Ki_Blast()
sleep(5)
else
src.target= null

else
for(var/mob/combatant/player/M in view(src))
src.target= M
break


But whenever I run my game I get:

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: Enemy Hunt (/mob/proc/Enemy_Hunt)
usr: Raditz (/mob/combatant/NPC/Raditz)
src: Raditz (/mob/combatant/NPC/Raditz)
call stack:
Raditz (/mob/combatant/NPC/Raditz): Enemy Hunt()
Raditz (/mob/combatant/NPC/Raditz): New(Grass (65,71,1) (/turf/Ground_Turfs/Grass))

So what's wrong with it?
In response to Kireis
Try adding a sleep(1) at the beginning of the while().

-Exophus
In response to Exophus
That makes the game super laggy. And I still get the error
In response to Teris
Show me what you have?

-Exophus

[edit]
When calling this proc on a mob it should be done so with a spawn() since it contains a while() loop, like so.

spawn()
src.Hunt()

Instead of just
src.Hunt()
In response to Exophus
mob/combatant/NPC
Raditz
icon= 'NPC\'s.dmi'
icon_state= "Raditz"
maxpl= 50
pl= 50
maxki=100
ki=100
New()
Enemy_Hunt()
..()
Bump(mob/combatant/player/M)
if(istype(M,/mob/combatant/player))
Attack(M)

Enemy_Hunt()
sleep(1)
while(src)
if(src.target)
if(src.target in view(src))
step_towards(src,src.target)
if(prob(20))
src.Ki_Blast()
sleep(5)
else
src.target= null

else
for(var/mob/combatant/player/M in view(src))
src.target= M
break

That's all the important stuff I believe.
In response to Teris
Yeah, I think you can take out the sleep(1). I forgot about the spawn() (See previous, editted, post).

-Exophus
In response to Exophus
mob/combatant/NPC
Raditz
icon= 'NPC\'s.dmi'
icon_state= "Raditz"
maxpl= 50
pl= 50
maxki=100
ki=100
spawn()
Enemy_Hunt()
Bump(mob/combatant/player/M)
if(istype(M,/mob/combatant/player))
Attack(M)


Okay I changed it to this. But i'm getting an error.

Enemy NPC's.dm:10:error::empty type name (indentation error?)
In response to Teris
Well which line is 10? I suppose it might be an identation error.

-Exophus
In response to Exophus
It's not. Line 10 is the one that calls the Enemy_Hunt() proc. It says that when I put it inside the spawn() proc. It never did that when it was inside the New() proc though.
In response to Teris
Oh, I didn't notice it before.

mob
enemy
New()//Put this and following things at correct space.
..()
spawn()
src.Enemy_Hunt()

That should work. I didn't notice you just threw the spawn in there. :\

-Exophus
In response to Exophus
Lag and the same error :( I think the problem really is the loop.
In response to Teris
Lag and the same error... Everytime I get that and nothing else is wrong, I have to add a sleep(1) and it works fine. Try adding that at the beginning of the while() again and trying it.

-Exophus
In response to Exophus
Alright. It works. Thanks alot for your help man. We spammed the forum up quite a bit lol.
In response to Teris
You can say that again. :P If you need anymore help feel free to post again and if I see it I'll try to help ya out again.

-Exophus
In response to Exophus
Lol speaking of which I have another post in Developer How-To that I haven't been able to get anyone to answer. It seems like it's just a simple problem though.

http://developer.byond.com/forum/ index.cgi?action=message_read&id=473052&forum=8&view=0