Update weapon damage from script?

gekido

Member
I have a number of weapons setup in my game, and want to be able to update their weapon damage amounts dynamically on startup from script.

Is there documentation for how I can get access to the various modules from script?

Is the 'damage processor' the expected way of doing this?
 
The bottom of this page has an example of getting a module:


Using a Damage Processor is definitely a good approach to this if you don't want to adjust the module directly.
 
So just for anyone else looking for this, here's a simple script that lets you do this:


C#:
using Opsive.UltimateCharacterController.Inventory;
using Opsive.UltimateCharacterController.Items.Actions;
using Opsive.UltimateCharacterController.Items.Actions.Impact;
using Opsive.UltimateCharacterController.Items.Actions.Modules.Shootable;
using UnityEngine;

namespace PixelWizards.Gameplay
{
    public class ItemMetadataUpdater : MonoBehaviour
    {
        /// <summary>
        /// Outputs the item amount.
        /// </summary>
        public void Start()
        {
            var inventory = GetComponent<InventoryBase>();
            if (inventory == null)
            {
                return;
            }

            for (var i = 0; i < inventory.SlotCount; ++i)
            {
                var item = inventory.GetActiveCharacterItem(i);
                if (item != null)
                {
                    Debug.Log("Inventory Amount: " + inventory.GetItemIdentifierAmount(item.ItemIdentifier));
                    // A single item can have multiple Item Actions.
                    var itemActions = item.ItemActions;
                    foreach (var action in itemActions)
                    {
                        var shootableAction = action as ShootableAction;
                        if (shootableAction != null)
                        {
                            UpdateAmmoModule(shootableAction);
                            UpdateDamageModule(shootableAction);
                        }
                    }
                }
            }
        }

        private void UpdateDamageModule(ShootableAction action)
        {
            // get the impact module
            var impactModuleGroup = action.ImpactModuleGroup;
            if (impactModuleGroup != null)
            {
                // we should only have one module
                for (var k = 0; k < impactModuleGroup.ModuleCount; ++k)
                {
                    var impactModule = impactModuleGroup.Modules[k] as GenericShootableImpactModule;
                    foreach (var entry in impactModule.ImpactActions.ImpactActions)
                    {
                        if (entry is SimpleDamage simpleDamage)
                        {
                            simpleDamage.DamageAmount = 10;
                        }
                    }
                }
            }
        }

        private void UpdateAmmoModule(ShootableAction action)
        {
            var ammoModuleGroup = action.AmmoModuleGroup;
            if (ammoModuleGroup != null)
            {
                for (int k = 0; k < ammoModuleGroup.ModuleCount; ++k)
                {
                    var shootableAmmoModule = ammoModuleGroup.Modules[k];
                    if (shootableAmmoModule != null)
                    {
                        shootableAmmoModule.AdjustAmmoAmount(10);
                        Debug.Log("Ammo Remaining: " + shootableAmmoModule.GetAmmoRemainingCount());
                    }
                }
            }
        }
    }
}
 
Last edited:
Back
Top