ID:2429567
 
mob/var/tmp/list/hud = new
// A global list named hud, it can be used anywhere within the code.

mob/Login()
// Called when your mob "logs in"
LoadHUD()
// Calls the Function/Proc LoadHUD(), which is below.
// Notice it's under /mob, since this is a /mob proc aswell
// you can choose to use src.LoadHUD() or just LoadHUD()


mob/proc/LoadHUD()
..() // Spooky Magic
hud["A"] = new/obj/HUD/Something1
hud["B"] = new/obj/HUD/Something2
// Creates 2 objects and stores them in a list each with their own index value.
// It's called an associative list, I think.
client.screen.Add(hud["A"],hud["B"])
// Adds both references to the client's screen from mob.hud
// The point of this is to load what's needed once as done here
// But you also have access to each piece of HUD as needed by ID within mob.hud

sleep(30)
// Lets wait 3 seconds..

client.screen.Remove(hud["A"],hud["B"])
// Removes both references [A] & [B] from the client's screen


obj/HUD
//icon = '.dmi'
//icon_state = ""
Something1
// icon_state = "1"
screen_loc = "1,1"
// 1,1 is the bottom-left corner
Something2
// icon_state = "2"
screen_loc = "2:2,1:16"
// You can also specify offsets as done above.

// Give it a icon and/or icon_state for graphics :D
You could just make the 'hud' list store the references to the objects instead of indexing them by a string and do something like
client.screen += hud


Saves the overhead of using Add() and having to keep track of every reference you have in the list (and looping if you don't want to keep track).

The only real use for your system is if you need to keep track of individual things in the list, at which point you should just have a variable pointing at it or another quick-access list that's not used for adding/removing on the screen list.
Depending on what other functions you define you could simply store multiple objects within a single index as need to add/remove them, or you could simply add them immediately to the client's screen.

The overhead itself that you speak of is negligible as it should only be called as needed.
The point is you should probably be adding and removing lists to the screen instead of individual objects, and the usage of Add() and Remove() should only be done when needed, += and -= are simply more performant, especially when dealing with larger lists of objects like the screen list can grow to be in more complex projects.

Your usage of them is actually one of the only valid places to use them, when adding and removing multiple things to a list, but your first idea of having the indices themselves point to a list of objects is actually a pretty solid solution if you do need to keep track of them as a group or something similar.

var/list/my_hud = list()

// Blah blah

my_hud["inventory"] = list()
my_hud["inventory"] += object_1
my_hud["inventory"] += object_2
// You could use Add() properly here too, but you'd likely want to create a reference to the list to not use :


client.screen += my_hud["inventory"]


Or something like that, so you're only using Add() when creating the list (if you use it there) and += when adding to the screen, so you can avoid the usage of Add() and Remove() on the screen list itself while adding and removing groups of objects at once.

I say this as someone working on a fairly large project that has actually had to go through and replace usage of Add() and Remove() in older code because it was being used on some lists that could get pretty big and when it was there was a noticeable drop in performance.
It's a special list like contents is, it can only contain objects.
Update :3

// Variables
client/var/tmp/list/khud = new

// Functions
client/proc
showHUD() {for(. in args){if(. in khud){if(isobj(khud[.])) {screen |= khud[.]}; else {for(. in khud[.]){screen |= .}}}}}
hideHUD() {for(. in args){if(. in khud){if(isobj(khud[.])) {screen -= khud[.]}; else {for(. in khud[.]){screen -= .}}}}}

// Demo
mob/Login()
client.khud["A"] = new/obj/Ball1
client.khud["B"] = list(new/obj/Ball2, new/obj/Ball3)
client.showHUD("A","B")
..()

obj
icon = 'icon.dmi'
Ball1 {screen_loc = "1,1"}
Ball2 {screen_loc = "2,2"}
Ball3 {screen_loc = "3,3"}
You don't actually need the 'in' check if you're using |= on a list, it does that for you, you're doubling up the check that way.

You should also make it so you can pass lists of objects instead of a bunch of objects as individual arguments.

Pretty good though :)
In response to Nadrew
Nadrew wrote:
You don't actually need the 'in' check if you're using |= on a list, it does that for you, you're doubling up the check that way.

You need it because if you try and access a index that doesn't exist further in the code you'll run into a runtime error.

You should also make it so you can pass lists of objects instead of a bunch of objects as individual arguments.

You can, I updated it. I got ahead of myself x3

Pretty good though :)

Thank you.

if(list[index])


Won't throw a runtime error if the index doesn't exist, it's actually a nice trick for quickly checking if an item is in a list, as long as it has an associated value it'll work.
if(khud[.]) screen |= khud[.]


I also meant something more like
var/list/something = list("A","B")
client.ShowHUD(something)


You'd need an istype() check for /list but it would make grouping things together loosely a lot more friendly.
Done :)

// Variables
client/var/tmp/list/khud = new

// Functions
client/proc
showHUD() {for(. in args){if(istype(.,/list)){for(. in .){showHUD(.)}}; else if(khud[.]) {if(isobj(khud[.])) {screen |= khud[.]}; else {for(. in khud[.]){screen |= .}}}}}
hideHUD() {for(. in args){if(istype(.,/list)){for(. in .){hideHUD(.)}}; else if(khud[.]) {if(isobj(khud[.])) {screen -= khud[.]}; else {for(. in khud[.]){screen -= .}}}}}

// Demo
mob/Login()
client.khud["A"] = new/obj/Ball1
client.khud["B"] = list(new/obj/Ball2, new/obj/Ball3)
var list/A = list("A")
client.showHUD(A,"B")
..()

obj
icon = 'icon.dmi'
Ball1 {screen_loc = "1,1"}
Ball2 {screen_loc = "2,2"}
Ball3 {screen_loc = "3,3"}
Now for the sake of forum readability uncompress it ;), writing code that way is generally not ideal when you want people to actually read and learn from it.

// Functions
client/proc
showHUD()
for(. in args)
if(istype(.,/list))
for(. in .)
showHUD(.)
else if(khud[.])
if(isobj(khud[.]))
screen |= khud[.]
else
for(. in khud[.])
screen |= .
hideHUD()
for(. in args)
if(istype(.,/list))
for(. in .)
hideHUD(.)
else if(khud[.])
if(isobj(khud[.]))
screen -= khud[.]
else
for(. in khud[.])
screen -= .