C
C#6mo ago
Ninja Kirby

should state machine have ability to add/remove

are state machines meant to be dynamic? lets say i got deez:
c#
public class State<TArgs>
{
public event EventHandler<TArgs> OnEnter;

public void Enter(object sender, TArgs e)
{
OnEnter?.Invoke(sender, e);
}

public event EventHandler<TArgs> OnExit;

public void Exit(object sender, TArgs e)
{
OnExit?.Invoke(sender, e);
}
}

public class StateMachine<TArgs>
{
private State<TArgs> _currentState;

public State<TArgs> CurrentState
{
get
{
return _currentState;
}
set
{

if (_currentState != value)
{
if (_currentState is not null)
{
_currentState.Exit(this, StateArguments);
}

value.Enter(this, StateArguments);

_currentState = value;
}

}
}

//adding/removing omitted for brevity :trolol:
}
c#
public class State<TArgs>
{
public event EventHandler<TArgs> OnEnter;

public void Enter(object sender, TArgs e)
{
OnEnter?.Invoke(sender, e);
}

public event EventHandler<TArgs> OnExit;

public void Exit(object sender, TArgs e)
{
OnExit?.Invoke(sender, e);
}
}

public class StateMachine<TArgs>
{
private State<TArgs> _currentState;

public State<TArgs> CurrentState
{
get
{
return _currentState;
}
set
{

if (_currentState != value)
{
if (_currentState is not null)
{
_currentState.Exit(this, StateArguments);
}

value.Enter(this, StateArguments);

_currentState = value;
}

}
}

//adding/removing omitted for brevity :trolol:
}
2 Replies
HtmlCompiler
HtmlCompiler6mo ago
do you mean add/remove in the sense of chaining methods, or something else
LasseVK
LasseVK6mo ago
It depends entirely on your definition of what "a state machine" is. On one hand, it could be a static "machine" that specifies exactly what happens if you do X in state Y. On the other hand, it could be a framework that allows you to create state machines, where you can specify what happens if you do X in state Y. In the first case, being able to modify it after the fact is probably not a feature. In the second case, probably it is.
Want results from more Discord servers?
Add your server