You can use call() or just parenthesis, but I prefer call as it is more explicit.
Example:
class BasicMachine(StateMachine):
def __init__(self):
super().__init__()
self.add_state("walk", self.walk)
self.add_state("static", self.static)
self.add_state("run", self.run)
self.position = type(FakeVector, (), dict(x=0.0,y=0.0,z=0.0))
self.velocity = type(FakeVector, (), dict(x=1.0,y=1.0,z=1.0))
def set_state(self, state):
self.current_state = state
def walk(self):
self.velocity.y = 0.1
def run(self):
self.velocity.y = 0.2
def static(self):
self.velocity.z = 0.05
def move(self):
self.position.y += self.velocity.y
self.position.x += self.velocity.x
self.position.z += self.velocity.z
self.velocity.x = 0.0
self.velocity.y = 0.0
self.velocity.z = 0.0
class TestMachine(StateMachine):
def __init__(self):
super().__init__()
self.add_state("walk", self.walk)
self.add_state("static", self.static)
self.add_state("run", self.run)
self.add_transition(walk, static, self.is_static)
self.add_transition(walk, run, self.is_running)
self.add_transition(static, run, self.is_running)
self.add_transition(static, walk, self.is_walking)
self.add_transition(run, walk, self.is_walking)
self.add_transition(run, static, self.is_static)
self.position = type(FakeVector, (), dict(x=0.0,y=0.0,z=0.0))
self.velocity = type(FakeVector, (), dict(x=1.0,y=1.0,z=1.0))
self._state = None
def set_state(self, state):
self._state = state
def is_static(self):
return self._state == "Static"
def is_running(self):
return self._state == "Running"
def is_walking(self):
return self._state == "Walking"
def walk(self):
self.velocity.y = 0.1
def run(self):
self.velocity.y = 0.2
def static(self):
self.velocity.z = 0.05
def move(self):
self.position.y += self.velocity.y
self.position.x += self.velocity.x
self.position.z += self.velocity.z
self.velocity.x = 0.0
self.velocity.y = 0.0
self.velocity.z = 0.0
The second example is basically the first example. You don’t need to do the tests because you can just set the state yourself, but the point is that you can use custom conditions for transitions.