I've changed my strategies!

Published December 05, 2022
Advertisement

Last time I wrote about how I implemented the strategy pattern into my state machine to allow for customized behavior that was easy to change and maintain.
(You can ready more about that by clicking here if you want).
Well, since then I have added more and more desired behavior into my Game Design Document, and I realized that the strategy pattern wouldn't cut it anymore.

So I broke the code into small chunks and created a behavior tree.
A behavior tree will allow me to have very complex behavior that is still fairly easy to maintain (although it is quite intensive to set up).
(This article describes behavior trees very well).

I still have my state machine, but it uses different behavior trees as per my wish (if that's a sentence I can use).
And since each node can take parameters, I can actually have a friendly NPC and an enemy NPC use the same behavior tree, but they act differently because of the parameters set.
The player would never know they use the same code base.

Furthermore, different variations of the enemy can use different behavior trees that will allow for more variation in the behaviors, making the game feel more alive.

I'm not an expert programmer. By far.
But I love modular code that is easy to maintain and expand upon, following the SOLID principle whenever possible.
And I'm having SO MUCH fun with this - and after all, that's why we want to make games in the first place, isn't it?

I'd love to hear about your experience with behavior trees, state machines and the strategy pattern - and if you have any preferences.

Oh, and I'm really looking forward to the day I can show some functioning demo of the game! ?

1 likes 6 comments

Comments

WildWorldGames

Nice work! Behavior trees are hard as hell to implement but definitely a lot more reliable than state machine when it comes to AI

December 06, 2022 07:39 PM
AndyPett

@RamiMorrar Thanks! ? Yeah, they require “holding your tongue straight in your mouth” as we say in Norway. Of course I could have implemented the same functionality by coding behavior directly into each state, but then I'd have to make different states for different NPC's and different Enemies as the game grew larger, which would end up with a lot of code repetition. This way, behavior is coded in one place and used by many. ?

December 06, 2022 07:47 PM
WildWorldGames

If you have multiple different NPC types, I could definitely see the need for it. A lot less many than implementing the state machine.

December 07, 2022 11:12 AM
AndyPett

@RamiMorrar Absolutely. And the groundwork I'm laying now should be reusable for the next project so that more time can be spent making each game better than the last in stead of doing the same thing next time (assuming the next game will have a lot of the same functionality, of course).

December 07, 2022 05:47 PM
Simitus

It sounds interesting, but potentially, very computationally expensive. Depends on the operations you're doing in each node of the tree.. if all you're doing is some > or < operations or some boolean state checks then those go quickly. I could see a threat response tree for example whose parent node is a detection event, then compares the two entities relative strength, estimates which is likelier to win, weighs risk vs reward and decides to either fight or run. I don't think it would be necessary to re-evaluate the outcome of that tree computation unless one of the involved entities changes state (ie, HP is lower) or the threat analysis changes (another enemy enters the battle).

January 04, 2023 12:05 AM
AndyPett

@Simitus Actually, since this post I evolved further and decided to go for a utility system in stead. A behavior tree was a step up, and it's very good for making certain kinds of decisions. But a utility system where you have a list of all potential actions, and each action has a set of considerations, suddenly you have a very intelligent system that is very dynamic, easy to extend and it's all based on how you score certain decisions and actions. So the game designers can have a high degree of control, while also creating very complex behavior.

And yes, making my code as computationally cheap as possible is something I'm constantly learning and improving on.
While I actually do have a degree in computer programming, and have been creating different kinds of software and apps for years, I have never before had to think about the resources the way you have to when making a game.

January 06, 2023 08:40 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement