There is a system in my game, where as you progress through the game as well as by gaining levels, certain abilities will unlock. However, I am having a hard time deciding if I should go about doing this with an array of booleans or if I should make individual booleans for each character and turn them on/off. However, I am not sure which is the most optimal or the most preferable. Or maybe there is an even better method, that I am not aware of. Was hoping to hear some feedback as to how some would approach this method.
Create a master Ability blueprint - an Actor Component. Encapsulate logic and variables all abilities share.
- enabled, stats, range, damage, duration and whatnot
- functions/events/methods for Use, Cooldown, Upgrade and so on
- visuals for the abilities
- anything else you need
Create a child for each ability providing they need unique functionality. Give the player character all abilities but keep them disabled. Enable as needed.
The following video is a foot in the door of the components how-to:
Huh, I’m assuming this is the much more optimal choice, instead of me storing away a bunch of booleans into ThirdPersonCharacter and activating them based on some criteria? And when you say encapsulate logic and variables all abilities share, do you mean have all the variables and logic be shared across all abilities?
Yes, that’s the core of OOP.
Creating a new ability will instantiate the variables and logic for each component. Each will have its own values, and can have its functionality triggered separately.
Surely, you’d rather avoid creating identical variables and writing the same code for 20 abilities.
Have a bool (isEnabled) in each component to figure out whether it can be used.
You could store it in the player’s blueprint, sure. You could pack it all into a single blueprint but you’ll be creating a lot of work for yourself in the long run.
Imagine what happens if you decide that each ability needs one extra variable. You create another 20 variables in the Player Character? Nah, go the the master ability blueprint all abilities inherit from and add 1 variable, compile. All abilities have 1 extra variable for you to fiddle with.
edit:
You can see inherited variable here:
So, ideally I should also have my character’s stats in a separate component as well? Should I also have damage calculation and my level up calculation in it as well? Sorry for all the questions, I am relatively new to how UE4 handles OOP. I am more used to RMXP and how it handles classes, so I’m still trying to get to grips with things, haha.
Think of components as chunks of reusable behaviour you can grant to any actor. Even if you have 5 completely unrelated classes that do need to share some kind of behaviour (like ExplodesOnDeath), having it in a component will simplify things. Components are about making things modular.
So, ideally I should also have my
character’s stats in a separate
component as well?
Are you going to re-use those stats anywhere else? If no, there’s probably no need to create another module to handle player stats. If multiple object classes need the same stats, you can wrap them in a GeneralStats component.
Should I also have damage calculation
and my level up calculation in it as
well?
If you’re going to need those in other blueprint classes then wrapping damage calculations in a component might be a good idea. Let’s say you’ve got 5 classes in your game:
- the player
- a vendor
- an enemy
- a questgiver NPC
- a trapped chest
These will vary quite dramatically when it comes to the in-game behaviour, but 1,3 & 5 will be dealing damage. You can give those three a DealDamage component which can pull the stats from the owning class’ GeneralStats and deal damage accordingly.
1, 2 & 3 can be given a LevelUp component that tracks the experience points (stored in GeneralStats) and increases the level of this class by pulling data out of a the appropriate DataTable.
You do not need to use components at all. You can use classes exclusively. But sprinkling it with component magic can make things easier.
I intend to have multiple characters have different stats, based on their play-style, so I am not entirely sure if I should have it be associated in there, rather than have it be in its home with the ThirdPersonCharacter. But, again, not entirely sure, since not every character will have the same growths and the same stats.
I can see all of that being super useful, but I am mostly just worried about my Event Dispatchers and how I’d call them in the components to do certain things. Because there were some aspects of my blueprints that made use of Event Dispatchers and now they don’t work in my Components.