ID:173287
 
ok heres the script and error
mob
verb
Attack()
set category="Combat"
for(var/mob/E in get_step(usr,usr.dir))
sleep(3)
if(E.NPC==0)
E.HP-=usr.Str
view()<<"[usr] attacks [E] for [usr.Str] HP!"
if(E.player==1)
if(E.HP<=0)
E<<"You died!"
E.HP=E.MHP
E.MP=E.MMP
E.loc=locate(2,2,1)
E.gold+=E.gold
E.gold=0
usr.Exp+=E.Expg
E.PK=0
LvlCheck(usr)
..()
return
else
if(E.HP<=0)
Respawn()
usr<<"You killed [E]!"
usr.gold+=E.gold
E.gold=0
usr.Exp+=E.Expg
LvlCheck(usr)
del E
return
else
usr<<"You can't attack an NPC!"
return


and the error is this

runtime error: Cannot read null.NPC
proc name: Attack (/mob/verb/Attack)
usr: Kyrael (/mob/Sabin)
src: Kyrael (/mob/Sabin)
call stack:
Kyrael (/mob/Sabin): Attack()

it gives this error when you keep attacking after the mob dies, any ideas?
You get the error because E is null because you sleep for 3 ticks between when you select them and when you do something with them. If the mob is deleted during those 3 ticks, then you get that error.
In response to Garthor
So exactly how do I fix it, lower the ticks? Or do I just simply remove a line?
In response to Kyrael
Either you make sure E still exists before you do anything to it after a sleep() (if(E)), or remove the sleep() altogether.

Even better, run a proc that belongs to E, that way, it immediately stops if E is deleted.
In response to Garthor
hmmm, where in my attack code would I add that proc, and what would the coding for that proc be?

Edit: Actually, nvm, how would I make the attack be slower without using sleep?
In response to Kyrael
The same way you slow down movement.

Besides, having sleep the way you used it doesn't make it slower. It just makes it so that there's a 3-tick delay between pressing attack and actually attacking. You still attack super-fast, just 3 ticks after you start holding down the macro.
In response to Garthor
Ok, Thanks, but one more question, whats the code to slow it down, because I've never had to slow a player down.
In response to Kyrael
I'd also suggest other methods than using a loop to find a mob. Use something like this:

mob
verb
Attack()
set category="Combat"
var/mob/M=locate() in get_step(usr,usr.dir)
if(M)
//do your attacking stuff here
In response to Goku72
Dosnt he have to define E? Or did I miss that?

-Chance-