ID:1705071
 
(See the best response by Reformist.)
Code:
//Well this was created by me(Zanoroku) seen it's working quite fine,so dont change this.
mob/verb/Attack()
set hidden=1
if(!usr.canattack)
return
if(usr.wieldingsword) flick("Sword Slash1",usr)
else flick("punch",usr)
for(var/mob/M in get_step(usr,usr.dir))
var/damage=round(M.Strength-M.Defence)
var/counterdamage=round(damage)
if(damage>=0)
M.Health-=counterdamage
M.effectdamage(counterdamage,"y")
if(M.enemy)
usr.hollowprotection=1
view(usr,8)<<"[usr] attacked [M] for [damage] damage."
usr.canattack=1
return
usr.Death(M)


Problem description:
How to put a delay on attack coding
Best response
I see quite a few things wrong with this code.

First off, these two lines:
var/damage=round(M.Strength-M.Defence)
var/counterdamage=round(damage)

Do the exact same thing. counterdamage tries to round a rounded value, so effectively is just a waste of processing.

Second, "return" should be "break". On top of that, "usr.Death(M)" should be called before break.

Third, the view() should be changed to a for(in range()) loop ideally, but that's just me nitpicking.

To add a delay to attacking simply put spawn(attackdelay) outside of the original for() loop and set canattack to 1. You'd also want to set canattack to 0 somewhere.

For example:

mob/verb/Attack()
set hidden = 1
if(src.canattack)
if(usr.wieldingsword)
flick("Sword Slash1", src)
else
flick("punch", src)
for(var/mob/M in get_step(src, src.dir))
var/damage = round(M.Strength-M.Defence)
if(damage > 0)
M.Health -= damage
M.effectdamage(damage, "y")
if(M.enemy) // What?
usr.hollowprotection = 1
for(var/mob/m in range(8, src)) // Ideally you'll want to make this check only player mob types
m << "[src] attacked [M] for [damage] damage."
src.canattack = 0
M.Death(src) // Death should be handled from the person dying, not the person doing the killing
break // stop the for() loop
spawn(20) // wait 2 seconds
src.canattack = 1// can attack again


Also put some side notes in there about several other things.
I like using world.time to determine when a player can perform another action...Also your damage why are you doing M.Strength-M.Defence shouldn't it be src.Strength-M.Defence?

mob
var/canAttack = null;

mob/verb/Attack()
set hidden = 1

if(world.time >= src.canattack)

if(usr.wieldingsword)
flick("Sword Slash1", src)
else
flick("punch", src)

for(var/mob/M in get_step(src, src.dir))
var/damage = round(src.Strength-M.Defence)

if(damage > 0)
M.Health -= damage
M.effectdamage(damage, "y")

if(M.enemy) // What?
usr.hollowprotection = 1

for(var/mob/m in range(8, src)) // Ideally you'll want to make this check only player mob types
m << "[src] attacked [M] for [damage] damage."

M.Death(src) // Death should be handled from the person dying, not the person doing the killing

break // stop the for() loop

src.canAttack = (world.time + 20);


also for a verb wouldn't calling usr be more appropriate?
He put M.Strength-M.Defence because that's what you originally had. Also no, don't call usr. You only use usr if you're calling a verb from an external src. In this case the usr and the src are the same.
In response to Jaysburn
Jaysburn wrote:
He put M.Strength-M.Defence because that's what you originally had. Also no, don't call usr. You only use usr if you're calling a verb from an external src. In this case the usr and the src are the same.

1. I know thats why the 2nd posted put that i meant the OP.
2. I think you have it backwards. (read the DM reference you can find plenty of examples of how usr and src are used.)
1. Oh hah I thought you were the OP. Whoops
2. Granted it's been a while since I've done any programming but I don't think I'm wrong. The mob performing the verb is the mob containing the verb, both the usr and the src. No point calling usr when src is automatically called.
It's not that important, but if there were anything in his system that externally causes the verb to be run problems could arise. If mob A were to somehow force mob B to use its attack verb, mob A is the usr with mob B being the src. So if he uses usr in the Attack() verb, problems could arise from that.


Edit

The difference is that you're not calling an external verb. It's not like a mob is trying to sit in a chair using the chairs Sit() verb, in which case the mob is the usr and the chair is the src.
In response to Jaysburn
Jaysburn wrote:
1. Oh hah I thought you were the OP. Whoops
2. Granted it's been a while since I've done any programming but I don't think I'm wrong. The mob performing the verb is the mob containing the verb, both the usr and the src. No point calling usr when src is automatically called.

Edit

The difference is that you're not calling an external verb. It's not like a mob is trying to sit in a chair using the chairs Sit() verb, in which case the mob is the usr and the chair is the src.

Hmm I didn't know that that is interesting must test.
In response to Gokussj99
sleep() and spawn() both were changed to run on real time, if I recall, so that's kind of redundant.
Btw the coding that was created by me was quite fine it does not have bug or any thing only thing i just wanted to know was to added the the delay. By the way thanks for the help and i'm still a novice at programming
Just because you don't have any errors doesn't mean you don't have any problems.
Only saying that im a novice programmer geez
I'm not trying to be rude or mean to you, I'm just stating the fact that just because a piece of code doesn't present any errors doesn't mean you won't have any problems with it, especially later down the line. It can help to try and think of what situations a verb/proc may come up in your game, and making sure it will be compatible with those situations.
In response to Reformist
Reformist wrote:
sleep() and spawn() both were changed to run on real time, if I recall, so that's kind of redundant.

not really redundant it gets rid of 2 annoying things

sleep = holding up a block of code
spawn = executing a block of code after x amount of time ( can lead to missing references useless checks for references such as mobs or obj's that just aren't needed if you did my simpler approach ).

for example that spawn if the mobile doesn't exist when that spawn block is called then it will throw errors.