ID:846679
 
(See the best response by Kaiochao.)
Im a bit a new when it comes to byond programming, ive been trying develop a simple inventory system using my own knowledge (without the libs). Some advice, and ways to improve it would be quite helpful.

Thank you.
Code:
mob/var
patha // These will be holding the object's path.
pathb
pathc
pathd
obj

var
newloc
hud
slot // the hud slots, i dont think they are in any use, but to be displayed, IMO that is.
icon = 'slot.dmi'
icon_state = "holder"
New(client/c)
c.screen+=src
red
icon = 'slot.dmi'
icon_state = "obj1"
density = 1
layer = 5
Click()
if(src.loc == usr)
usr.overlays += src

verb
Pick()
set src in oview(1)
if(usr.patha == null)
usr.patha = /obj/red
src.loc = usr
src.newloc = "3,3"
else
return
if(usr.pathb == null)
usr.pathb = /obj/red
src.newloc = "3,4"
src.loc = usr
else
return
if(usr.pathc == null)
usr.pathc = /obj/red
src.newloc = "4,3"
src.loc = usr
else
return
if(usr.pathd == null)
usr.pathd = /obj/red
src.newloc = "4,4"
src.loc = usr
else
return
New(client/c)
c.screen += src
src.screen_loc = "[src.newloc]"



mob/Login()
..()
mob/verb
inventorysys()
if(usr.invhud == 0)
var/obj/hud/slot/a = new /obj/hud/slot(usr.client)
a.screen_loc = "3,3"
var/obj/hud/slot/b = new /obj/hud/slot(usr.client)
b.screen_loc = "3,4"
var/obj/hud/slot/c = new /obj/hud/slot(usr.client)
c.screen_loc = "4,3"
var/obj/hud/slot/d = new /obj/hud/slot(usr.client)
d.screen_loc = "4,4"
usr.invhud = 1
updateinv()
else
for(var/obj/hud/slot/a in usr.client.screen)
del(a)
usr.invhud = 0
mob/var
invhud = 0
mob/proc/updateinv()
if(src.patha == null)
return
else
new patha(usr.client)


Problem description:

Best response
Instead of using 4 variables and manually setting each one after the other, you can use a list.
mob
var
slots[0]

The '[0]' declares the variable as a list, and the 0 sets the variable to a new, empty list. (if it was a 1 or any other number, it would be a list with x nulls inside)

To fill it with slots, you'd do something like this:
mob/verb/inventorysys()
if(!invhud)
for(var/x in 3 to 4) for(var/y in 3 to 4)
var obj/hud/slot/s = new (client)
s.screen_loc = "[x],[y]"
slots += s
invhud = 1
updateinv()
else
for(var/obj/hud/slot/s in slots)
del s
invhud = 0

That arranges them in the way you want. The important part is this line:
slots += s

That, of course, adds the slot to the list of slots. So by the end of it, the 'slots' list should contain all 4 slots.

To update your inventory with, say, the first four items in your contents, you could do this:
mob/proc/updateinv()
for(var/n in 1 to min(contents.len, slots.len))
var obj/item = contents[n]
var obj/hud/slot/s = slots[n]
s.show_item(item) // <--this just just an example

min(contents.len, slots.len) allows you to have a variable slots length as well as contents length. It chooses the minimum number so that it doesn't try checking the 5th item in contents when you only have 4, because that would cause a runtime error.
contents[n] got the nth item in your 'contents' (list).
slots[n] got the nth (corresponding) slot in your 'slots' list.

Of course, you may choose to display your items differently, like attach an item to a slot, so you could do something like swap the items between slots. In that case, you'd do this:
obj/hud/slot
// This contains the item which the slot is holding
var obj/item

// It's more convenient to save the client for later use
var client/c
New(client/c)
src.c = c
c.screen += src

proc/show_item(obj/i)
// Just a couple safety checks.
if(!i) return

if(item && i != item)
hide_item()

item = i
i.screen_loc = screen_loc
c.screen += i

// This would be called when you drop an item.
proc/hide_item()
if(!item) return

item.screen_loc = ""
c.screen -= i
item = null

// This would be called when you pick up an item.
mob/proc/add_item(obj/item)
// Find an empty slot
var obj/hud/empty_slot
for(empty_slot in slots)
if(!slot.item)
break
// The special property of 'break' in that for() means
// empty_slot is either 'null' or the last object to be looped over
// before the 'break' was made.

if(!empty_slot)
src << "You have no more space in your inventory!"
return

empty_slot.show_item(item)

If you read through it slowly enough, it should all make sense. There's nothing really complicated here. You could even try adding a swap_item() proc yourself.
Thank you very much Kaiochao!
Very helpful and detailed!