ID:139052
 
Code:
mob
FollowNPC
name = "Generic FollowNPC"
icon = 'base.dmi'
var/mob/target = 0
var/mode = "Total Retaliation"
var/weapontype = ""
New()
..()
serp
if(mode == "Attack")
var/mob/D = locate() in oview(src,8)
world << "It's in attack mode!"
if(!src.target)
world << "It doesn't have a target!"
if(D)
world << "It found someone!"
if(!target && src.owner != D)
target = D
else
world << "It isn't finding anyone."
// irrelevant pieces of code
if(src.target != 0 && src.target)
// if(get_dist(src,src.owner) <= 8)
if(get_dist(src,target) <= 8)
step_towards(src,target)
else
src.target = 0
src.canattack = 1
/* else
src.target = 0
src.canattack = 1*/

else
if(src.target != 0)
src.target = 0
src.canattack = 1


Problem description: This is the code for an NPC that follows you around and fights for you. I cut out irrelevant pieces of code and did not include everything necessary to make it function, but this should give you the gist of what I'm trying to do. The problem I'm having is that when Mode is set to Attack it doesn't find any mobs so target never gets set and therefore it doesn't attack them. Any suggestions?
When you create it, do you make sure to have it's mode set to attack?
In response to Lugia319
If you have it set to attack and it still does not work, you may want to try a different method of finding mobs, such as:
for(var/mob/m in view(src,8))
if(m.client)
target=m


EDIT: Also, why are you associating a numerical value to a /mob variable? Just leave it blank, there is no need for any = sign unless you want to give them a default target. If you want to give them a default target, you should set it to a /mob.
In response to Lugia319
Lugia319 wrote:
When you create it, do you make sure to have it's mode set to attack?

No, Mode gets set in-game. All the world statements show up, but it says it's not finding anyone.

Albro1 wrote:
If you have it set to attack and it still does not work, you may want to try a different method of finding mobs, such as:
> for(var/mob/m in view(src,8))
> if(m.client)
> target=m
>


I already tried something similar to that with oview and it didn't change the outcome.
In response to Unwanted4Murder
Well, the only way it's going to look for a target is if it has the Attack mode when it is created, thus the New() proc.
In response to Lugia319
Lugia319 wrote:
Well, the only way it's going to look for a target is if it has the Attack mode when it is created, thus the New() proc.

I didn't include the whole thing... it actually loops back to serp with a goto. I didn't include the goto line because I knew people would criticize me for it being inefficient, but I dunno how else to do it.
In response to Lugia319
Lugia319 wrote:
Well, the only way it's going to look for a target is if it has the Attack mode when it is created, thus the New() proc.

This.
If you want a working AI proc, take a look at Forum_account's AI "library". It shows you how. Use an ai() proc, and do a
spawn(1) ai()

at the end of it.

Then it will constantly cycle once it is created(Of course you have to call ai() in New().)

Thanks Lugia, I was looking at the specific code and completely ignored the fact that he was using all of this under New().

EDIT: To reply to your last post, U4M, keeping New() from completing is probably not the best idea. Best let the New() proc complete itself. Use a separate proc(My suggestion above) to do your AI actions.
In response to Albro1
I changed it to a seperate proc and have it set to run itself after a 3 second spawn(). No change. Any other suggestions?

Edit: I just noticed that it also doesn't announce it when the NPC KOs another NPC / player (view(M,10) << "[M] has knocked out [src]!") or announce in the world chat when the NPC kills another player (world << "[M] has killed [src]!"). Could this be related?
In response to Unwanted4Murder
Here is a snippet for finding a target, if you're still in need of help.

<small>Edit: It's not messaging because you have it doing player( range/view/world << message ).</small>

mob

Mob

name = null

icon = null

var

/* the target variable. */ mob/target = null

/* the mode of the 'Mob'. */ mode = null

New()

..()

spawn() /* call the system. */ find_target()

proc

find_target()

while( /* there is no target. */ !target )

/* loop through the mob's view. */ for( var/mob/player in oview ( src , 8 ) )

/* check if they are a client. */ if( player.client )

/* set the target. */ target = player

world << "A target was found! [target]"

break

/* if there is no target, announce no target was found. */ if( !target )

world << "No target was found!"

sleep( /* however long you want it to tick inbetween loops. */ 5 )