ID:285130
 
Hey! I've recently been dabbling around with BYOND code, using the reference as a tool to work with, and I've been somewhat curious about a certain something.

Is there any difference in functionality between a variable defined as 'test_variable' and 'src.test_variable', assuming the variable was defined for the source? I'm aware that if you define a mob as M, that you can access the variables for M though the same process (M.test_variable), so I'm really wondering if src.* is simply a redundant function due to the source being a mob with that variable, or if it actually does make a difference in some way. Would it make any difference if the source was a datom?

If my question didn't make much sense, let me know, I'll post a snippet of code to explain my question a little better.
As far as I'm aware, src is already implied. I never bother doing stuff like src.name or src.desc in my code since there's no real reason to type src out.

There is one caveat however:

mob/verb/rename(name as text)
name = name

mob/verb/blah()
world << name


The above will not work at all. You'd have to do...

mob/verb/rename(name as text)
src.name = name

mob/verb/blah()
world << name


So that the compiler specifically knows you mean src's name variable, and not the local variable name. Obviously you can just not use name as the variable name and do something else like _name or newname so you don't have to do the src.name thing.
http://www.byond.com/docs/guide/

Read Chapter 5. It's an easy, short read. Section 4 deals with what LordAndrew is talking about.
var test = "global"

obj
var test = "object-level"

proc/test()
var test = "proc-level"
src << "[test], [src.test], [global.test]"

if(1)
var test = "compile-time error"

tester
var test = "compile-time error"
Wow, fast responses! =D

Thanks guys, this actually helps a bit; I forgot to take into consideration that there could very well be multiple variables using the same name on different levels.

Thanks for pointing out the guide, Airjoe. Actually, it was one of the things I used as study material, I'm not sure why I forgot about it. xD; Certainly a good reminder though!

EDIT: Hey, just figure I'd share my findings a little further from the help you've given me. Wrote a little hybrid code from what Kaiochao was trying to demonstrate.

datom
var
test = "datom-level"
proc
test(test)
world << "[test], [src.test], [dat.test], [global.test]"
// Will return "proc-level, datom-level, datom-level, global"

var
test = "global"
datom/dat = new()

mob
var
test = "mob-level"
verb
mob_level()
world << "[test], [src.test], [dat.test], [global.test]"
// Will return "mob-level, mob-level, datom-level, global"
verb_level(test as text)
test = "verb-level"
world << "[test], [src.test], [dat.test], [global.test]"
// Will return "verb-level, mob-level, datom-level, global"
datom_level()
dat.test("proc-level")


Edited the code a little. As it turns out, LordAndrew was correct; src is implied unless a verb or a proc has an argument under the same variable name.

Thanks again for the help, guys. =3