ID:162065
 
When programming a game, which is better, using [src] or [usr] for Stats, Inventory and whatnot. Do they mean the same thing, is one better then the other when you allow a player to have more then character, somebody inform me please.
It really depends on the situation, although most cases src (the source of the proc/verb) is the most acceptable:

This will help you out:
http://www.byond.com/members/ DreamMakers?command=view_post&post=35932
If your using it in a verb or mob/Stat you dont need either if your referring to the person using it, you could just put:
mob/verb/Train()
GainStats()
mob/proc/GainStats()
Power+=1
MaxHP+=1
HP+=1
mob/Stat()
stat("Health","[HP]")

You dont need src or usr for those such of things, it'd be a waste of typing. And src and usr arent the same thing anyway, they each have their own places.

If you dont put src. the compiler automatically assumes its src., so theres really no point using it at all in most cases.

You can code a whole game without using even 1 src. in your code. In fact my game is standing proof of that, Dragonball Finale doesnt have even 1 src. anywhere in its code. I cant think of one situation where its necessary to use src., "src" on the other hand is a different story.
usr refers to the object that started the call stack.
For example:

proc/force_verb(mob/M)
M.verby()

mob/verb/verby()
world << "[usr] executed verby for [src]"


If you called force_verb() on another mob, "usr" will be the person who called force_verb, not the mob who actually owns the verby verb (IE, not M).

src refers to the object that owns the verb.
Like in the above example: src was the mob, while usr was the caller.
Which is better, + or -?
In response to Dragonn
How would you do this without src?
if(M == src)

Jamora1124 wrote:
When programming a game, which is better, using [src] or [usr] for Stats, Inventory and whatnot. Do they mean the same thing, is one better then the other when you allow a player to have more then character, somebody inform me please.

src and usr are definitely not the same thing. The distinction between them is a little confusing for new users, but it basically boils down to this:

  • src is the object that owns the current proc/verb.
  • usr is the mob belonging to the player who initiated the current verb.

    Because usr is really meant for verbs, it's fairly meaningless outside of them and not good to rely on in those cases. However there are a few procs where usr is okay, like Stat(), because it's pretty much only ever called by client/Stat(). Inside of Stat(), usr is the player looking at the statpanel, whereas src is the mob whose stats are actually being looked at. If you were to change client.statobj, you'd see a different mob's stats. So that said, inside of Stat() you probably want to stick with src for most stuff anyway.

    The other places usr is safe outside of an official verb are some of the pseudo-verbs like atom/Click(). Unless you go mucking with the client/Click() verb or calling atom/Click() directly, it's safe to treat atom/Click() like a regular verb.

    Lummox JR
In response to Dragonn
Dragonn wrote:
If you dont put src. the compiler automatically assumes its src., so theres really no point using it at all in most cases.

You can code a whole game without using even 1 src. in your code. [...] bla bla

Right, src. is pointless and shouldn't be used because if you don't write it the compiler uses it anyway, (when possible). :P
Seriously though, you should tend to include the full syntax because it makes the code easier to understand, and not only in terms of simplicity - the shortcut is more for advanced people to use, since it's more vague (I prefer to write it out fully myself). The full syntax distinguishes the variable from a local or global one, and of course that it belongs to the source object, which all make it easier to newbies to get acquainted with the language. And of course you have to write it fully if there's a global or local variable with the same name as one of src's vars, which can surprisingly be considerably common in certain procs.

To expand on what others have said, 'src' and 'usr' aren't always the same, but when they are, it's more useful to use 'src'.
In response to Traztx
Oops, Dragonn I didn't realize you said without using "src." but I took the "." as the end of your sentence. I see now that you mean and agree.

I'm behind a proxy so I can't test this out, but I wonder what the following does:

var
something = "a"

mob
var
something = "b"
verb
seesomething()
var something = "c"
usr << "something==[something], src.something==[src.something]"


My guess is that output is "something==c, src.something=b" because the local something overrides the object's something as well as the global something, but the explicit reference to src clarifies it.

And of course, there isn't a vital reason to name variables like that, so I still agree "src." is unnecessary.
For the sanity of all newbies, src is better. People should leave usr alone unless a player is accessing verbs or procs belonging to another object, such as obj/verb/Get() or obj/Click(), in which case src is the object and usr is the player.

Other uses for usr exist, but they are few and far between and should really only be done by people who know what they're doing!
In response to Foomer
Foomer wrote:
For the sanity of all newbies, src is better. People should leave usr alone unless a player is accessing verbs or procs belonging to another object, such as obj/verb/Get() or obj/Click(), in which case src is the object and usr is the player.

Other uses for usr exist, but they are few and far between and should really only be done by people who know what they're doing!



I don't advocate giving this kind of simplified advice that has just lead to people making the same mistakes over and over again. People should know the fully story behind src and usr.

This kind of reminds me about that fiery argument between the usage of . and : that in the end showed people were horribly misguided about their merits.