I'm having a little problem with this very, very, very basic battle engine I've written, and quite frankly, it's driving me crazy. I can't add any depth to this proc unless I can figure out why it doesn't always work. Here's the deal: every once in a while I get this error.
runtime error: Cannot read null.NPC
proc name: Fight (/mob/proc/Fight)
usr: Player (/mob)
src: the thug (/mob/newbierapist)
call stack:
the thug (/mob/newbierapist): Fight(null)
the thug (/mob/newbierapist): Fight(Player (/mob))
Player (/mob): Fight(the thug (/mob/newbierapist))
No idea why. I'd say that it happens maybe once in every 25 times I select the attack option, but if it happens at all, I'd say it's too much. If anybody here could take a look at the code for my Fight proc and just take a stab at what might be generating that error, I would really appreciate it. Personally, I can't see how it could be the code at this point, and it does seem to me as though I usually get this error if I try to execute the fight command very shortly after logging in (say, within 5 seconds). But take a look and tell me if there's something I'm missing.
Fight(mob/M)
var/sleeptime = round(2000/src.quickness)
if (sleeptime > 55) sleeptime = 55
else if (sleeptime < 20) sleeptime = 20
do
if ((M.NPC) && (M.warmode == 0))
M.warmode = 1
M.enemy = usr
sleep(10)
spawn M.Fight(M.enemy)
else if (src.NPC)
var/mob/topscorer
topscorer = MostDamage(src)
if (topscorer != src.enemy)
src.enemy = topscorer
Fight(src.enemy)
return
if (!src.CanHit(M))
usr << {"
"}
sleep(sleeptime)
else
var/damage = rand(src.low_dam,src.high_dam)
if (M.NPC)
AddDamage(M,damage)
M.HP -= damage
usr << {"
"}
sleep(sleeptime)
while (src.warmode == 1)
Could it be an issue with the Dream Maker client, itself? If so, I could probably write a routine to prevent the user from attacking anything for, say, five seconds after login. If you have any questions about what's going on in that code, just ask me. I realise it's pretty darn sloppy. I'm sort of a "get it working and then clean it up" kind of guy.
Thanks!
-Justin
<font color="grey" [src.charname] misses [M.charname].</font>"}
<font color="grey" [src.charname] hits [M.charname] for [damage] points of damage.</font>"}
From the error output...I can tell that the error is coming up when the thug calls the fight proc for the second time... The error output call stack works from the bottom up... So it says that the player called the proc...then the thug called it... And then the thug called it again...and that's where the error is coming from...
And the error is saying that the M argument in the Fight(mob/M) proc is null when the proc is called that second time...
So we take a look at the code and narrow the problem down to this section:
var/mob/topscorer
topscorer = MostDamage(src)
if (topscorer != src.enemy)
src.enemy = topscorer
Fight(src.enemy)
return
...since this is the section of code that is called when an NPC uses this proc... More specifically...since we know that the problem comes from the second call to Fight()...we know that the problem is in the line I've marked in bold...
So what does that mean? It means that src.enemy is null at this point... Or at least not a mob reference... So when Fight(src.enemy) is being used to call Fight(mob/M)...src.enemy isn't being successfully passed (for whatever reason) to mob/M...and it's being defaulted to null and giving you the error...
I'd say that the problem is in the MostDamage(src) proc... topscorer is being set to whatever that proc returns...and then that is being set to src.enemy... So whatever that proc is returning is not compatible with mob/M...
Perhaps this is enough info for you to fix the problem... If not...I think posting your code for the MostDamage() proc will let us give you the answer you're looking for...
Sorry if most of this sounds too complicated... It makes sense in my own head...but that may only be because I know enough to understand what's going on... Although your code seems knowledgeable enough that I think you'll understand what I'm saying... So hopefully I've been a help...lol