ID:954978
 
(See the best response by Kaiochao.)
Code:
mob
verb
Attack(mob/M as mob in oview(1))
var/damage = src.str - M.def
if(damage <= 0)
usr << "Your attack does not hurt [M] at all!"
M << "You are unhurt by [usr]'s attack."
else
M.hp -= damage
M:deathcheck()
M:levelup()

This is the original attack code I had where the user had to click on the actual attack button to attack.

mob
Click(mob/M as mob in oview(1))
var/damage = src.str - M.def
if(damage <= 0)
usr << "Your attack does not hurt [M] at all!"
M << "You are unhurt by [usr]'s attack."
else
M.hp -= damage
M:deathcheck()
M:levelup()


I edited it to make it so instead of creating a verb the user just has to click on the mob to attack. It compiled correctly but when I run it, it says this: runtime error: undefined variable /turf/var/def
proc name: Click (/mob/Click)
usr: Guest-570174474 (/mob)
src: the heartless (/mob/enemy/heartless)
call stack:
the heartless (/mob/enemy/heartless): Click(the turf (4,3,1) (/turf), "mapwindow.map", "icon-x=14;icon-y=18;left=1;scr...")

Any suggestions?
Add in if(ismob(M)) into Click() to ensure that it's a mob you're clicking and not a turf.
In response to NNAAAAHH
NNAAAAHH wrote:
Add in if(ismob(M)) into Click() to ensure that it's a mob you're clicking and not a turf.

While you are correct one would of assumed Click(mob/M as mob in oview(1)) would be doing the exact same job.
Parsing mob/M in oveiw()... etc inside of click seems a little odd to me, my preference would be:
mob
//just make attack parse mob/m instead in get_step().
proc
Attack(mob/M)
//attack stuff

Click(atom/a)//this means you can use the type that the atom is to invoke different
//behaviour with just one set of click code.
if(ismob(a)) // if a is a mob
if(a in oview(1, src)) // if a is within 1 tile of src
src.Attack(a)

This way you can run checks for turfs and objs as well, say maybe for doors or chests, but it is entirely up to you.
Best response
The problem here is that none of you are using proper atom.Click syntax. In atom.Click, src is the object being clicked and usr is the mob belonging to the player who clicked src.
mob/Click()
if(get_dist(src, usr) <= 1)
src << "[usr] hits you!"
usr << "You hit [src]!"

And, of course, get_dist is much more efficient than oview(1) and is practical in this situation.
In response to Kaiochao
Ok here is what I have.

mob
Click()
var/damage = usr.str - src.def
if(get_dist(src, usr) <= 8)
src << "[usr] hits you for [damage] damage!"
usr << "You hit [src] for [damage] damage!"
else
src.hp -= damage
src:deathcheck()
src:levelup()

The clicking works and it displays the message correctly for the amount of damage done. But for some reason now it seems like damage isn't actually being done because no matter how many times I hit the mob, he won't die. (Before I added in click when I had to click the attack verb to attack the mob would die correctly).

Here is my other sets of code for levelup and deathcheck in case there is something wrong with them:
mob
proc
levelup()
if(usr.nexp <= usr.exp)
usr.level += 1
usr.hp += 40
usr.maxhp += 40
usr.str += rand(1,5)
usr.def += rand(1,3)
usr.nexp += 12 * usr.level
usr.exp = 0
view() << "[usr] has grown to level [usr.level]!"
usr << "You have grown stronger!"
usr << "Your hp has increased to [usr.maxhp]."
usr << "Your strength has increased to [usr.str]."
usr << "Your defense has increased to [usr.def]."


mob
proc
deathcheck()
if(src.hp <= 0)
src.hp = 60
usr.exp += 2
var/obj/item/heal = new/obj/item/heal/(loc=src.loc)
view() << "[src] was killed by [usr]!"
for(var/a in src.loot)
if(prob(loot[a]))
var/obj/O = new a
O.loc = src.loc
loc = null
sleep(50)
src.Move(locate(15,15,1))

Change the colon to a . I already said you shouldn't use a colon.
In response to Shocker_92
Look at your if() statement: if they're within 8 tiles you display the damage message, else you subtract the damage from their health.
In response to DarkCampainger
Dunno how I missed that >.<.
Thanks for the help guys. Got it working :)
Found an error where if the player clicks themself then they can damage and kill themself. Would I have to add in an if() to fix that?
In response to Shocker_92
Yes. If you want code to run under certain conditions (am I clicking myself?), you use a conditional. Otherwise, do nothing.