In my problem, I have a series of "states" I need to be able to iterate over. Each state holds a 2-dimensional array of objects. Each object has a boolean and an int, both initialized to random values. The next state depends on the current and, for any given state, there is only one possible next state.
Assuming the initial state is considered to be 0, how would I implement IEnumerator or IEnumerable (or both, or neither), in order to iterate over these states.
Ideally, I would be able to use syntax like this:
State++;
or:
if(State == State + 3) {
DoSomething();
}
Or maybe even use a slider to move between states.
I'm fairly new to Unity and C# and I have been beating my head against a wall trying to figure this out. I'm not even sure if these interfaces are the way to go, or if another method would be more appropriate. Any help is highly appreciated.
Below is a skeleton of what I already have. It seems the StateData class would be my IEnumerable(?) because it holds what I want to iterate over. This seems to fulfill IEnumerator.MoveNext(), though; so, I'm confused.
MonoState.cs
public class MonoState : MonoBehaviour {
public StateData currentState;
private void Start() {
// here's where I initialize the data.
}
private void Update() {
// here's where refresh my scene to match the data
}
}
StateData.cs
public class StateData {
ItemData[][] data;
public StateData(ItemData[][] initalState) {
data = initialState;
}
public ItemData[][] GetNextState() {
// here's where I calculate the next state
// and I assign it to data.
}
}
ItemData.cs
public class ItemData {
public bool Flag;
public int Value;
}