ID:157257
 
What would be the best way to make the code below more simplified and more efficient?
client
MouseEntered(atom/A,location)
if(istype(A,/obj/HUD/Slot1Tab))
var/i=new/obj/HUD/PP1(usr.client)
textfont.QuickText(i, "PP 35/35", layer=FLY_LAYER)
if(istype(A,/obj/HUD/Slot2Tab))
var/i2=new/obj/HUD/PP2(usr.client)
textfont.QuickText(i2, "PP 35/35", layer=FLY_LAYER)
if(istype(A,/obj/HUD/Slot3Tab))
var/i3=new/obj/HUD/PP3(usr.client)
textfont.QuickText(i3, "PP 35/35", layer=FLY_LAYER)
if(istype(A,/obj/HUD/Slot4Tab))
var/i4=new/obj/HUD/PP4(usr.client)
textfont.QuickText(i4, "PP 35/35", layer=FLY_LAYER)
MouseExited()
if(locate(/obj/HUD/PP1) in screen)
del(locate(/obj/HUD/PP1) in screen)
if(locate(/obj/HUD/PP2) in screen)
del(locate(/obj/HUD/PP2) in screen)
if(locate(/obj/HUD/PP3) in screen)
del(locate(/obj/HUD/PP3) in screen)
if(locate(/obj/HUD/PP4) in screen)
del(locate(/obj/HUD/PP4) in screen)
else return
// Note that /obj/HUD/PPX is with just PP; the screen_loc is directly modified so there's less copy+pasting going on


/obj/HUD/PP
New(client/C, i)
if(!C || !i) del src
src.screen_loc = "NORTH,WEST-[5-i]" // Aligned to the top right corner.
C.screen += src


// Note that the slot tab's path is different, it is better if all the slots were under one child so less overhead caused since we can define the mouse enter/exit (using specifically under the object instead of the client)

/obj/HUD/Slot
Tab1
Tab2
Tab3
Tab4

// Note that if all the tabs above only have the screen_loc different, it would be best to have just one path and directly edit its screen_loc in my opinion

MouseEntered()
// var/num = copytext("[src.path]", length("[src.path]")-1) // If you want the tab #
var/i=new/obj/HUD/PP(usr.client, i)
textfont.QuickText(i, "PP 35/35", layer=FLY_LAYER)

MouseExited()
del(locate(/obj/HUD/PP) in screen)


This is just a quickie
If your going to create and delete them like this, why not cache references to the object(s) in the client? It would save you the trouble of using locate to find them, just check if the variable is set. You may need to reset the value to nil after deleting the object.