I'm trying to learn the Command design pattern and apply it to the game I'm working on. First I read about the general implementation, and I feel like I understand it pretty well. Now I want to know how to use it in games. I started reading this book about design patterns in games and in the section about the command pattern the author says:
We can use this same command pattern as the interface between the AI engine and the actors; the AI code simply emits Command objects.
The decoupling here between the AI that selects commands and the actor code that performs them gives us a lot of flexibility. We can use different AI modules for different actors.
I'm trying to figure out how this pattern would be used for game AI in practice. I assume there would be an
AIEngine class or something like that with
GenerateCommand() function. Then the
GameActor would ask the
AIEngine for a
Command whenever it finishes executing the previous one.
What I don't understand is how exactly it would work. In order to generate commands the
AIEngine needs to keep track of all the
GameActor objects anyway (or at least of the one it's generating the commands for), so what's the point of using commands? At this point the
AIEngine could just manage the
GameActor objects directly.
I've been trying trying to wrap my head around this for quite a while. If someone could write a example implementation of the
AIEngine interfacing with
GameActor it would help me a lot. I don't think I'm able to understand it without seeing some code.