Creating a component
Last updated
Last updated
This page explains how to create a new component for Motion to extend Motion to your needs. In this example we will create a Health Component which adds a Health value to the Motion Character which we can then use to implement fall damage.
To begin, we need to create a new Actor Component by right-clicking in the Content Browser and selecting Blueprint Class.
In the modal that opens, scroll down and select BP_BaseCharacterCurvedComponent.
Name it BP_HealthComponent.
Double-clicking it will show you the empty Event Graph.
Since the Character Blueprint already comes with a Event Dispatcher for OnPlayerLanded
we can just subscribe to that!
To do so, get the Character Core Component
and from there the Character
. Drag the Character
reference, create a Bind Event to On Player Landed
node and createa a new custom Event to it.
Then create a new function named HandleOnPlayerLanded
. Inside this function we will place the code that is executed when the event fires.
Connect it to the EventOnPlayerLanded
node.
Now our component is ready for testing! To test if our Event Handler is actually called, add a Print String
node inside of Handle on Player Landed
.
For our new component to run we simply need to add it to our Character Blueprint. Open BP_MotionCharacter
, press Add Component
in the Components tab, search for the new Health Component and select it.
That's it already! Now to test if the previously added Print String
node is actually called, hit the Play button in your editor's toolbar and jump. When landing, a message should appear on your screen.
This example will only cover a simple example solution for damage. Feel free to adjust and extended the logic as needed for your project.
Add a variable of Type Float
and name it Health
. Set it's default value to 100 in the Details
tab and then replace the code inside of Handle on Player Landed
with the following code:
Create a new variable of Type Boolean
called bShowDebugInformation
. Set it`s default value to True
and create a new function Print Debug Information
and add the following code:
Then call it in Event Tick
as follows:
Hit the Play button in your editor's toolbar again and it should look like this now when you jump and land:
You have successfully created a component for Motion! Congratulations!