ID:266716
 
Why doesnt this work?
mob
NPC
Security
icon = 'security.dmi'
density = 1
New()
.=..()
spawn() for(var/mob/M in oview(7))
if(M.key == "Thief jack")
src.dir = (get_dir(src,M))
var/obj/laser/B = new(src.loc)
walk(B,src.dir)
sleep(10)
del(B)

And how do I fix it so I dont have to use the : proc on the obj/laser?
obj
laser
icon = 'laser.dmi'
density = 1
Bump(atom/M)
if(ismob(M))
M:HP -= 5
M:Die()
else if(M.density == 1)
del(src)
Thief jack wrote:
Why doesnt this work?
mob
NPC
Security
icon = 'security.dmi'
density = 1
New()
.=..()
spawn() for(var/mob/M in oview(7))
if(M.key == "Thief jack")
src.dir = (get_dir(src,M))
var/obj/laser/B = new(src.loc)
walk(B,src.dir)
sleep(10)
del(B)

I think the reason it's not working is that right after the mob is created, it goes into a loop to check all mobs in view and shoot at them. The problem is, it will only shoot at the mob if it's you (which I think is the opposite of what you intended, unless I'm mistaken), and once it loops through the mobs that are in view that first time, it stops; it never goes through the loop again. Since the security laser is probably created before you are, it never sees you in time to shoot at you.

I'd rewrite it like this:
mob/NPC/Security
icon = 'security.dmi'
density = 1
New()
..()
spawn()
Scan()

proc/Scan()
var/slept
for(var/mob/M in oview(7))
slept=0
// Since there's a timer in this loop, mobs in oview(7)
// might not remain in oview(7) after the sleep().
if(!(M in oview(7)) continue
if(M.type==type) continue // don't shoot other turrets
if(M.key == "Thief jack") continue // I think this was your intent
src.dir = (get_dir(src,M))
// I'm adding a second arg to B.New() so the laser knows who fired it
// Odds are that'll come in handy for you later
var/obj/laser/B = new(src.loc,src)
walk(B,src.dir,2) // 1 tile every 1/5 second; fast
slept=1
sleep(10)
del(B)
spawn(slept?(-1):10) Scan() // respawn this proc

I think what you really want is something more like that.

And how do I fix it so I dont have to use the : proc on the obj/laser?
obj
laser
icon = 'laser.dmi'
density = 1
Bump(atom/M)
if(ismob(M))
M:HP -= 5
M:Die()
else if(M.density == 1)
del(src)

Minor problem on the else if: M.density will automatically be 1 if it's bumped at all. You can safely get rid of the else if; del(src) should be called at the end of Bump() anyway unless you intend the laser to keep passing through.

To get rid of the : operator, you need to cast the item to a /mob type so the compiler knows what it's dealing with.
Try this:
obj/laser
var/atom/owner
icon = 'laser.dmi'
density = 1
New(newloc,atom/newowner)
..()
owner=newowner
// You could even move walk() here if you wanted to,
// by checking newowner.dir yourself.

Bump(atom/A)
if(!A) del(src) // this is a failsafe
var/placehit=locate(A.x,A.y,A.z)
if(ismob(A))
var/mob/M=A
M.HP=max(M.HP-5,0)
if(!M.HP) M.Die()
if(A.type==type)
loc=A.type // pass through other lasers
// you might need to call walk() again; I don't know
return
new /obj/laserhit(placehit,src) // special effects
del(src)

Lummox JR