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.

Login to reply.