ID:2113018
 
Code:
/mob/New()
new /obj/secrets(src) // Creates /obj/secrets within mob contents

/obj/secrets // Container of /datum/secret
var/list/datum/secret/secrets = list()

/obj/secrets/New()
secrets += new/datum/secret(src) // adding a datum to the secrets list

/datum/secret
var/secrettext = "This is a secret."

/datum/secret/New(obj/secrets/S)
S.verbs += /datum/secret/proc/Hear // This adds a verb to the obj secrets

/datum/secret/proc/Hear()
usr << secrettext
usr << "Ssshhhhhh!"
usr << src.type


Problem description: So this is my problem. This is a sample code that reproduces the problem I'm having, my actual code is a different project. When a proc is added to the verbs var of an object, it loses its source. The soruce becomes the object the verb was added to, and I've found no way to get around this problem or easily get the source object inside of the proc code.

The output of this is an error, secrettext is undefined in the context of the Hear() proc. This is because when it is added to the verbs list it's source is changed to that of /obj/secrets. If the first line is commented out, the output will be:
Ssshhhhhh!
/obj/secrets


I would like to create a very flexible data structure where datums can give verbs to an object freely whilst still having access to the variables of the datum. This is so I could create very modular mechanics, where I don't need to code a lot to create new features.

The soruce becomes the object the verb was added to, and I've found no way to get around this problem or easily get the source object inside of the proc code.
This is intentional. What you think is the source object isn't actually an object that exists.
The 'src' variable is always going to be the container of the verb, usr being what called the verb. If you want to retain access to the datum you'd need to store a reference to it in the container somewhere and use that.
I solved it by creating /obj/proc_button that contained a reference to the proc that should be called on Click, and using statpanels to display the buttons.