Making different weapons have different commands from inputs

This is more a conceptual question then a “hey is my code good?” one. I don’t want to go down a rabbit hole only to to scrap it later. Basically, brainstorming… Is this the ideal way to set this up for modularity and as tight without wasting space.

I’m trying to map out how I’d like this system to be set up, the most recent example I can give is Monster hunter wilds. Every weapon uses the same inputs but has vastly different responses to what each input does, not just movesets. Some weapons can block, some enter a morph state, or super mode. All these happen from pressing the same button but change based on the weapon equipped.

My current idea of how to do this is the following

  • Instead of an Input having a preset command, it uses a blueprint interface out of the event.
  • The weapon base class takes in the equipped weapon and finds the montage.
  • The montage has anim notify data to detect hitting, block times, etc.

    Side note: I think stage 2 can be clearer and smaller in code but I can’t figure out how off the top of my head

This is my current idea set up. There are somethings i’m trying to avoid. Like class calls which double the size of a file in memory as well as call for way to much information then is necessary. I’m going for the cleanest and simplest steps with modularity in mind.

This sounds like a perfect use-case for the Gameplay Ability System (GAS).

The learning curve can be a bit steep at the beginning, but if you allow yourself the time it will pay off in the long run. It’s a system that scales very well when you start adding more content to your game, and supports networking out of the box if that’s something you need.

It’s an Epic Games official plugin that’s used in games like Fortnite and other AAA titles, so pretty much any use-case you come up with will be possible to handle.

From what you’re describing, it sounds like you can create different GameplayAbilities for each weapon. All of them could inherit from a base GameplayAbility where you have the shared functionality (like the input config).

Here are some resources to get you started with GAS:

Official docs: Gameplay Ability System for Unreal Engine | Unreal Engine 5.5 Documentation | Epic Developer Community

Unofficial docs, but I recommend this as an excellent guide and go-to reference document which covers everything: GitHub - tranek/GASDocumentation: My understanding of Unreal Engine 5's GameplayAbilitySystem plugin with a simple multiplayer sample project.

Video tutorial: Getting Started with the Gameplay Ability System (GAS) in Unreal Engine 5.4 | Community tutorial

The way I have it in my game is I have the weapon stored in an “Actor object reference” type variable, so no need for casting, it announces itself to the player and then with an interface I call the function “Shoot”

No dependencies, size-map is nice and clean.

Sorry I was researching all of that. Unreal has a nack for adding things and just kinda going “well wouldn’t that be neat if it was common knowledge huh?”

Basically from what I gather it’s something like this

  • add “ability system” to base character component (after activating the plugin)
  • Make a “Gameplay ability” blueprint class like “guard”
  • Add the code you would normally for that ability or action in the character class in the gameplay ability instead
  • Then set up which abilities are attached to each weapon

If that’s the case… yeah that’s basically an extremely clean system.

Yes! That’s pretty much it.

You can also have, for example, a base GameplayAbility BP called “Shoot”, and then create some child BPs that inherit from it like “ShootPistol”, “ShootShotgun”, etc, so you can reuse the common logic, like executing an AnimMontage, blocking movement, etc.

Another handy feature is that for each GameplayAbility you can configure a GameplayTag that gets added to the owning character while the ability is active, and you can configure an ability to cancel or be blocked by tags from other abilities, which cleanly solves issues like “you can’t shoot while jumping”.

You can grant and remove abilities on the fly via the AbilitySystemComponent attached to the characters (for example when switching weapons). As you dig deeper, you’ll find a lot of useful features like these ones.