I’m a little stumped though in terms of working out whether these two approaches can be combined. The finite state machine idea seems better suited to more dynamically changing environments where the agent has to react appropriately, whereas the decision trees seem better-suited for more proactive agents.
I’m not sure that many of us have written games complicated enough for decision trees - generally, a small series of if tests will be sufficient - but finite state machines seem simple enough.
Yes, I tend to keep an extra tab on the right called “Test”, where I run small programs (overriding the main program) to test functions like this, then I comment the Test tab out to run the main program
@ignatz@brookesi This is all so interesting, how do I implement state machines and decision trees? Are there existing projects with just the base functions I can peer at?
@archistudent You can implement a simple state machine by having a table with each key representing a single state, you can then “move” between states by simply setting the a state variable. eg
Enemy = Class()
function Enemy:init(stateMachine)
self.state = "init"
self.stateMachine = stateMachine
end
function Enemy:update()
self.stateMachine[self.state](self)
print("State = "..self.state)
end
local bobsStates = {
init = function(self) self.state = "waiting" end,
waiting = function(self) self.state = "moving" end,
moving = function(self) self.state= "attacking" end,
attacking = function(self) self.state = "waiting" end
-- etc
}
local bob = Enemy(bobsStates)
bob:update()