ID:142884
 
Code:
mob
verb
Attack(mob/M in oview(1))
M.deathcheck()
if(!M == src)
usr << "No target to attack!"
return
else
var/randumdmg = rand(1,3)
var/dmg = usr.atk * randumdmg - M.def
if(dmg <= 0)
if(prob(5))
usr << "You do [dmg] damage to [M]!"
M << "[usr] damages you for [dmg] damage!"
M.hp -= dmg
M.deathcheck()

else
usr << "You miss [M]!"
M << "[usr] misses you!"
return
else
if(rand(1,20) == 20)
usr << "Critical Hit!"
dmg *= 2
usr << "You do [dmg] damage to [M]!"
M << "[usr] damages you for [dmg] damage!"
M.hp -= dmg
M.deathcheck()

else
usr << "You do [dmg] damage to [M]!"
M << "[usr] damages you for [dmg] damage!"
M.hp -= dmg
M.deathcheck()

mob
proc
deathcheck(mob/M)
if(M.hp <= 0)
var/e = round(M.lvl * 25)
usr << "You kill [M]!"
usr << "You gain [e] Experince."
usr.exp += e
del M
..()


Problem description:
im trying to make it so whenever you attack it checks if the mob is dead (deathcheck) but its not working? anyway i could make it so it always checks even outside the attack verb... Thanks for any help
oh sorry for bugging you guys i fixed it by

                        M.deathcheck()//changing this
M.deathcheck(M)// to this
In response to Nategrant
but i still want to know if there is a way for it to check like all the time like within a spawn for() but that only works under login()
if(usr.dead == 1){usr << "Cannot attack the dead";return}
In response to Curzon
that has nothing to do with my problem sorry.... i said it didn't call the deathcheck proc but i fixed that...
In response to Nategrant
This is the most (well, one of anyways) ugliest snippet I have ever seen.

Let's fix it up (read the comments):
mob
verb
Attack(mob/M in oview(1))
// M.deathcheck() <-- Why do you even need this here?
if(!M == src) // Incorrect, you are checking if(1 or 0 == src)... what are you really trying to do here? If you are checking for no M, just do if(!M), that'll work perfectly
usr << "No target to attack!"
return
// else No point for else as you used return in the above if() statement, which stops the rest of the program of this verb from happening. But, eh, your choice.
var/randumdmg = rand(1,3)
var/dmg = usr.atk * randumdmg - M.def // Why not just add the randomizer in there? dmg = usr.atk*rand(1,3)-M.def
if(dmg <= 0)
if(prob(5)) // Uh... so if an attack does -100, which they will GAIN that HP, you want it to happen? I think you want >=, meaning if dmg is MORE/EQUAL THAN 0; <= is less/equal than, which includes -200
usr << "You do [dmg] damage to [M]!"
M << "[usr] damages you for [dmg] damage!"
M.hp -= dmg
M.deathcheck()

else
usr << "You miss [M]!"
M << "[usr] misses you!"
return
else // Ugh, the rest is a bit too messy for me to split apart
if(rand(1,20) == 20)
usr << "Critical Hit!"
dmg *= 2
usr << "You do [dmg] damage to [M]!"
M << "[usr] damages you for [dmg] damage!"
M.hp -= dmg
M.deathcheck()

else
usr << "You do [dmg] damage to [M]!"
M << "[usr] damages you for [dmg] damage!"
M.hp -= dmg
M.deathcheck()

// What most people fail to realize is that making a generalized procedure for taking health, etc (ex: mob/proc/TakeHp(dmg)) is a better way to go.
// Because, if you need to change something in the HP take-away, than you would need to change one line or so in the proc; not go throughout the whole source

mob
proc
deathcheck(mob/M) // Usually, people make M the killer and src, who the proc was called from, the dead man
if(M.hp <= 0)
var/e = round(M.lvl * 25)
usr << "You kill [M]!" // EWWW! No usr in procs! Look up what usr means, it is not the killer!!! It is the first person who died in this case!
usr << "You gain [e] Experince."
usr.exp += e
del M // Why delete the poor player?
..() // Absolutely useless here, unless you are planning to overwrite somewhere later under another path
In response to GhostAnime
well i just looked at end... and i'm just a beginner coder so stop screaming at me "the is the ugliest"-why thank you.... most of the things what are not needed are for further use or testing which i forgot to take out.


i also think you got messed up in proc... on the line for usr << etc. you said no usr.... because it would send the message to the dead guy which doesn't happen while i try it
                del M  // Why delete the poor player? //i also looked at this and I'm deleting the mob not the player.... and the mob because I'm killing them ??
            if(!M == src)
usr << "No target to attack!"
return
(doody head!) that means if M not is src o.o and the else means it just will attack IF THE M = THE ATTACKER i will (slap the monkey) you mean seriously!!!


by the way M.deathcheck(src) it should go so not M.deatchcheck or the src is going to be null in the proc GENIUS


(edited out swearing. -the digitalmouse)
In response to Nategrant
Nategrant wrote:
i also think you got messed up in proc... on the line for usr << etc. you said no usr.... because it would send the message to the dead guy which doesn't happen while i try it

No I didn't, I said it would be sent to the first guy who initiated the procedure, which is pretty much a dead guy (this testing would work if you had at least two keys logged in with one of them killing the other and vice-versa the second time). If you looked up usr in the reference...

http://www.byond.com/members/ DreamMakers?command=view_post&post=35932

I never screamed at you about it being the ugliest, I just mentioned it as I am sure it is quite physically impossible for me to scream it on the web without it being on an audio file >_>

And as a beginner programmer, this is the best time to learn some rather good programming practices rather so something will not creep up on you and smack you around without you finding out the cause of the said problem (which usr loves doing, if used incorrectly)

As for the deleting the poor player comment... what if M was another player? Unless this is a one-player game, someone will eventually attack another player and kill them... and guess what happens? If you are planning to delete non-client /mob's, than check if they don't have a client: if(!M.client) del M

In case you didn't know: client --> player reference who is logged in (so NPC as no client)
In response to Pirata Inmortal
by the way M.deathcheck(src) it should go so not M.deatchcheck or the src is going to be null in the proc GENIUS

src is a reference to the source of the procedure, the object it belongs to.
In response to Keeth
genius he used mob/M o__o hes not defining the killer as the src
In response to Pirata Inmortal
Actually he was defining the killer as usr, which is right.
Since the procedure is going to be called by the mob that used the attack verb.

What he really wanted was deathcheck(M).
Although it's weird to call the death check from another mob.
In response to Keeth
usr should never be used in a proc such as this one. So no, using usr in it was not "right." Calling src.deathcheck(M) when attacking something is backwards and insane and is going to result in a huge headache if you want to actually do anything with deathcheck(). This is pretty much the only proper way of writing a deathcheck proc:

mob/proc/deathcheck(var/mob/killer)
world << "[killer] has killed [src]"
del(src)
In response to Pirata Inmortal
Pirata Inmortal wrote:
            if(!M == src)
> usr << "No target to attack!"
> return
DUMB ASS that means if M not is src o.o and the else means it just will attack IF THE M = THE ATTACKER i will bitchslap you mean seriously!!!
by the way M.deathcheck(src) it should go so not M.deatchcheck or the src is going to be null in the proc GENIUS

Pirata Immortal,

Tone down your attitude - You are neither helpful nor show respect. If you want to flame people for asking for help, find another website to do it on than BYOND. Ironically, your assessment of the code in question is also incorrect.
In response to Alathon
i edited his reply to be a bit more socially acceptable - but only just!
In response to Keeth
Thank you for proving it works... because it did ever since i said i fixed and i tryed with others and everything works perfectly.. idk why i cant make the usr talk because when you kill eachother it works fine...

Thanks agian keeth for not yelling at me for starting to learn how to code... lol
In response to Nategrant
The problem with using usr outside of a verb (or even in a verb if you ask me) is that it can end up causing weird seemingly one-off bugs later in the project, especially in a death checking proc.

For instance, do you think letting players set traps would be a neat addition to your game? If someone dies from stepping on a trap he's going to be usr in DeathCheck() (because when he hit the arrow key to move usr was set to him and continued through the call stack). This would produce an nice exploit for quick leveling. Just having NPCs kill someone will throw the system off because usr will never be set to a non-player mob unless you set it yourself.

We've all gotten mind-boggled by an error using usr before and that's why you'll commonly see people telling others not to use it in procs. I really think it'd be better for you to switch your DeathCheck() to one formed like Garthors, if you need help doing so just ask and we'll help you through it. By the way, calling a DeathCheck() like his from a players attack verb would look something like this: M.DeathCheck(src)

For more usr information check out this article by Lummox JR.
In response to YMIHere
yea im really confused and i wouldn't know how to do it the other way if you would like to walk me through proabably inside my game or on msn would be the best place.

MSN [email protected]
game byond://75.73.42.169:5773
In response to YMIHere
YMIHere wrote:
Just having NPCs kill someone will throw the system off because usr will never be set to a non-player mob unless you set it yourself.

Actually, usr is given a value in mob/New(), so basic AI will work. Even more disastrous, however, is triggered AI: mobs that will only start walking around once you walk close enough to trigger them. Then, usr will be the person who first activated those mobs. THAT would be excellent fodder for exploits. You'd get credit for every kill those monsters made!