Answer the question
In order to leave comments, you need to log in
How to change state with different duration?
It is necessary to come up with an algorithm that switches state with different periodicity. The counter reaches a certain steps limit and switches to the next state, and then, having reached the last state, switches to the first one and starts all over again. It should work like this:
// state=1, 4 steps
update()
update()
update()
update()
// state=2, 2 steps
update()
update()
// state=3, 3 steps
update()
update()
update()
// state=1, 4 steps
update()
update()
update()
update()
// ...
states := []int{1, 1, 1, 1, 2, 2, 3, 3, 3} // 1, 2 и 3 состояния
for i := 0; ; i++ {
state := states[i%9]
}
Answer the question
In order to leave comments, you need to log in
Probably something like that? It works play.golang.org/p/Zo3jD2T8j8
type State struct {
state int
step int
states []int
}
func NewState(states []int) *State {
return &State{
0,
states[0],
states,
}
}
func (s *State) update() {
if s.step > 0 {
s.step--
fmt.Println("step=", s.step) //do something
} else {
s.changestate()
fmt.Println("state=", s.state) //do something else
}
}
func (s *State) changestate() {
if s.state < len(s.states)-1 {
s.state++
} else {
s.state = 0
}
s.step = s.states[s.state]
}
func main() {
st := NewState([]int{4, 2, 3, 4})
st.update()
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question