Download

Character custom components and modular programming

Hello Unreal Engine enthusiasts,

I am relatively new to game programming and previously I was learning Unity3D to understand some basics.
In Unity3D I tend to structure my Character code in a modular kind of way, where each mechanics is a different script\component (e.g.: sprint, wall jump, shoot, and e.t.c.) that is added to the character Object and then accessed with a GetCompoent(“Sprint”) function, assuming “Sprint” is added to the object.

In UE4 I see there are various types of Components that I can create using C++ code, but I’ve been struggling to understand the correct workflow using multiple components to implement different mechanics for the Character.

Can anybody help me with some simple code examples please?

Here is some basic workflow that I am intended to use:

  1. Lets say there is a component “Sprint” that has a function “OnSprint()” that should be called by the “MainCharacter” class
  2. “MainCharacter” would then get reference (?) to each of the “Sprint” Component and call “OnSprint()” when needed.
  • How do I get this reference? Do I use GetComponents() function and loop through the array to find “Sprint”? Or is there a better way to do that.
  • Is there a better\different workflow to accomplish that?

Thanks.

i use a custom component for my ability system inside the constructor i set it up (this will be the reference you need to call into it int he rest of your code):

character .h


class UKTAbilityManager* AbilityManager;

.cpp inside your constructor or that AYourCharacter() thingy (i honestly don’t know its name)


AbilityManager = CreateDefaultSubobject<UKTAbilityManager>(TEXT("AbilityManager"));

then whenever i need to use an ability i just call into it:


AbilityManager->UseAbility()

i’m not the best programmer so second opinions would be advised this is just how i use my custom components