[Solved] AmmoType2 will not increment on pickup

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!)
malon3
Posts: 103
Joined: Fri Dec 26, 2008 4:41 pm

[Solved] AmmoType2 will not increment on pickup

Post by malon3 »

Issue:
AmmoType2 does not increment, unless picking up another copy of the weapon associated with it.
Picking up "MyNewAmmo" increments nothing.
Picking up a backpack increments only AmmoType1, not AmmoType2.

The FULL code below, for debug purposes, try it yourself.

To test, load the code then do "summon mynewgun" and "summon mynewammo" and "summon backpack"

Remember that full screen shows the secondary ammo type in the bottom right corner (if you decide to help) as a way to see how much secondary ammo you have

Code: Select all

version "4.8.0"

// MyBaseWeapon is never used directly
class MyBaseWeapon : Weapon
{
  Default
  {
    Weapon.AmmoType1 "Clip";
    Weapon.AmmoType2 ""; // we set this in beginplay()
    Weapon.AmmoGive1 1;
    Weapon.AmmoGive2 1;
    Weapon.AmmoUse1 0;
    Weapon.AmmoUse2 0;
    Weapon.SlotNumber 2;
  }
  States
  {
    // just default pistol states for testing purposes, nothing special here
    Ready:
        PISG A 1 A_WeaponReady;
        Loop;
    Deselect:
        PISG A 1 A_Lower;
        Loop;
    Select:
        PISG A 1 A_Raise;
        Loop;
    Fire:
        PISG A 4;
        PISG B 6 A_FirePistol;
        PISG C 4;
        PISG B 5 A_ReFire;
        Goto Ready;
    Flash:
        PISF A 7 Bright A_Light1;
        Goto LightDone;
        PISF A 7 Bright A_Light1;
        Goto LightDone;
     Spawn:
        PIST A -1;
        Stop;
    }

  override void BeginPlay()
  {
    Super.BeginPlay();

    // The new gun and new ammo are named similarly "MyNewGun" and "MyNewAmmo"
    // Allowing us to easily get the associated ammo, by stripping off the word "Gun" and inserting "Ammo"
    String className = GetClassName();
    className = className.Left(className.Length() - 3);
    AmmoType2 = className.."Ammo";
  }
}

Class MyNewGun : MyBaseWeapon
{
  Default
  {
    Inventory.PickupMessage "picked up MyNewGun";
  }

  // This is just to show that when you summon it in console, the ammotypes are properly set
  override void BeginPlay()
  {
    Super.BeginPlay();
    console.printf(GetClassName().." ammo1type is "..AmmoType1.GetClassName());
    console.printf(GetClassName().." ammo2type is "..AmmoType2.GetClassName());
  }
}

// MyBaseAmmo is never used directly
Class MyBaseAmmo : Ammo
{
  Default
  {
    Inventory.Amount 1;
    Inventory.MaxAmount 999;
    Inventory.InterHubAmount 999;
    Ammo.BackpackAmount 1;
    Ammo.BackpackMaxAmount 999;
  }
  States
  {
    Spawn:
      CELL A -1;
      Stop;
  }
}

Class MyNewAmmo : MyBaseAmmo
{
  Default
  {
    Inventory.PickupMessage "picked up MyNewAmmo";
  }
}
Last edited by malon3 on Mon Apr 04, 2022 3:40 pm, edited 1 time in total.
malon3
Posts: 103
Joined: Fri Dec 26, 2008 4:41 pm

Re: AmmoType2 will not increment on pickup

Post by malon3 »

So the answer is

"this is an issue with what's being considered the parent ammo. Since MyNewAmmo inherits from MyBaseAmmo, it assumes that it's a sub type of the parent ammo type MyBaseAmmo, similar to ClipBox vs Clip. To fix this, you'll need to override the GetParentAmmo() function in your MyBaseAmmo class:"

Code: Select all

override class<Ammo> GetParentAmmo()
{
  class<Object> type = GetClass();
  while (type.GetParentClass() && type.GetParentClass() != "MyBaseAmmo")
  {
    type = type.GetParentClass();
  }
        
  return (class<Ammo>)(type);
}
 
"by default Ammo is considered the "stopping" point when looking for the parent ammo class. This fails when implementing new base ammo types unless overridden because it'll go all the way up to MyBaseAmmo and then finally stop
Anything that inherits from MyBaseAmmo will, unless fixed, think it's actually a type of MyBaseAmmo ammo
Similar to how picking up a RocketBox will give RocketAmmo
It sees that MyNewAmmo isn't the parent type so instead it gives MyBaseAmmo thinking that's what you wanted
You can override who's considered the parent to fix this"

Thanks to boondorl on discord

Return to “Scripting”