ID:132971
 
Var_Accessed(var_name,atom/Src) // Src is the owner of the var, if available (would be null for global vars and stuff)
world<<"[var name] accessed!"
switch(var_name)
if("h_pee")
if(ismob(Src))Src:UpdateHP()

mob/var/h_pee=1337
mob/verb/ChangeHP(a as num)
usr.h_pee=a // the var has been accessed! Var_Accessed() would be called.
mob/proc/UpdateHP()
winset(w.e)


Gets called whenever a var is accessed. This would simplify certain things so much. Instead of tracking every single var change by ourselves, just override that prawk.

(Turn vars into procs ftw? Is it possible?)
I suppose you could use a datum to handle your variables. Just have a "set" proc to set the value to something, or an "add" proc to add/subtract a number from it(if it's a number).
I'm not sure what you're asking, but if it's tracking what user has what variables.

mob
verb
UserVariables()
for(var/A in src.vars)
src << "[A] = [src.vars[A]]"


That can be adapted for all atoms, as I'm fairly sure every atom has the vars list.
In response to Tiberath
I think he's asking for a proc to be called when a variable's value is changed.
In response to Kaiochao
Kaiochao wrote:
I think he's asking for a proc to be called when a variable's value is changed.

That's easy enough to achieve without the use of datums.

mob
proc
update_variable(variable, value)
if(variable in src.vars)
src.vars[variable] = value
src << "Your [variable] has been changed to [value]!"
// Perform additional code here.
else return FALSE

mob
var
a = 0

verb
test()
var/name = input("Please select your name", "Name") as null|text
if(name)
src.update_variable("name", name)
world << src.name

test2()
// Same as src.a += 36
src.update_variable("a", src.a + 36)


Of course, it gets tedious if you want to perform different actions per variable, but still doable. It just means you can't set the variable as you normally would. (if you want it to be for all atoms, just change the mob/proc to atom/proc and away you go.)
In response to Tiberath
That's not exactly what I meant :/

Whenever a var is accessed the proc is called automatically with the info. Of course we can do this manually, but we could just override the Var_Accessed() proc, and not worry about calling the specific proc for every single var we change. I have trouble expressing myself sorry D:
seconded. maybe a proc for datums? something like _var_changed(variable, oldvalue, initialvalue)?

helps great in converting old projects!
This sounds like the equivalent of properties in some other languages. A syntax similar to Python's would be better (although probably harder to implement) than having one global proc that is called every time any variable is accessed.
I don't think this would be too hard to implement...
Just have a proc call whenever any of the assignment operators (= += -= *= /= &= |= ^= <<= >>=) are used during run time.
In response to Naokohiro
Naokohiro wrote:
I don't think this would be too hard to implement...
Just have a proc call whenever any of the assignment operators (= += -= *= /= &= |= ^= <<= >>=) are used during run time.

This would be even easier to just program yourself, taking an OOP approach.

mob/MyPlayer
proc/LoseHP(N)
HP-=N
DeathCheck()


Then at any time while you're programming if they lose health just use LoseHP(Damage) instead of HP-=Damage.

It is a very good practice to have anyway while programming. If you ever work in a language like Java most of the tutorials you read encourage keeping variables private (only able to be modified from inside the module you're working on) and changing them externally through public functions (procs).

It isn't that I don't think this is a good idea but honestly you can do it yourself, and it isn't hard.
In response to AJX
That's not an object-oriented approach. That's a functional approach. An object-oriented approach would be creating your own class (i.e., a datum) to handle the variable information.
In response to Popisfizzy
Popisfizzy wrote:
That's not an object-oriented approach. That's a functional approach. An object-oriented approach would be creating your own class (i.e., a datum) to handle the variable information.

Duly noted. But the point still stands. :)
In response to AJX
I know that. It's quite obvious how you can program it to do this, but it's just not pretty, nor will it work for existing projects that haven't already used this method throughout the code.
In response to Naokohiro
Naokohiro wrote:
I know that. It's quite obvious how you can program it to do this, but it's just not pretty, nor will it work for existing projects that haven't already used this method throughout the code.

To be honest I don't think it would be difficult in the slightest to apply this sort of system to an existing project. A simple find/replace of HP+= / HP-= to HPUp( / HPDown( and throwing a ) on after each one would only take a few seconds in a small project, and minutes in a large project.

And I also don't believe it isn't pretty either. As a personal preference I MUCH prefer using a single proc for handling damaging a player which automatically calls deathcheck() than doing HP-=5 , DeathCheck() in every code where someone could take damage.


I just personally feel that the developer's time is better spent on things that can't be easily managed by ourselves. Sure, convenience is nice but not in such a simple situation and not at the cost of their valuable time.
In response to AJX
I guess what I'm trying to say is that I'm not looking for a workaround. Unless this is the real thing, then I don't care about it, nor will I consider how I could use var tracking anyways. See ya.