- Home /
A smarter way to create a state machine within a state machine
Hi all,
I have an interesting problem that has likely a better way to be solved then I am doing it.
Essentially consider that I have 2 enums.
enum matchStates
{
PRE_MATCH,
DURING_MATCH,
END_MATCH
}
;
enum inGameStates
{
STARTING,
SHOOTING,
SCORING,
FINISHING
}
;
Within my update loop I have a case statement to loop through the first set of matchStates.
switch (matchState) {
case matchStates.PRE_MATCH:
break;
case matchStates.DURING_MATCH:
timer -= Time.fixedDeltaTime;
if (timer <= 0) {
ExecuteInGameState();
timer = timePerTurn;
}
break;
case matchStates.END_MATCH:
break;
}
ExecuteInGameState is of course a function that....checks inGameState to see what 'sub action' I need to perform.
switch (inGameState) {
case inGameStates. STARTING:
Starting();
break;
case inGameStates.SHOOTING:
Shooting();
break;
case inGameStates. SCORING:
Scoring();
break;
case inGameStates. FINISHING:
Finishing();
break;
}
While this works fine it feels like a code smell so I am wanting to see what C# or unity pattern should be considered for such a problem - if such a thing exists. I know the animation engine has the concept of sub states which is what I have here, but when I look at the code, it just feels messy and makes the class look clunky.
Any thoughts?
Cheers
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Boss AI help 1 Answer
Best way/pattern to exchange messages between GUI elements (buttons, windows, etc) 1 Answer
Quick State Machine Question c# 1 Answer