Accessing custom property dynamically

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

Accessing custom property dynamically

Post by malon3 »

How do I access the "AmmoUsage" property from the projectile?

The name of the gun is "CompanionZombieManGun" and there will be many more gun types that inherit from BaseGun, so how can I do this dynamically?

Code: Select all

class BaseGun : Weapon
{
  int AmmoUsage;
  property AmmoUsage : AmmoUsage;
  Default
  {
    Weapon.AmmoUse1 0;
    BaseGun.AmmoUsage 0;
  }
  States
  {
    Fire:
      TNT1 A 0 A_FireProjectile("myProjectile");
  }
}

class CompanionZombieManGun: BaseGun
{
  Default
  {
    Weapon.AmmoUse1 1;
    BaseGun.AmmoUsage 2;
  }
}

class myProjectile : Actor
{
  TNT1 A 0
  {
    Death:
      console.printf(target.player.readyweapon.AmmoUse1..""); // no error when compiling this line
      console.printf(target.player.readyweapon.AmmoUsage..""); // complains about AmmoUsage Unknown identifier
  }
}
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49223
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Accessing custom property dynamically

Post by Graf Zahl »

You'd have to cast your weapon to BaseWeapon - but it's ot that easy because it will fail if you switch weapons before the projectile hits something.
Your code may randomly abort both on target.player and target.player.readyweapon. You should access neither in the projectile.

Your only safe way would be to copy the needed info from the weapon to the projectile when being spawned.
malon3
Posts: 103
Joined: Fri Dec 26, 2008 4:41 pm

Re: Accessing custom property dynamically

Post by malon3 »

Thank you, I had already prepared for the weapon changing before impact. I saved the state before the shot and after the shot.

My mistake was thinking that casting to the parent class would affect values set by the child class.

Thank you Graf, you've answered several of my dumb questions.
Post Reply

Return to “Scripting”