Remove Inventory On Weapon Drop?

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.

Remove Inventory On Weapon Drop?

Postby CaptainNurbles » Mon Jan 11, 2021 12:01 pm

I'm wondering if it is possible to have items removed from your inventory when a weapon is dropped. As an example, I drop a weapon and in doing so the ammo I've got for that gun is removed from my inventory. Is there a way to code this in? I've looked around the ZDoom wiki and forums and haven't found much help.
CaptainNurbles
 
Joined: 27 Jan 2018
Location: The Deepest Reaches of Space
Operating System: Windows 10/8.1/8/201x 64-bit
Graphics Processor: nVidia (Modern GZDoom)

Re: Remove Inventory On Weapon Drop?

Postby Mikk- » Mon Jan 11, 2021 2:31 pm

If you fancy using ZScript it seems like you can override the virtual function: OnDrop

here's a little zscript code using Doom's shotgun as an example:

Code: Select allExpand view
class customShotgun : shotgun
    
{
    default
        
{
        weapon.SlotNumber 3;
        }
    override void OnDrop(actor dropper)
        {
        if(dropper)
            dropper.TakeInventory("shell",999);
        }
    }
Last edited by Mikk- on Mon Jan 11, 2021 2:36 pm, edited 1 time in total.
User avatar
Mikk-
yooooooooooo
 
Joined: 30 Jun 2009
Location: Somewhere off Kanagawa
Discord: Mikk0451#3922

Re: Remove Inventory On Weapon Drop?

Postby Jarewill » Mon Jan 11, 2021 2:33 pm

It's possible either with ACS or with ZScript.
With ACS you can set up a script like this:
Code: Select allExpand view
Script "WeaponAmmo" ENTER
{
    While(true)
    {
        If(!CheckInventory("Weapon")){TakeInventory("Ammo",10);}
        Delay(1);
    }

Or you can add this override to your weapon if it's done in ZScript:
Code: Select allExpand view
Override void DetachFromOwner()
{
    owner.A_TakeInventory("Ammo",10);
    Super.DetachFromOwner();

Though it's probably better to do weapon magazines this way if using ZScript.
Jarewill
 
 
 
Joined: 21 Jul 2019

Re: Remove Inventory On Weapon Drop?

Postby Matt » Mon Jan 11, 2021 4:50 pm

DetachFromOwner will also happen if the weapon is destroyed or taken away. I don't know about OnDrop but I assume from the name of the function that it will only kick in on an actual drop.
User avatar
Matt
Putting the XD into *xdeath since 2007
 
Joined: 04 Jan 2004
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia

Re: Remove Inventory On Weapon Drop?

Postby CaptainNurbles » Mon Jan 11, 2021 7:07 pm

[quote="Jarewill"]It's possible either with ACS or with ZScript.
With ACS you can set up a script like this:
Code: Select allExpand view
Script "WeaponAmmo" ENTER
{
    While(true)
    {
        If(!CheckInventory("Weapon")){TakeInventory("Ammo",10);}
        Delay(1);
    }
}


Your ACS script works perfectly for what I've in mind, thank ya!
CaptainNurbles
 
Joined: 27 Jan 2018
Location: The Deepest Reaches of Space
Operating System: Windows 10/8.1/8/201x 64-bit
Graphics Processor: nVidia (Modern GZDoom)

Re: Remove Inventory On Weapon Drop?

Postby Matt » Mon Jan 11, 2021 7:10 pm

How does that not continuously take away ammo until you're completely out?
User avatar
Matt
Putting the XD into *xdeath since 2007
 
Joined: 04 Jan 2004
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia

Re: Remove Inventory On Weapon Drop?

Postby Player701 » Mon Jan 11, 2021 11:40 pm

Indeed, the state needs to be saved somewhere, or the script will keep taking ammo continuously when the weapon is not present.

The fixed script can be found further below. However, unless you are limited to Zandronum compatibility, please do not use ACS to implement or modify game logic. It was once widespread only because there were no better tools, but now we have them. The solution should be implemented in ZScript, by overriding either OnDrop or DetachFromOwner, depending on your exact use case(s), as shown by Mikk- and Jarewill in the posts above.

Code: Select allExpand view
#include "zcommon.acs"

bool weaponCheck[8];

script "WeaponAmmo" ENTER
{
    bool hasWeapon = CheckInventory("Weapon");
    int pnum = PlayerNumber();
   
    if (!hasWeapon && weaponCheck[pnum])
    {
        TakeInventory("Ammo", 10);
    }
   
    weaponCheck[pnum] = hasWeapon;
    delay(1);
    restart;
}
User avatar
Player701
 
 
 
Joined: 13 May 2009
Location: Russia
Discord: Player701#8214
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support


Return to Scripting

Who is online

Users browsing this forum: No registered users and 0 guests