Implementing ammo regeneration that is always active in zsc

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!)
Post Reply
anud
Posts: 3
Joined: Sat Aug 07, 2021 9:02 am

Implementing ammo regeneration that is always active in zsc

Post by anud »

Hello I'm making a mod that changes most of the weapon to also use a common resource "TimeMana", and I haven't figured out how where the regen code should be.

After I implemented the custom ammo

Code: Select all

class TimeMana : Ammo {
    default {
        Inventory.PickupMessage "Picked up 10 time mana"; // "Picked up a clip."
        Inventory.Amount 10;
        Inventory.MaxAmount 100;
        Inventory.Icon "CLIPA0";
    }
}
My initial try was to augment the tick function inside the DoomPlayer extension

Code: Select all

class SorcDoom : DoomPlayer {

    Default {
        Player.DisplayName "SorcDoom" ;
        Player.StartItem "IceLance" ;
        Player.WeaponSlot 1, "IceLance" ;
        
        Player.StartItem "SorcSpark" ;
        Player.WeaponSlot 2, "SorcSpark" ;
        Player.StartItem "Clip", 50 ;
        Player.StartItem "TimeMana", 1 ;
        // Player.WeaponSlot 1, "Pistol" ;
    }

    override void Tick()
    {
        Super.Tick(); // Note this could be after as well, as long as it's called *somewhere* unconditionally in the main Tick block.
        A_GiveInventory("TimeMana", 1);
    }
}
But after the initial tick it doesn't update anymore (ingame the TimeMana inventory value is bumped up to 2)

Another solution was to have a TimeManaGenerator item that while being held it would increment time mana

Code: Select all

class TimeManaGenerator : CustomInventory {
    default {
        Inventory.PickupMessage "$GOTCLIP"; // "Picked up a clip."
        Inventory.Amount 1;
        Inventory.MaxAmount 1;
        Inventory.Icon "CLIPA0";
    }

    States {
        Held: 
            TNT1 A 1 {
                A_Print("Hello");
                A_GiveInventory("TimeMana", 1);
            }
            Loop;
        
    }
}
but the held state doesn't trigger.

Any ideas on what needs to be done?
User avatar
Enjay
 
 
Posts: 26533
Joined: Tue Jul 15, 2003 4:58 pm
Location: Scotland
Contact:

Re: Implementing ammo regeneration that is always active in

Post by Enjay »

Accidental duplicate of this thread that was made while not logged in has been moved to the garbage bin at OPs request.
User avatar
Virathas
Posts: 249
Joined: Thu Aug 10, 2017 9:38 am

Re: Implementing ammo regeneration that is always active in

Post by Virathas »

You can give an itemthat uses the DoEffect() to grant mana. As a simple example:

Code: Select all

class ManaGiver : Inventory
{
Default
{
Inventory.Amount 1;
Inventory.MaxAmount 1;
}
override void DoEffect()
{
Super.DoEffect();
Owner.GiveInventory("TimeMana");
}
}
This of course does not handle things like "do not regenerate if dead" and regenerates 1 every tic (35/second).

I think the Tick() function didn't work for player, since player uses PlayerThink() in that place (and overriding it seems to be dangerous, at least with multiplayer compatibility in mind). Someone can probably correct me about this though.
Jarewill
 
 
Posts: 1766
Joined: Sun Jul 21, 2019 8:54 am

Re: Implementing ammo regeneration that is always active in

Post by Jarewill »

Virathas wrote:I think the Tick() function didn't work for player, since player uses PlayerThink() in that place (and overriding it seems to be dangerous, at least with multiplayer compatibility in mind). Someone can probably correct me about this though.
Actually, the Tick override still works for the PlayerPawn actor, as I was using that method in most my mods.
However I do not know why the OP's code didn't work.
anud
Posts: 3
Joined: Sat Aug 07, 2021 9:02 am

Re: Implementing ammo regeneration that is always active in

Post by anud »

Thanks for the responses. In the end I used the PlayerThink() override approach because that seems to work.
Reading about multiplayer compatibility what are things that should i keep in mind?

this is the function implementation

Code: Select all

 override void PlayerThink()
    {
        Super.PlayerThink();
        timePass = timePass + 1;
        if(timePass % 35 == 0) {
            A_GiveInventory("TimeMana", 1);
        }
        if(timePass > 700) {
            timePass = 1;
        }
    }
User avatar
Player701
 
 
Posts: 1640
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: Implementing ammo regeneration that is always active in

Post by Player701 »

There shouldn't be any multiplayer issues. Note that you can use GetAge() instead of a custom variable that you increment every tick. The Tick() override should work as well, it worked in my tests for sure. If the code works if you override PlayerThink but not Tick, it could be a bug in your GZDoom version, so you should update to the latest version unless you haven't already done that.
Post Reply

Return to “Scripting”