ID:153414
 
Though generally it's presented as always being safe in verbs and generally it is. Yesterday I went back to my CTF2 source and finally fixed a long standing bug in my source code which was a problem dealing with misuse of usr. I had used usr in my object's drop verb which would have been safe had I not called it from my mob.Killed() proc. To see why this doesn't work here's the call stack.

client.Center()   <-  If it's a player attacking usr is set here to the client's mob
mob.UseWeapon() <- This step is the first in the case of an AI attacking in which case usr isn't anything
obj/getable/equipment/Weapon.UseWeapon()
atom.Dagage()
atom.Killed()
mob.dropall()
mob.drop() <- This is a verb but at this point usr isn't what I wanted!


So if the attacker was a played the attacked would have dropped all their items rather than the mob attacked if the attack was a player otherwise nothing would happen(I made some null checks so if usr was null nothing happened rather than a run-time error).

Though apparently this was just an oversight of the problem at the time I programmed CTF2 since I properly handled this in other places ie:

obj/verb/Get()
set src in view(0)
GetObj(usr)
obj/proc/GetObj(mob/who)
src.loc = who


So when an AI needs to pick up an object I call the GetObj proc directly and when a player uses the Get() verb it's just a wrapper for the GetObj() proc.

The point here is you should never have an AI character call a verb unless you don't use usr in the verb so:

mob/verb/say(msg as text)
world << "[src]: [msg]" //This is safe to call from an AI mob or maybe
//another player forcing speech on a character

mob/verb/say(msg as text)
world << "[usr]: [msg]" //This isn't safe to call from an AI mob as usr wouldn't be valid.


The point of this post being if you're calling verbs from procs you should be careful or just not use usr in those verbs. This is particularly important for handling AI since much of the functionality you may want them to have already exsist in player verbs.
And this is why you don't call verbs directly. If you want to have an AI-accessible verb, move the verb into a proc, and make the verb call that proc.
In response to Garthor
I also tried to point out that when you're calling a verb, it's no different than if you're calling a proc. "usr" is safe to use in verbs. It's not safe to use in procs which are also verbs.