Ambiera ForumDiscussions, Help and Support. |
|
|
|||||
|
My previous version of AI was made, pretty much, entirely out of "if-else" statements. From what I know, it's not a particularly good approach to making AI for enemies. So, instead, I tried searching for what a "state machine" is, and after finding pretty much nothing (if certain examples based on some library that is definetly not present in Coppercube), I decided to make... Somewhat of a state machine. For that, I used "switch" loop, and a formula that would declare the state. The whole reason why I am asking this question is, firstly, the formula itself being "quite lengthy", and, secondly, because I already heard about one guy who made pretty much an entire game with "if-else", and it wasn't really great. This is how formula looks: if (HealthDif < 0)There are (currently) six states - 0 for being dead; 1 for, basicaly, being alive and detecting player in front; 2 for reacting to sounds (only shooting right now); 3 for aiming; 4 for shooting; 5 for following player. I am a bit worried because amount of calculations seem to be quite heavy, I don't know if this even is more optimized than a bunch of "if-else"-s. So... if there are some people who understand this subject, is this approach better than making a lot of "if-else"-s (I believe, I mentioned this a lot already), or am I being, to put it more culturally, "not smart enough"? Edit: it seems like forum cut the name of this thread. It was saying something like 'Is using "switch" better than "if-else" for AI?'. Weird.
|
||||
|
Hi dekon_17, I believe, it's better to use a state machine. You can find a nice video explaining how it works here:https://youtu.be/_Hv9eyero6o. In the video this guy uses TS, but if I remember correctly he pretty much goes through the whole implementation. I also rewrote the same code to use with coppercube at some point. It should be working, but I'm not 100% sure, since I modified it a lot after that for specific games. This one should do the same as video tutorial. function StateMachine() {
|
||||
|
In order to use it, you inherit your behavior class from it:
If you never used this before you can read about ES5 inheritance here:https://eli.thegreenplace.net/20... You also need Object.createa polyfill to use in coppercube desktop:
|
||||
|
Now you add states to your behavior class:
Remember to call StateMachine.call(this) - I believe, the article about inheritance contains detailed information why it is needed. Set initial state and call update() method inside your on animate:
Each of your states is a separate method:
|
||||
|
If you need some additional actions then state machine enters or exists some specific step you can redefine onEnter and onExit methods for each state. You switch to another state like:
Assuming you're switching from 'idle' to 'attack' state, it will execute idle on exit method once, than attack on enter method once, and than will call attack on update method every frame. I believe, that is the cleanest approach that you can use. |
||||
|
Woah. Didn't expect that state machines can be this big (my code right now is 278 lines, that includes pathfinding, which takes almost a third of that, and 4 functions besides required one - "onAnimate"). I will try to do something like that, but I can't say if I will be able to get this done. I also suppose that it might be bigger than what I have now. However, as I said, "I suppose", so, I am not sure about all that. Regardless, thanks for information, okeoke, couldn't find all that before on my own. |
||||
|
Well, I've heard that too, that a bunch of IF structures are deemed amateurish. But I don't agree. A lot of academic coders declare some coding standards, but never code anything by themselves, Or then it's bugged bloatware. Sure bad code can contain lots of IFs, but so can good code. The point is, a state machine does not exclude IF structures at all, but they make it more effective. Trying to squeeze all functionality into a single formula is overcomplicating the task in my view. What is much more important in a state machine, is a priority system, that switches states, based on a "current mode" code and its priority, that works more like the charts, rather than like nested IF structures, that are hard to maintain. But you can still use a ton of IFs in order to attest a certain mode to a certain NPC. But that's just a number. The respective actions are then executed in the state machine based on the current mode alone, which is just a number, a code, like I said. It helps to keep the actions structured, while allowing wide-spread, unstructured mode influencers. Just my opinion. |
||||
|
|