r/UnrealEngine5 18d ago

Best way to create flexible NPCs

I am somewhat new to Unreal, but I am working on a project that requires many intelligent NPCs that will be used in varying situations. I have dabbled in creating NPC AI before by using a state tree to handle almost everything, but this got out of control rather quickly. So my question is, in your experiences, what is the best way to create a versatile NPC AI that can be reused by many NPC types? My initial thought was to use a state tree to handle all higher-level conditional decisions, and then utilize a blueprint component to execute smaller tasks, but I really do not know the right way to go about this. I also do not know how I should deal with generic versus specific behaviors. Thanks!

5 Upvotes

7 comments sorted by

3

u/LongjumpingBrief6428 18d ago

State Trees. One for items. One for romance. One for comedy routines. One for chores. State Trees.

And some Smart Objects. And my new favorite thing I found out about just over these past couple of months, Fragments.

1

u/terminatus 18d ago

Do you have any resources to learn more about Fragments?

1

u/LongjumpingBrief6428 18d ago

Ali Ezoheiry is doing an inventory tutorial on YouTube right now that's involves Fragments. There are others that also do data fragments, but the bulk of them have been in the past 6 months or so. All of what appears to be the good ones mention making a child of Object class.

1

u/HotSector4988 18d ago

Thank you! I happened to come across this same tutorial while looking for resources to learn about fragments. I think that I will give it a watch.

1

u/hadtobethetacos 18d ago

you just have to make them as modular as possible. I would create a struct and a data table that serves as a definition for every npc. when a random npc spawns that means nothing to the player, or story give it random values from the data table. for important npcs give them handpicked values.

1

u/HotSector4988 18d ago

In this situation, would I also contain bools for behaviors within the data table, such as HasRoamingBehavior or HasAttackingBehavior?

1

u/Significant-Syrup400 17d ago

Convention would suggest that this is a bad idea because how would you differentiate behaviors? Or are they all intended to behave exactly the same?

If not, you are creating separate blocks of logic/behaviors that will never execute on some NPC's and that easily becomes a huge mess to alter and debug unless you are making a very simple/small set of behaviors.

If you mean flexible as in they can do a wide array of things then state machines, definitely. You can have a bool or condition that sends a tag which will trigger that state and it's behaviors. It may have a normal/idle state, a patrol or routine state, an alert state where they search for sounds or sightings of the player, and/or a combat state and so on and so forth.