ID:1373647
 
(See the best response by Kaiochao.)
Code:
mob/proc/GoldHUD()
var/t = "[usr.gold]"
var/pixX = 0
for(var/i = 1, i <= length(t), i++)
var/obj/number= new()
var/a = copytext(t,i,i+1)
numb.icon = 'numbers.dmi'
numb.icon_state = "[a]"
numb.screen_loc = "15:[pixX],2"
usr.client.screen += number
pixX +=32</b>


Problem description:I have tried numerous things with this code, but keep running into the same problem. The procedure updates the user's gold just fine on the HUD, but does not delete the numbers that were added at the last call of GoldHUD().

For example, if a user has 1000 gold, the HUD will display 1000, but if the user later spends 100 gold, the HUD will display 9000 instead of 900 because the old numbers are still displaying.

Any help would be greatly appreciated :)

Best response
You need to store your 'number' objects in a list that you can remove from client.screen when you update.
mob
var gold_HUD_objects[]

proc/GoldHUD()
client.screen -= gold_HUD_objects
gold_HUD_objects = new
...
for(...
var obj/number = new
gold_HUD_objects += number
...
...

I suggest using maptext instead of an object per-char, but you might have special graphical text that you really want to use.
That makes a lot of sense! I just couldn't figure out how I could save the numbers in such a way that I could delete them later. I was trying to do it individually, which is just inefficient and not very possible. :P Let me try this out and I'll let you know if it works.
Hmm...I've implemented your suggestion and am now getting a runtime error (below). Here's the updated procedure:

mob/var
gold_HUD_objects[]

mob/proc/GoldHUD()
usr.client.screen-=gold_HUD_objects
var/t = "[usr.gold]"
var/pixX = 0
for(var/i = 1, i <= length(t), i++)
var/obj/number = new()
var/a = copytext(t,i,i+1)
usr.gold_HUD_objects+=number
number.icon = 'numbers.dmi'
number.icon_state = "[a]"
number.screen_loc = "15:[pixX],2"
usr.client.screen += number
pixX +=32


runtime error: type mismatch: the obj (/obj) += the obj (/obj)
proc name: GoldHUD (/mob/proc/GoldHUD)
usr: Ralf1324 (/mob)
src: Ralf1324 (/mob)
call stack:
Ralf1324 (/mob): GoldHUD()
Ralf1324 (/mob): Login()
In response to Ralf1324
You forgot to initialize the list! At first, gold_HUD_objects is equal to null. The [] just means it's defined as a list, like how "number" is defined as an /obj.

Basically, you missed this line before the for() loop:
gold_HUD_objects = new
I've always initialized lists in the var itself, for example:
mob/var/list
bankeditems = list()


Woops! Guess that's not the right way to do it :P

It works perfectly now! Thanks for your quick responses and your help! I really appreciate it :)
In response to Ralf1324
These lines are equivalent:
var items[0]
var items[] = new
var items[] = list()
var list/items = new
var list/items = list()

var items[]
items = new
items = list()

var list/items
items = new
items = list()

There's a lot of ways to declare and initialize lists, but they're the same as regular variables in that you can declare and initialize separately.
var n = 0

// vs
var n
n = 0
In response to Kaiochao
That makes sense. Thanks for taking the time to explain. =]