[#modding] `MagazineAmmoLoader.HandleEvent(GetMissileWeaponProjectileEvent E)` doesn't set ...

Issue #6654 new
Freehold Games Bot Account created an issue

Marked for crossposting by: Noelle Lavenza (transgendeer)

Message (jump):

<Noelle Lavenza (transgendeer)> MagazineAmmoLoader.HandleEvent(GetMissileWeaponProjectileEvent E) doesn't set E.Projectile to MagazineAmmoLoader.Ammo (if valid/non-null), causing issues with using this event in mods with bows.

Comments (4)

  1. Armithaig

    Projectiles’re separate objects from Ammo (Zetachrome Arrow | ProjectileZetachromeArrow), XYP it’s not mentioned here what you’re trying to do?

    We can provide the ammo object in that event if you ultimately want it.

  2. Noelle Lavenza

    I realised that when I tried implementing it with a Harmony patch; the final implementation I wound up with was effectively the following:

            [HarmonyPatch(typeof(MagazineAmmoLoader), nameof(MagazineAmmoLoader.HandleEvent), new Type[] { typeof(GetMissileWeaponProjectileEvent) })]
            static bool Prefix(MagazineAmmoLoader __instance, GetMissileWeaponProjectileEvent E, ref bool __result)
            {
                if (__instance.Ammo != null && __instance.Ammo.IsValid())
                {
                    E.Projectile = GetProjectileObjectEvent.GetFor(__instance.Ammo, __instance.ParentObject);
                    __result = false;
                    return false;
                }
                return true;
            }
    

    or, translated out of Harmony:

            public override bool HandleEvent(GetMissileWeaponProjectileEvent E)
            {
    +            if (Ammo?.IsValid())
    +            {
    +                E.Projectile = GetProjectileObjectEvent.GetFor(Ammo, ParentObject)
    +                return false;
    +            }
                if (!string.IsNullOrEmpty(ProjectileObject))
                {
                    E.Blueprint = ProjectileObject;
                    return false;
                }
                return base.HandleEvent(E);
            }
    

    The main reason this is necessary is to make GetMissileWeaponProjectileEvent work for launchers and bows, which don’t set ProjectileObject. It might need to go after the ProjectileObject clause instead of before to avoid issues with things that do set it and instead ignore the Ammo object, like anything that loads lead slugs. (It might also need to set E.Blueprint in the Ammo clause as well, to avoid inconsistencies if something uses both E.Projectile and E.Blueprint expecting them to correspond.)

  3. Log in to comment