ModuleInstance

Abstract Scriptable Object

Description

ModuleInstances can be attached to Modules so that unique data can be selected per StatusEffectData. Create one by simply inheriting from the ModuleInstance class or using Create > Status Effect Framework > Module Instance Script.

As an example, say you need a VFX prefab to be instantiated on each effect. Instead of having to create a unique Module for each prefab that you need, just add the variables you need in the ModuleInstance and attach it to the Module using the [AttachModuleInstance] attribute.

Now, every time you add a module to a StatusEffectData, it will instantiate those values for that effect making it so you only need to create one Module scriptable object with any constant values and then unique instance values will be displayed in each specific StatusEffectData.

Example

using UnityEngine;
// Use StatusEffects.Modules namespace for organization.
namespace StatusEffects.Modules
{
    public class VFXInstance : ModuleInstance
    {
        public GameObject prefab;  
    }
}
using System.Threading;
using UnityEngine;

// Use StatusEffects.Modules namespace for organization.
namespace StatusEffects.Modules
{
    // Setup scriptable object in create menu.
    [CreateAssetMenu(fileName = "Vfx Module", menuName = "Status Effect Framework/Modules/Vfx", order = 1)]
    // This attribute will attach the module instance so 
    // that the vfx can be unique to each effect.
    [AttachModuleInstance(typeof(VfxInstance))]
    public class VfxModule: Module
    {
        public override async Awaitable EnableModule(StatusManager manager, StatusEffect statusEffect, ModuleInstance moduleInstance, CancellationToken token)
        {
            VfxInstance vfxInstance = moduleInstance as VfxInstance;
            // Make sure the particle system stop action is set to destroy so it
            // automatically destroys itself when all particles die.
            GameObject vfxGameObject = Instantiate(vfxInstance.Prefab, manager.transform);
            // If we want this effect to be added everytime more are stacks
            // added we just immediately begin destruction on the current 
            // particle.
            if (vfxInstance.InstantiateAgainWhenAddingStacks)
                statusEffect.OnStackUpdate += (previous, stack) => OnStackUpdate(vfxInstance.Prefab, manager, statusEffect, previous, stack);
            else
                while (!token.IsCancellationRequested)
                    await Awaitable.NextFrameAsync();
            // Attempt to stop the particle system.
            if (!vfxInstance.InstantiateAgainWhenAddingStacks)
            {
                // Note that you need to check if the effect is null in case 
                // the cancellation was invoked from the destruction of 
                // the MonoBehaviour.
                if (!vfxGameObject)
                    return;
            
                vfxGameObject.GetComponent<ParticleSystem>().Stop();
            }
        }
    }
}

Last updated