ID:170967
 
Hey, I have a problem with procs and datums, yet again. if I have a proc, how would I say "The user who executed the proc" basically? I can't use src (it'll turn up /datum_name/), and I can't use usr, so what else is there?
proc/Something(mob/caller)
caller << "Text"



And call it like:

var/your_datum/D = new()
D.Something(src) //Src being the person that you want to call the proc for
Lenox wrote:
Hey, I have a problem with procs and datums, yet again. if I have a proc, how would I say "The user who executed the proc" basically? I can't use src (it'll turn up /datum_name/), and I can't use usr, so what else is there?

You have to tell the procedure yourself who executed it. For example:

proc/ExecuteMe(mob/User)
if(ismob(User))
User << "You executed me!"

obj
verb
Test()
set usr in world
ExecuteMe(usr)
You'll need to pass the value on as an argument.

An argument is in it's simplest form a variable inside the proc that you can set the value of when the proc is called.

Well start with a quick example of what a proc with an arguments definition looks like.
obj/proc/testProc(var/mob/M)


Now inside testProc() we'll have a var called M (of the mob type).

NOTE: It's important to remember that just because you make the var for a particular type doesn't mean only atoms of that type can be held in it.

Now we'll call testProc() and pass on a value to testProc()'s first argument, M.
//Inside a proc or verb on the players mob.
var/obj/O = new /obj (src.loc) //Create a new object.
O.testProc(src) //Call O's testProc() proc and pass on the value of src to testProc()'s first argument, M.


Now we have access to the player from within testProc(), in the form of a var named M.

Quick non-proof read example:
mob/verb/testVerb()
var/obj/O = new /obj (src.loc)
O.testProc(src)


obj/proc/testProc(var/mob/M)
M << "[src.name]: You smell like beans."



That should output "The obj: You smell like beans." to who ever calls testVerb().
In response to DarkView
Aiight, thanks for the help all ya'll :), I think I really understand this now. I've set up a Guild System, a Group system, and a battle system using datums now ^_^
In response to DarkView
One thing we all missed you should know about. You can have multiple arugments. You've probably encountered something similar with verbs, or even came to this conclusion yourself, but just in case here is an example of a proc with two arguments.



mob/verb/testVerb()
var/obj/O = new /obj (src.loc)
O.testProc(src, "beans")


obj/proc/testProc(var/mob/M, var/smell)
M << "[src.name]: You smell like [smell]."



The example should have the same effect, but you can now change "beans" when you call the proc to any text string you want.
Essentially you just put a , between each argument when you're calling the proc and when you're defining it.
In response to DarkView
Yeah, I've come across that before. Hrm, I need to find out how to call the proc from outside the datum, right now i'm just using this and using New() within the datum to start the proc..
mob
Player
Click()
new/battle_system_Player/
return ..()

So is there a way to call the proc that lies within the Datum from outside the Datum in my Click() proc so that I can actually pass src as the caller?
Lenox wrote:
Hey, I have a problem with procs and datums, yet again. if I have a proc, how would I say "The user who executed the proc" basically? I can't use src (it'll turn up /datum_name/), and I can't use usr, so what else is there?

The misconception here is that users don't execute procs; they execute verbs.

Lummox JR
In response to Lenox
Yep. All you need is a variable containing the datum.
So you'd replace new /battle_system_Player with var/battle_system_Player/battleDatum = new /battle_system_Player.

Now you need to understand something about the way a proc is called. When you write testProc() it's actually reading it as src.testProc(), which means call src's testProc().
It defaults to src, but you can set it to what ever you want.

We'll set it to 'battleDatum' like so:
mob/verb/testVerb()
var/battle_system_Player/battleDatum = new /battle_system_Player //Create a new battle_system_Player datum, and put it in the battleDatum var.
battleDatum.testProc(src)


Now one important thing to understand is the compiler will only let you do this if the variable is made to hold a type of datum/atom that has the proc or verb you're trying to access.
It's also worth noting that you'll get a run-time error if you put the wrong sort of value in the var (ie, a text string) and try to call a proc from it.

On a similar note you can access the battleDatum's vars the same way. Ie, battleDatum.name



[EDIT] Whoops. Forgot something major again. You're using client.Click(), correct? That's a client proc, which means that src will not be the players mob, it'll be the players client.
There's a lot to go into, and it doesn't make much sense until you understand it all, but basically if you want to access the players mob through Click() or any other client procs you'll need to use src.mob instead of src.
In response to DarkView
DarkView wrote:
Yep. All you need is a variable containing the datum.
So you'd replace new /battle_system_Player with var/battle_system_Player/battleDatum = new /battle_system_Player.

Now you need to understand something about the way a proc is called. When you write testProc() it's actually reading it as src.testProc(), which means call src's testProc().
It defaults to src, but you can set it to what ever you want.

We'll set it to 'battleDatum' like so:
mob/verb/testVerb()
> var/battle_system_Player/battleDatum = new /battle_system_Player //Create a new battle_system_Player datum, and put it in the battleDatum var.
> battleDatum.testProc(src)

Now one important thing to understand is the compiler will only let you do this if the variable is made to hold a type of datum/atom that has the proc or verb you're trying to access.
It's also worth noting that you'll get a run-time error if you put the wrong sort of value in the var (ie, a text string) and try to call a proc from it.

On a similar note you can access the battleDatum's vars the same way. Ie, battleDatum.name



[EDIT] Whoops. Forgot something major again. You're using client.Click(), correct? That's a client proc, which means that src will not be the players mob, it'll be the players client.
There's a lot to go into, and it doesn't make much sense until you understand it all, but basically if you want to access the players mob through Click() or any other client procs you'll need to use src.mob instead of src.


Does this answer your question?:
mob
Player
Click()


:)

It works, thanks all :P