User avatar
xdlewisdx
Civil Servant Zombie
Civil Servant Zombie
Posts: 17
Joined: Mon Dec 12, 2016 3:44 am
x 3

Clear Players Inventory

Tue Jan 10, 2017 6:13 am

Just wondering if there is a way of clearing a players inventory like

player.Inventory.clearInventory

or something like that?
0 x

sharkbound
Civilian Zombie
Civilian Zombie
Posts: 74
Joined: Mon Apr 06, 2015 8:36 pm
x 4

Re: Clear Players Inventory

Thu Jan 12, 2017 4:25 am

xdlewisdx wrote:Just wondering if there is a way of clearing a players inventory like

player.Inventory.clearInventory

or something like that?

Here is the main way of doing it:

Code: Select all

            UnturnedPlayer player = (UnturnedPlayer)caller;

            // Loops over each of the player's inventory pages
            for (byte page = 0; page < 8; page++)
            {
                // gets the item count for the current inventory page its looking in
                var items = player.Inventory.getItemCount(page);

                // loop over all items in current inventory page
                for (byte index = 0; index < items; index++)
                {
                    // remove the item
                    player.Inventory.removeItem(page, 0);
                }
            }
0 x

Haruki
Civil Servant Zombie
Civil Servant Zombie
Posts: 7
Joined: Mon Jan 30, 2017 9:25 pm
x 1

Re: Clear Players Inventory

Sat Feb 04, 2017 4:14 am

I believe this way is better because it doesn't have an established value like the answer from the guy above (pages < 8). This way it takes the inventory size from the player exactly on how big it is, so you don't do any unnecessary iterations.

Code: Select all

            foreach (var i in pCaller.Inventory.items)
            {
                if (i == null) continue;
                for (byte w = 0; w < i.width; w++)
                {
                    for (byte h = 0; h < i.height; h++)
                    {
                        try
                        {
                            byte index = i.getIndex(w, h);
                            if (index == 255) continue;
                            i.removeItem(index);
                        }
                        catch { }
                    }
                }
            }
1 x

Return to “General Development Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests

Advertisement