State: Difference between revisions
Tayfunwiki (talk | contribs) (Created page with "== <code>State</code> Property in Actor Class == === Overview === The <code>State</code> property in the <code>Actor</code> class is responsible for indicating the current state of an actor within the game, specifying whether they are alive (Active) or dead (Passive). === Syntax === <syntaxhighlight lang="c#"> /// <summary> /// Gets the state of the actor, either Passive (Dead) or Active (Alive). /// </summary> public IState State { get; protected set; } </syntaxhighli...") |
Tayfunwiki (talk | contribs) No edit summary |
||
Line 23: | Line 23: | ||
==== Example: ==== | ==== Example: ==== | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
if (someActor.State == IState.Passive) { | public class SomeClass : MonoBehaviour { | ||
public Actor someActor; | |||
} else if (someActor.State == IState.Active) { | |||
void Start() { | |||
if (someActor.State == IState.Passive) { | |||
// Handle the logic for a dead actor | |||
} else if (someActor.State == IState.Active) { | |||
// Handle the logic for a living actor | |||
} | |||
} | |||
} | } | ||
Latest revision as of 05:57, 12 January 2024
State
Property in Actor Class
Overview
The State
property in the Actor
class is responsible for indicating the current state of an actor within the game, specifying whether they are alive (Active) or dead (Passive).
Syntax
/// <summary>
/// Gets the state of the actor, either Passive (Dead) or Active (Alive).
/// </summary>
public IState State { get; protected set; }
Description
- Property Type:
IState
. This property uses theIState
enumeration to represent the state of an actor. - Accessibility: Publicly readable but can only be modified within the
Actor
class or its subclasses, ensuring controlled state management. - Values: The property can hold either
IState.Active
orIState.Passive
, corresponding to alive or dead states of the actor.
Usage
The State
property is primarily used to track and control the state of an actor in game scenarios. It can be queried to determine the actor's state and make decisions based on whether the actor is alive or dead.
Example:
public class SomeClass : MonoBehaviour {
public Actor someActor;
void Start() {
if (someActor.State == IState.Passive) {
// Handle the logic for a dead actor
} else if (someActor.State == IState.Active) {
// Handle the logic for a living actor
}
}
}
Remarks
- The protected setter in
State
ensures that the actor's state can only be changed by the actor itself or its derived classes, providing encapsulation and preventing unauthorized state changes. - This property is crucial for game mechanics that depend on the actor's life status, such as spawning, interactions, and gameplay effects.