(Solved) Help with a recharging gun

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
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
OnionTaco22
Posts: 9
Joined: Tue Aug 29, 2023 3:49 am
Operating System Version (Optional): Windows 11
Location: Australia

(Solved) Help with a recharging gun

Post by OnionTaco22 »

** I have solved this **
** I have included the new code at the bottom **

I'm making a replacement for the chaingun that recharges ammo overtime, but I'm running into an issue. The code for the most part works, but the way it recharges isn't right. When you finish shooting the weapon, it's supposed to have a ~2 second cooldown before it starts recharging again. The main problem though is that the weapon doesn't seem to process something to do with the JustFired bool, meaning the gun doesn't care if you've just shot (or even if you're still shooting!) and will still recharge ammo anyways.

I'm still fairly new to zscript, so I'm not quite sure what I've done wrong here.

Code: Select all

class NewChaingun : Chaingun
{
	bool JustFired;
	property JustFired: JustFired;
	
	Default
	{
		Weapon.SelectionOrder 900;
		Weapon.AmmoType "ChaingunBullets";
        	Weapon.AmmoUse 1;
        	Weapon.AmmoGive 20;
		Weapon.SlotNumber 4;
		Weapon.SlotPriority 5;
		Weapon.Kickback 50;
		Inventory.Pickupmessage "$UN_CHAIN_PICKUP";
		Tag "Recharging Chaingun";
		AttackSound "weapons/chngun";
		
		NewChaingun.JustFired false;
	}
	States
	{
		Select:
			CHGG A 1 A_Raise;
			Loop;
		Deselect:
			CHGG A 1 A_Lower;
			Loop;
		Ready:
			CHGG A 1 A_WeaponReady;
			Loop;
		Fire:
			CHGG A 6 {
				A_FireBullets(5.6, 3, -1, 7, "BulletPuff");
				A_GunFlash();
				invoker.JustFired = true;
			}
			CHGG B 6 A_FireBullets(5.6, 0, -1, 7, "BulletPuff");
			CHGG B 0 A_ReFire;
			Goto Ready;
		Flash:						  
			CHGF A 6 Bright A_Light1;
			CHGF B 6 Bright A_Light2;
			Stop;
		Spawn:
			MGUN A -1;
			Stop;
	}
	
	override void DoEffect() {
		int TicsToCharge = 0;
		if (owner && GetAge() % TICRATE == 0 && JustFired == false) {
			owner.A_GiveInventory("ChaingunBullets");
		} else if (JustFired == true) {
			while (TicsToCharge % 70 == 0) {
				TicsToCharge += 1;
			}
			JustFired = false;
		}
	}
}
-- New Code

Code: Select all

class NewChaingun : Chaingun
{
    int TicsToCharge;
    property TicsToCharge: TicsToCharge;
    
    Default
    {
        *snip*


        NewChaingun.TicsToCharge 70;
        // starts TicsToCharge at 70 because the charging function
        // requires it to be at 70 to charge
        
        //$Category "Weapons"
    }
    States
    {
        *snip*


        Fire:
            CHGG A 6 {
                A_FireBullets(5.6, 3, -1, 7, "BulletPuff");
                A_GunFlash();
                invoker.TicsToCharge = 0;
            }


            *snip*
    }
    
    override void DoEffect() {
        if (owner && GetAge() % TICRATE == 0 && TicsToCharge == 70) {
            owner.A_GiveInventory("ChaingunBullets");
        } else if (TicsToCharge < 70) {
            TicsToCharge++;
        }
    }
}

Return to “Scripting”