Make sure you use code tags friend: Using code tags properly
As for your question: if the objective of your agent isn’t to follow a sequence of events but rather adapt to a wide variety of situations based on needs, a vanilla state machine probably isn’t the ideal structure as it’s fairly rigid in its goal planning. I would do some reading on Goal Oriented Action Planning (GOAP) or any similar task-based AI.