Class ParticleAttribute<T>
Representation of an attribute that is part of a particle's state.
ParticleAlgorithm subclasses should use instances of ParticleAttribute<T> to represent their state variables. Only these attributes are displayed and editable in the simulation UI and recorded in a simulation history. They also provide correct read and write behavior in synchronous rounds, i.e., a ParticleAttribute<T> provides its most recently written value only to the particle it belongs to, and it always returns the value from the previous round to other particles.
ParticleAttribute<T>s are created using the factory
methods provided in the ParticleAlgorithm base class
(see CreateAttributeInt(string, int) etc.).
For example, an integer attribute is declared and initialized with the
value 3
as follows:
public class MyAlgorithm : ParticleAlgorithm {
public ParticleAttribute<int> myAttr;
public MyAlgorithm(Particle p) : base(p) {
myAttr = CreateAttributeInt("Fancy display name", 3);
}
}
Display names of attributes must be unique because they are used to
identify attributes when saving and loading simulation states.
Note the difference between the GetValue() and
GetCurrentValue() methods. Reading a ParticleAttribute<T>
like a variable of type T
will return the same value as
GetValue(). Depending on the desired semantics, it may be helpful to
wrap attributes in properties when writing a particle algorithm.
Namespace: AS2.Sim
Assembly: .dll
Syntax
public abstract class ParticleAttribute<T> : ParticleAttributeBase
Type Parameters
Name | Description |
---|---|
T | The type of values the attribute stores. |
Constructors
| Edit this page View SourceParticleAttribute(Particle, string)
Declaration
public ParticleAttribute(Particle particle, string name)
Parameters
Type | Name | Description |
---|---|---|
Particle | particle | |
string | name |
Methods
| Edit this page View SourceEquals(ParticleAttribute<T>)
Indicates whether the current object is equal to another object of the same type.
Declaration
public abstract bool Equals(ParticleAttribute<T> other)
Parameters
Type | Name | Description |
---|---|---|
ParticleAttribute<T> | other | An object to compare with this object. |
Returns
Type | Description |
---|---|
bool | true if the current object is equal to the |
Equals(object)
Determines whether the specified object is equal to the current object.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The object to compare with the current object. |
Returns
Type | Description |
---|---|
bool | true if the specified object is equal to the current object; otherwise, false. |
Overrides
GetCurrentValue()
Returns the latest value of this attribute.
The return value changes based on value assignments made within this activation. Use GetValue() to get the attribute's value at the beginning of the current round.
Declaration
public abstract T GetCurrentValue()
Returns
Type | Description |
---|---|
T | The latest value of this attribute. |
GetHashCode()
Serves as the default hash function.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
int | A hash code for the current object. |
Overrides
GetValue()
Returns the attribute's value from the snapshot taken at the beginning of the current round.
The return value is not changed by assigning new values to this attribute! To get the latest assigned value, use GetCurrentValue().
Declaration
public abstract T GetValue()
Returns
Type | Description |
---|---|
T | The attribute value at the beginning of the current round. |
SetValue(T)
Assigns the given value to this attribute.
Note that this value will only be visible to other particles in the next round.
Declaration
public abstract void SetValue(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | The new value assigned to this attribute. |
Operators
| Edit this page View Sourceoperator ==(ParticleAttribute<T>, ParticleAttribute<T>)
Declaration
public static bool operator ==(ParticleAttribute<T> a1, ParticleAttribute<T> a2)
Parameters
Type | Name | Description |
---|---|---|
ParticleAttribute<T> | a1 | |
ParticleAttribute<T> | a2 |
Returns
Type | Description |
---|---|
bool |
implicit operator T(ParticleAttribute<T>)
Declaration
public static implicit operator T(ParticleAttribute<T> attr)
Parameters
Type | Name | Description |
---|---|---|
ParticleAttribute<T> | attr |
Returns
Type | Description |
---|---|
T |
operator !=(ParticleAttribute<T>, ParticleAttribute<T>)
Declaration
public static bool operator !=(ParticleAttribute<T> a1, ParticleAttribute<T> a2)
Parameters
Type | Name | Description |
---|---|---|
ParticleAttribute<T> | a1 | |
ParticleAttribute<T> | a2 |
Returns
Type | Description |
---|---|
bool |