ID:139652
 
Code:
Hi guys! I'm using in a project I've been working on. In the code below, it shows the updateInventory() procedure it includes
mob
proc
updateInventory()
spawn(2)
var/i=1
winset(src,"inventory",{"cells="2x[src.contents.len]""})
for(var/atom/a in src.contents)
src<<output(a,"inventory:1,[i]")
src<<output(a.suffix,"inventory:2,[i]")
i++
src.updateInventory()

This does accomplish displaying my items in my game's inventory grid. [duh] However, I don't want it to display ALL of my contents. That's silly. Here I try my stupidly simple solution to not displaying everything:
mob
proc
updateInventory()
spawn(2)
var/i=1
winset(src,"inventory",{"cells="2x[src.contents.len]""})
for(var/obj/Items/a in src.contents)
src<<output(a,"inventory:1,[i]")
src<<output(a.suffix,"inventory:2,[i]")
i++
src.updateInventory()

This does filter out what I want, without compromising the item stacking. However! When you go to drop an item with this code:
mob
proc
itemDrop(obj/Items/o)
if(o.canStack && o.contents.len)
var/obj/Items/theItem=pick(o.contents)
theItem.loc=src.loc
if(o.contents.len)
o.suffix="x[o.contents.len+1]"
else
o.suffix="x1"
else
o.loc=src.loc
o.suffix=""

The item (if stacked) does diminish to 1 after several uses, however after you drop the last one, it lands on the ground, but the grid still displays the same obj in your grid with a suffix of 1. Even though it should be going away! I have looked through the code and couldn't find the reason causing the bug. If anybody can help explain why it's doing what I explained above, I would greatly appreciate it.


Having updateInventory() loop (and especially using recursion to loop) is stupid. The only time you need to call updateInventory() is when your inventory changes (which is going to be, on average, MUCH less often than five times per second). So, remove the spawn(), remove the recursive call, and just call updateInventory() whenever you need to, you know, update your inventory.

Anyway, for your issue: you are setting the number of cells to the length of your contents, but not displaying EVERY object in your contents. Truncating the length of the list is what causes the old items to not be shown. All you need to do is change src.contents.len to i-1, and move that winset() to after the for() loop.