Command Pattern and benefit

Hi there, I have a question regarding the Command Pattern in UE5.

For some context, I am working on a hobby project—a turn-based tactical game where two teams fight each other. I am at the stage where I need to create a system that allows the player controller and the AI to perform various actions required for their turn, such as moving a unit to a specific point or shooting at a target. In my research, I came across the Command Pattern.

From my understanding, to use the Command Pattern correctly, one would create an abstract class or an interface for a command object. Child classes can then be created from this base class to issue specific commands, such as Move. The command’s execute function can be written within these child classes, allowing both the Player Controller and the AI to utilize the same function.

My question is, couldn’t I simply use a FunctionLibrary to call the command when needed? Creating a class just to use a function seems like an unnecessary extra step. I understand that allowing players to configure their input could be a reason to use this pattern, but Unreal Engine’s Enhanced Input system already handles this.

So, my question is: did I miss another benefit of creating a command abstract class instead of using a FunctionLibrary? Thank you for any clarification you can provide.

Well, it depends.

I’ve used simple function based system for turn based game, but at some point I realized I need to queue different actions and then manage them if something changed during turns.

For example, you queued different actions from 5 soldiers that want to attack some poor soul. What if target is dead already? Well, you change your target. What if there is no more targets? What if you tried to heal your ally, yet it’s dead now? Maybe you want to resurrect instead!

And if command pattern is used, each command is self sufficient and have all necessary data to be resolved, check what requirements must be met for success, set rules when it’s finished and etc

Basically, it gives you a lot of flexibility and reduce hardcoding. Maybe if your system is simple enough it’s a bit of overkill, but in my game it just made further development easier

1 Like

Alright, thank you for your answer. I will simply try it. After all, this project exists anyway to try new stuff.

1 Like