//mob/var/tmp/GUIstamina
proc/UpdateStamina(mob/player,value)
spawn()
//initialise with a variance of 2 so the code below animates to right value from next closest value
if(!player.GUIstamina) player.GUIstamina = round(((player.stamina.value) / player.stamina.max_value) * 100)-2
var/_stamina = player.stamina.value
//changes the raw value of the stat
player.stamina.Set_CurrentValue(_stamina + value)
var
//value of stamina as a % of maximum
_percent = round(((player.stamina.value) / player.stamina.max_value) * 100)
//the value we are changing to, using stam because its a var defined outside of this proc
_target = player.stamina.value
//the difference between _percent and the % value currently displayed
_difference = round(((player.stamina.value) / player.stamina.max_value) * 100) - player.GUIstamina
//this is the integer used to modify the display value below
n = 1
//sets 'n' according to the direction we're going (higher or lower)
if(_difference > 0) n = 1
if(_difference < 0) n = -1
//if the stamina isn't different, don't animate
if(_difference == 0) return
if(player.client && player.GUIstamina!=_percent)
var/statHUD/s = locate() in player.client.screen
if(s)
//animation loops once per % difference between display and intended value
for(var/i = abs(_difference), i>0, i--)
//change the HUD by 1 in whichever direction requried
player.GUIstamina += n
s._HUDUpdate(player.GUIstamina)
//stop the loop if the HUD display == the stamina % or if the player's stamina value has changed since animating
//if the stamina valua HAS changed, this proc will be called all over again.
if(player.GUIstamina == _percent || _target != player.stamina.value)
break
//0.1 is slower than 0, but way faster than 1; cannot work out why. Reference is silent on it.
var/r = rand(0,1)/10
sleep(r)
Problem description:
Extra notes on the code:
* What its doing each loop is changing the icon_state value of the HUD icon. Between 1-100 for the stamina, which is a sphere expanding and contracting. And between 0-300 for magic, which is a horizontal bar, 300 pixels wide.
* I'm not using animate because the HUD icons are high quality icons made in photoshop, even photoshop-level content-aware re-scaling would distort them beyond recognition. So AFAIK animate() is not an option, until Lummox does an animate() proc for changing numerics / or has that intuitively put into animate(icon_state).
The above code works great! But I'm a little concerned it could be resource-greedy, esp when applied to 3 HUD bars, per player.
There's also the concern that I've done it the hard way, or that there's a better way, or that there's a gaping potential calamity with this method?
Technically being able to reliably 'animate()' the icon_state var (which is an integer) in a more efficient way would be a significant upgrade)
Any input or feedback greatly appreciated.
* Use spawn when you don't want to wait for the proc to finish before moving on
* Or use "set waitfor = FALSE" in the proc.
This proc should really be called AddStamina() or ModStamina() since it adds a value to the current stamina. Or, you could split this proc into an AddStamina(), which changes the stamina and calls UpdateStamina(), which updates the HUD.
It's not clear why you're including that -2 in the GUIstamina calculation.
Your _difference var duplicates your _percent calculation.
You should have a object-level variable to contain the HUD object instead of looking for it every time.
Early exit conditions should happen sooner.
The most common case might be when _difference > 0. It comes first, so the other two checks don't even happen.
Here's how you could've used animate():