ID:1290518
 
(See the best response by Jp.)
Code:
proc
ChatColor(var/StringValue)
switch(StringValue)
if(src.Faction == "Earth")
ChatColor = "green"


Problem description: src.Faction: undefined va

May it be because I am using a mob variable in a global proc or because I am using src in a global proc ?

Thanks in advance.
mob/proc/ChatColor()
switch(Faction)
if("Earth")
ChatColor = "Green"
In response to Kozuma3
I just realized my code was really wrong and that's a terrible mistake, thanks for correcting it Kozuma3.

That way or the one I am doing now, works prefectly but

Picture it this way:

proc
ChatColor()
if(src.Faction == "Earth")
ChatColor = "green"


That works, what is really bugging me is why it doesn't let me use src in a proc ? It's because my proc is global ? This is my question. I could use usr, but I don't want to since in DM reference it says it's not good code pratice.
In response to BloodyJr
src refers to the container that the procedure belongs to. A global procedure does not have a container, so src is never valid. Two ways to get around this issue is to either give the procedure a parameter that holds a reference to a mob, or make the procedure a mob method:

mob
var Faction = "Fire"
var chatColor

proc/ChatColor(mob/m)
if (m.Faction == "Fire")
m.chatColor = "red"

// or...

mob
var Faction = "Water"
var chatColor

proc/ChatColor()
if (src.Faction == "Water")
src.chatColor = "blue"
In response to LordAndrew
Thanks Andrew. That was a good answer and example. I decided to use a reference to a mob.

This is what I am doing.

//Procs.dm

proc
ChatColor(mob/Active/A)
if(A.Faction == "Earth")
ChatColor = "green"
if(A.Faction == "Hueco Mundo")
ChatColor = "grey"

// Verbs.dm

var/tmp
ChatColor = "white" // Since I don't want this to be save. I hope I am doing this correct too.

mob/verb
OOC(T as text)
ChatColor(usr) // I using usr, may you tell me if this is correct ?
if(!T) // If it has no value
return 0
if(IsWorldChatDisabled())
world << output("Sorry, World Chat is disabled.", "ChatOutput")
return 0
if(T == uppertext(T)) // Prevent Caps
T = lowertext(T)
if(!usr.Faction) // If it has no value
usr << output("<b>[usr]: [T]</b>", "ChatOutput")
else
usr << output("<b><font color=[ChatColor]>[usr.Faction]</font> - [usr]: [T]</b>", "ChatOutput")
In response to BloodyJr
Yes, ChatColor(usr) is fine, because usr refers to the one calling the verb. ChatColor(src) would also work in this case, because the player also contains the verb.

As a side note, you probably shouldn't be outputting the "Sorry, World Chat is disabled." message to the world since people could spam that. ;)
Ugh, you are right. Didn't notice that mistake, I'll just change it to the usr only.

Also, I mostly use usr to verbs and src to procedures, since that's how I learned in DM reference or that's how I undestood.
To be honest, I didn't know you could use src in a verb. That will just confuse me since now I know I can use both but what I'll be asking to me is, "Shall I use usr or src in this verb ?"

Thanks for all this information Andrew.
Best response
src is defined in all procedures, and verbs are a special kind of procedure.

Just try to keep in mind what src and usr mean. 'src' is the thing the procedure/verb is attached to. 'usr' is the player activating the verb. So in this example:

mob/verb/say(t as text)
hearers(usr) << "[usr] says [t]"


usr is what you want because you want to use the name of the player that ran the verb. But in this example:

obj/torch
verb/extinguish()
set src in view(1)
viewers(src) << "[usr] extinguished [src]!"
luminosity = 0


sometimes we use 'src' - for the torch - and sometimes we use 'usr', for the player extinguishing the torch.
Very good explanation Jp. With some trial and error, I'll get there fast! Now I have a good idea of what usr and src mean.
For further reading, LummoxJr wrote up an article.

http://www.byond.com/forum/?post=35932