ID:141451
 
Code:
mob
Unit
proc
Attack()
spawn(20)
src.Attack()
var/d=100
var/mob/Unit/E
for(var/mob/Unit/e in orange(4))
if(get_dist(src,e)<d)
if(e.owned!=src.owned&&src.dead==0)
if(e.dead==0)
d=get_dist(src,e)
e=E
if(E)
step_to(src,E)
flick("shoot",src)
E.hp-=src.dmg
if(E.hp<=0)
E.death(E)
src.kills++
if(!E)
var/obj/Buildings/b
for(var/obj/Buildings/B in orange(1))
if(B.owned!=src.owned)
if(src.dead==0)
b=B
if(b)
flick("shoot",src)
b.hp-=4
if(b.hp<=0)
b.dead(b)


Problem description:
The code above is supposed to make the Soldier who sees an enemy within 4 tiles, move towards him and attack him.
(I know, you may see that theres no way for the victim to dodge any bullets but thats how its intended.)
It also checks for buildings withing 1 tile, and then attacks it until it gets destroyed.

The problem: It doesn't do anything, the Soldiers stand there and look at each other.

One, orange/oview/view etc all use "usr" by default. This isn't what you want. You should use "orange(1, src)", "orange(4, src)", etc.
Two, make sure Attack is called when the soldier is created.
In response to Immibis
I used what you said(putting src where needed) and I've already had the Attack() proc call when a soldier is created.
mob
Unit
New()
Attack()

But it still doesn't work, They stare into each other...
In response to Mickemoose
mob
Unit
New()
.=..()
spawn Attack()

Try that
In response to Bakasensei
Yeah, Still the same problem.
In response to Mickemoose
Try changing "e=E" to "E=e" (that's the problem with confusing variable names) and "if(!E)" to "else"
In response to Immibis
Wow, A simple mistake like that. But it works now!
Thanks.