ID:164947
 
I still can't figure out an easy way to add objects to my hud. I want it to only show up once equipped. And of course to disappear once un-equipped. I've got a random code of like I thought up but of course it doesn't work.

I got this...
client+=new('Guns.dmi',"Colt 45",14,2)

and
client-=del('Guns.dmi',"Colt 45",14,2)


It doesn't work and I don't blame it, I made it up. Is there any easy way like so to add objects to my hud?
I'd assign particular HUD objects as such. Then you would be able to locate an object of that particular type and remove it.
/obj/awesomehud/SayButton
//variables and such here

For adding:
var/obj/awesomehud/SayButton/C = new
client.screen += C

For getting rid of:
var/obj/awesomehud/SayButton/C = locate(/obj/awesomehud/SayButton) in client.screen
client.screen -= C


That's the simplest way I could think of to do this.
You need to use client.screen instead of just client. Also, the objects truly are objects, so you'd have to do something like client.screen+=new/obj/hud/something.
In response to CaptFalcon33035
Yeah, though you'd better have global screen objects (at least for your common always-on HUD) rather than creating an object for every single player. That way you don't only save objects, but you don't even need to call locate() to remove those screen objects later.

<small>Also, your code example isn't the simplest, you don't need to state the path twice in the locate() line and you don't even need the temp var to hold the reference first</small>
In response to CaptFalcon33035
Thanks a lot. Just what I wanted.
In response to Kaioken
Kaioken wrote:
Yeah, though you'd better have global screen objects (at least for your common always-on HUD) rather than creating an object for every single player. That way you don't only save objects, but you don't even need to call locate() to remove those screen objects later.

You do still need to call locate(), I'm pretty sure.

Also, your code example isn't the simplest, you don't need to state the path twice in the locate() line and you don't even need the temp var to hold the reference first

I believe it makes it more efficient to place it in between the parentheses in locate() so it knows exactly what to check for. However, I'm not sure about that.

The temporary variable is just so he knows what it's doing. Besides, he may want to do more with the object than just remove it.
In response to CaptFalcon33035
CaptFalcon33035 wrote:
Kaioken wrote:
Yeah, though you'd better have global screen objects
You do still need to call locate(), I'm pretty sure.

Eh, of course not. Did you think about it before being sure?
example-
obj/HUD
icon = 'x'
layer = 10

var/obj/HUD/some_hud

//adding
if(!some_hud) some_hud=new
player.client.screen += some_hud

//removing
player.client.screen -= some_hud
//check if object is no longer needed, kind of optional tho
for(var/client/C)
if(some_hud in C.screen) return
some_hud=null



I believe it makes it more efficient to place it in between the parentheses in locate() so it knows exactly what to check for.

I believe it does just that ("places it between the parenthesis") when you compile. I think typecast statuses aren't compiled, I don't think there's any reason for them to be, anyway.
In response to Kaioken
obj
hud
New(client/C)
if(C)C.screen += src
else del src // so it gets garbage collected
obj/hud/proc
Delete()
for(var/client/C)
if(istype(src,/obj/hud/text)) continue
// dont delete that
C.screen -= src

In response to Xx Dark Wizard xX
In short, again, that method is flawed because you'll be creating a screen object for each player that uses that HUD, while you can just create a single object and re-use it for all players.
In response to Kaioken
I knew you would be able to do that, Kaioken, but do you believe that anyone wants to have a bunch of variables recording their HUD objects? I don't think they would. I wouldn't. Who's to say this isn't the only object he wants removed?