A question about backpacks and inventory item capacity

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
User avatar
Chaosvolt
Posts: 61
Joined: Mon Apr 19, 2021 12:58 pm

A question about backpacks and inventory item capacity

Post by Chaosvolt »

In a nutshell, I was pondering if there's any way to rig it so that picking up a backpack could change the maximum amount of a usable inventory item, like it naturally can for ammo? I see two possible approaches to this idea, but I'm not sure how either would work.

Option one would be if picking up a backpack could in some way alter the Inventory.MaxAmount of non-ammo items. I'm not 100% sure how that'd look in ZScript or ACS, nor if it'd work in a way that doesn't break when there's more than one player.

Option two would be for the items in question to drain a specific ammo item instead of depleting themselves when activated. The only problem there is how would you then go about rigging the hotbar to automatically display the ammo it wants to use instead of how many of that item you have?

A third option that comes to mind maybe would be for the pickups to actually be ammo instead of inventory, set to give you a matching amount of the relevant item in its Pickup state, setting it so the max amount of the inventory item itself is equal to the backpack max of the linked ammo, then having the item's use state check for the ammo as well as the item.

One list possibility, given what I might one day use this feature for: If the relevant item was primarily obtained from a conversation menu, either buying it from a shopkeeper or obtained via a crafting menu, then presumably ZSDF is robust enough to lock a dialogue option behind a check for whether you have a given item or not, thus allowing it to detect whether you're at capacity, and change the amount to check for based on if you have a backpack?
User avatar
Player701
 
 
Posts: 1401
Joined: Wed May 13, 2009 3:15 am
Discord: Player701#8214
Graphics Processor: nVidia with Vulkan support
Location: Russia

Re: A question about backpacks and inventory item capacity

Post by Player701 »

Chaosvolt wrote:Option one would be if picking up a backpack could in some way alter the Inventory.MaxAmount of non-ammo items. I'm not 100% sure how that'd look in ZScript or ACS, nor if it'd work in a way that doesn't break when there's more than one player.
This is most certainly possible with ZScript, but your non-ammo item needs to have the INVENTORY.KEEPDEPLETED flag set; otherwise, when you run out of it, the item will disappear from the inventory completely, and the next time you pick it up, it will have its default MaxAmount. This method will not break when there's more than one player.

Code: Select all

class MyItem : Inventory
{
    Default
    {
        +INVENTORY.KEEPDEPLETED;
        ...
    }

    ...
}

class MyBackpack : Backpack replaces Backpack
{   
    override Inventory CreateCopy(Actor other)
    {
        let mi = MyItem(other.FindInventory('MyItem'));

        if (mi == null)
        {
            // Give the item if it's not present
            mi = MyItem(Spawn('MyItem'));

            // Make sure that item was spawned and pickup succeeded
            if (mi != null)
            {
                mi.Amount = 0;

                if (!mi.CallTryPickup(other))
                {
                    mi.Destroy();
                    mi = null;
                }
            }
        }

        if (mi != null)
        {
            // In this example, the MaxAmount is increased by 10 once.
            // Note that CreateCopy is not called again
            // if the backpack is already in the player's inventory.
            mi.MaxAmount += 10;
        }

        return Super.CreateCopy(other);
    }

    // When removing the backpack, reduce the MaxAmount to normal
    override void DetachFromOwner()
    {
        let mi = MyItem(Owner.FindInventory('MyItem'));

        if (mi != null)
        {
            mi.MaxAmount -= 10;
        }

        Super.DetachFromOwner();
    }
}
I do not quite understand the other options you propose. If the provided solution is not desirable, please elaborate further on your other suggestions and provide some (hypothetical) examples.
User avatar
Chaosvolt
Posts: 61
Joined: Mon Apr 19, 2021 12:58 pm

Re: A question about backpacks and inventory item capacity

Post by Chaosvolt »

Nice, that looks like a good fix for it I'll want to test out.
Player701 wrote: I do not quite understand the other options you propose. If the provided solution is not desirable, please elaborate further on your other suggestions and provide some (hypothetical) examples.
Basically I was just trying to figure out a rough outline of alternatives just in case the first option wasn't actually doable, since I wasn't sure. Fairly certain the solution you brought up covers it just fine for the most part.

I suppose one implementation the second proposal might be useful for would be Build-engine style powerups that have charges it depletes instead of discrete uses, both basically amounts to something where the displayed amount doesn't correlated to individual uses of the powerup. I do think I have a vague idea on how that could possibly be implemented, but I'll have to fiddle with it sometime.

Return to “Scripting”