Class EntityAttributes
- Namespace
- ElectricDrill.AstraRpgFramework.Attributes
- Assembly
- com.electricdrill.astra-rpg-framework.Runtime.dll
Manages the attributes of an entity. This component calculates attribute values by combining base values, flat modifiers, percentage modifiers, and spent attribute points. It requires an ElectricDrill.AstraRpgFramework.Attributes.EntityAttributes.EntityCore component on the same GameObject.
[DisallowMultipleComponent]
[RequireComponent(typeof(EntityCore))]
public class EntityAttributes : MonoBehaviour, IHasAttributeSet, IValueProvider<AttributeSO, long>, IAttributeReader, IValueContainer<AttributeSO>, IHasEntity, IEventRegistrar
- Inheritance
-
objectEntityAttributes
- Implements
- Extension Methods
Properties
AttributeSet
Gets the AttributeSet used by this entity. The source of the attribute set depends on whether UseBaseAttributesFromClass is true. If true, it returns the attribute set from the EntityClass. Otherwise, it returns the ElectricDrill.AstraRpgFramework.Attributes.EntityAttributes._fixedBaseAttributeSet.
public virtual AttributeSetSO AttributeSet { get; }
Property Value
AvailableAttributePoints
Gets the number of attribute points currently available to spend.
public int AvailableAttributePoints { get; }
Property Value
- int
EntityClass
Gets the class for this entity. The class is used to determine base attributes when ElectricDrill.AstraRpgFramework.Attributes.EntityAttributes._useBaseAttributesFromClass is true.
public IClassSource EntityClass { get; }
Property Value
FlatModifiers
Gets the instance of flat modifiers for the attributes. Flat modifiers are added directly to the base attribute values.
protected virtual AttributeSetInstance FlatModifiers { get; }
Property Value
OnAttributeChanged
Event raised when an Attribute changes.
public AttributeChangedGameEvent OnAttributeChanged { get; }
Property Value
PercentageModifiers
Gets the instance of percentage modifiers for the attributes. Percentage modifiers are applied after flat modifiers.
protected virtual AttributeSetInstance PercentageModifiers { get; }
Property Value
TotalAttributePoints
Gets the total number of attribute points, including both available and spent points.
public int TotalAttributePoints { get; }
Property Value
- int
UseBaseAttributesFromClass
Gets or sets whether to use base attributes from the entity's class. If true, base attributes are derived from the EntityClass. If false, fixed base attributes defined in this component are used.
public bool UseBaseAttributesFromClass { get; set; }
Property Value
- bool
Methods
AddFlatModifier(AttributeSO, long)
Adds a flat modifier to an attribute.
public void AddFlatModifier(AttributeSO attribute, long value)
Parameters
attributeAttributeSOThe attribute to modify.
valuelongThe flat value to add.
AddPercentageModifier(AttributeSO, Percentage)
Adds a percentage modifier to an attribute.
public void AddPercentageModifier(AttributeSO attribute, Percentage value)
Parameters
attributeAttributeSOThe attribute to modify.
valuePercentageThe percentage value to add.
BeginBulk()
Opens a bulk-update bracket. While the bracket is open, all attribute-changed events are deferred: each affected attribute is recorded in a dirty set with its pre-mutation snapshot, and a single OnAttributeChanged event (plus callback) is raised per genuinely-changed attribute when the outermost bracket disposes. Nested brackets are supported; flush happens only on the outermost dispose.
public EntityAttributes.BulkUpdateScope BeginBulk()
Returns
Remarks
Use this around broad mutations such as level-up/down, class swap, or RefundAll to collapse what would otherwise be many events into one per affected attribute. The dirty set and snapshot dictionary are reused across brackets — no per-bracket heap allocation.
ComputeFinalAt(AttributeSO, int)
Computes the attribute's final value as it would be at the specified entity level, without touching the runtime cache. Used to snapshot old/new values around level changes and by scaling components that need per-level projections.
public long ComputeFinalAt(AttributeSO attribute, int level)
Parameters
attributeAttributeSOThe attribute to compute.
levelintThe level at which to evaluate the attribute.
Returns
- long
The clamped final value at the given level, or 0 if the component is disabled.
Remarks
Pure and cache-free: callers can interleave ComputeFinalAt(AttributeSO, int) with Get(AttributeSO) without poisoning cached state. Disabled components always return 0 for consistency with Get(AttributeSO).
Contains(AttributeSO)
Returns whether attribute is tracked by this component's attribute set.
public bool Contains(AttributeSO attribute)
Parameters
attributeAttributeSO
Returns
- bool
Get(AttributeSO)
Gets the final calculated value of a specific attribute. The final value includes base value, flat and percentage modifiers, and spent attribute points. The result is clamped to the attribute's min/max values.
public long Get(AttributeSO attribute)
Parameters
attributeAttributeSOThe attribute to retrieve.
Returns
- long
The final value of the attribute if the component is enabled (clamped to min/max values); otherwise, 0
GetAllSpentPoints()
Gets a dictionary containing the number of attribute points spent on each attribute.
public Dictionary<AttributeSO, int> GetAllSpentPoints()
Returns
- Dictionary<AttributeSO, int>
A dictionary where keys are attributes and values are the points spent on each attribute.
GetBase(AttributeSO)
Gets the base value of an attribute at the entity's current level. Base attributes don't consider flat or percentage modifiers, nor spent attribute points.
public long GetBase(AttributeSO attribute)
Parameters
attributeAttributeSOThe attribute to retrieve the base value for.
Returns
- long
The base value of the attribute (clamped to its min/max values) if enabled; otherwise, 0
GetSpentOn(AttributeSO)
Gets the number of attribute points spent on a specific attribute.
public int GetSpentOn(AttributeSO attribute)
Parameters
attributeAttributeSOThe attribute to check.
Returns
- int
The number of points spent on the attribute.
RefundAllSpentPoints()
Refunds all spent attribute points, making them available to spend again. Pre-snapshots each invested attribute's current value inside a bulk bracket, so the per-investment callbacks collapse into a single event per genuinely-changed attribute. Iterates the portfolio's zero-alloc enumerator to avoid copying the investments dictionary.
public void RefundAllSpentPoints()
RefundFrom(AttributeSO, int)
Refunds a certain amount of attribute points from a specific attribute. Event propagation is handled by the portfolio callback.
public void RefundFrom(AttributeSO attribute, int amount)
Parameters
attributeAttributeSOThe attribute to refund from.
amountintThe number of points to refund.
RefundPointsForAttribute(AttributeSO)
Refunds all spent attribute points for a specific attribute, making them available to spend again. Event propagation is handled by the portfolio callback.
public void RefundPointsForAttribute(AttributeSO attribute)
Parameters
attributeAttributeSOThe attribute to refund points for.
SetFixed(AttributeSO, long)
Sets a fixed base value for a specific attribute. This is only applicable when UseBaseAttributesFromClass is false.
public void SetFixed(AttributeSO attribute, long value)
Parameters
attributeAttributeSOThe attribute to set.
valuelongThe fixed base value to assign.
SpendOn(AttributeSO, int)
Spends a certain amount of attribute points on a specific attribute. Cache invalidation and change-event raising are driven by the portfolio's ElectricDrill.AstraRpgFramework.Attributes.PointsTracker.EntityPointsTracker.OnInvestmentChanged callback; the call to Get(AttributeSO) primes the cache so the callback handler can read the old value.
public void SpendOn(AttributeSO attribute, int amount)
Parameters
attributeAttributeSOThe attribute to increase.
amountintThe number of points to spend.
Subscribe<TEvent>(TEvent)
Registers gameEvent on the channel matching TEvent.
public void Subscribe<TEvent>(TEvent gameEvent) where TEvent : ScriptableObject
Parameters
gameEventTEvent
Type Parameters
TEvent
Exceptions
- ArgumentException
Thrown when no channel is registered for
TEvent.
TryGet(AttributeSO, out long)
Tries to get the final value of an attribute. Returns true if the attribute is contained in the attribute set; otherwise, false.
public bool TryGet(AttributeSO attribute, out long value)
Parameters
attributeAttributeSOThe attribute to get.
valuelongWhen this method returns, contains the final value of the attribute if the attribute is contained in the attribute set; otherwise, 0.
Returns
- bool
true if the attribute is contained in the attribute set; otherwise, false.
TryGetBase(AttributeSO, out long)
Tries to get the base value of an attribute (before any modifiers are applied). Returns true if the attribute is contained in the attribute set; otherwise, false.
public bool TryGetBase(AttributeSO attribute, out long value)
Parameters
attributeAttributeSOThe attribute to get the base value of.
valuelongWhen this method returns, contains the base value of the attribute if the attribute is contained in the attribute set; otherwise, 0.
Returns
- bool
true if the attribute is contained in the attribute set; otherwise, false.
Unsubscribe<TEvent>(TEvent)
Removes gameEvent from the channel matching TEvent.
public void Unsubscribe<TEvent>(TEvent gameEvent) where TEvent : ScriptableObject
Parameters
gameEventTEvent
Type Parameters
TEvent
Exceptions
- ArgumentException
Thrown when no channel is registered for
TEvent.
Events
OnAttributeChangedCallback
Entity-local C# event raised whenever an attribute value changes on this component. Fires alongside OnAttributeChanged (SO-based) for each attribute change. EntityStats subscribes to drive the attribute→stat cascade without scanning scaling SOs on every access.
public event Action<AttributeChangeInfo> OnAttributeChangedCallback
Event Type
- Action<AttributeChangeInfo>