ID:141887
 
Code:
mob
proc
Update_screen_ammo(mob/m)
var/Ammo=list()
var/aymo=num2text(src.ammo)
var/CX=-(lentext(aymo)*3)
for(var/a=1, a<lentext(aymo)+1, a++)
Ammo+=copytext(aymo,a,a+1)
for(var/X in Ammo)
world<<X
var/obj/onscreenammo/O=new/obj/onscreenammo()
O.screen_loc="23,23"
O.icon_state=X
O.pixel_x=CX
O.pixel_y=-20
CX+=10
m.client.screen+=O
obj
onscreenammo
icon='onscreenammo.dmi'

mob
verb
testit()
Update_screen_ammo(src)


Problem description:

Okay, I just copied and edited this code, because I'm so completely lost when it comes to editing icons with coding. I attempted to change it in to a lovely system where it checks how much ammo you have, then plops in on the client's screen in the top right corner. However I can't seem to figure out how to space the numbers out. Obviously I have no idea what I'm doing, and if there was a guide out there that went in to great definition of how to do this, I'd be happy to read it. Thanks!



I got it working with a work-around.

mob
proc
Update_screen_ammo(mob/m)
for(var/obj/onscreenammo/o in world)
if(o.owner==src)
del o
var/Ammo=list()
var/aymo=num2text(src.ammo)
var/CX=-(lentext(aymo)*3)
var/doublestop
for(var/a=1, a<lentext(aymo)+1, a++)
Ammo+=copytext(aymo,a,a+1)
for(var/X in Ammo)
if(lentext(Ammo)>1&&!doublestop) //got it working with this:
doublestop=1
continue
var/obj/onscreenammo/O=new/obj/onscreenammo()
O.icon_state=X
O.owner=m
O.screen_loc="23:[CX],23"
CX+=10
m.client.screen+=O
obj
onscreenammo
var/owner
icon='onscreenammo.dmi'


The pixel_x and pixel_y vars only affect the object's graphic when it's on the map, and doesn't effect it when it's on the screen.

To apply a pixel offset to a screen object, you do this in the screen_loc. The format is like this
screen_loc = "[x_coordinate]:[x_offset],[y_coordinate]:[y_offset]"


So, just put this in your code, like so.

mob
proc
Update_screen_ammo(mob/m)
var/Ammo=list()
var/aymo=num2text(src.ammo)
var/CX=-(lentext(aymo)*3)
for(var/a=1, a<lentext(aymo)+1, a++)
Ammo+=copytext(aymo,a,a+1)
for(var/X in Ammo)
world<<X
var/obj/onscreenammo/O=new/obj/onscreenammo()
O.icon_state=X
O.screen_loc="23:[CX],23:[-20]"
CX+=10
m.client.screen+=O


Personally, I would make it change the X coordinate of the screen_loc as well, since pixel offsets do have an upper bound (I think it's 192 pixels). Though, that might not be necessary in your case; I don't think you're going to have all of the bullets necessary to make use of all of the digits.
In response to D4RK3 54B3R
Excellent, that got it working just fine. I'm surprised that's all I was missing. I updated my code a bit more so that each time I called it, it would still work fine, like so:

mob
proc
Update_screen_ammo(mob/m)
for(var/obj/onscreenammo/o in world)
if(o.owner==src)
del o
var/Ammo=list()
var/aymo=num2text(src.ammo)
var/CX=-(lentext(aymo)*3)
for(var/a=1, a<lentext(aymo)+1, a++)
Ammo+=copytext(aymo,a,a+1)
for(var/X in Ammo)
var/obj/onscreenammo/O=new/obj/onscreenammo()
O.icon_state=X
O.owner=m
O.screen_loc="23:[CX],23"
CX+=10
m.client.screen+=O
obj
onscreenammo
var/owner
icon='onscreenammo.dmi'


However, all seems to work well until I get in to the double digits. The problem is like so:

Say I'm 'supposed' to have 20 ammo. Instead I have 220, meaning it's doubling the first digit for some reason.. I can't exactly see why though. :s
In response to Speedro
I tried a work around, but it didn't work so well.


mob
proc
Update_screen_ammo(mob/m)
var/skip
for(var/obj/onscreenammo/o in world)
if(o.owner==src)
del o
var/Ammo=list()
var/aymo=num2text(src.ammo)
var/CX=-(lentext(aymo)*3)
for(var/a=1, a<lentext(aymo)+1, a++)
if(lentext(aymo)>1&&!skip) //skipping the first digit if there's more than one digit...
skip=1
continue
Ammo+=copytext(aymo,a,a+1)
for(var/X in Ammo)
var/obj/onscreenammo/O=new/obj/onscreenammo()
O.icon_state=X
O.owner=m
O.screen_loc="23:[CX],23"
CX+=10
m.client.screen+=O