This is my tutorial for creating a fully functional experience system based on the tutorial I used in UDK. This system was originally created, as far as I can tell, by MKTWO in the following thread: Experience-system - Epic Games Forums. I cannot claim this to be original work but just adapted from this person.
Please be aware this took quite a bit of effort to write out and is only the second tutorial like document I’ve written. If I have made mistakes please let me know and I will do my best to fix them.
Before we start, I did hijack a couple things from the content example pack from the HUD level. You will need to **MIGRATE **the following items unless you know how to work material editor, which I don’t really yet. I highly suggest watching Unreal’s Youtube videos for materials found here: https://www.youtube.com/playlist?list=PLZlv_N0_O1gaCL2XjKluO7N2Pmmw9pvhE
Items to migrate:
-
T_HUD_Bar_Generic (Texture2D)
-
Health_Bar (Material)
-
Change the name of this file once you migrate it to EXP_BAR so not be confused by later references
You will need to alter the exp bar material just slightly but it should be easy enough.
When you open the material editor move to the far left and highlight the top parameter going to the CLAMP function and change its Parameter name to “Gathered EXP”
Change the parameter below that one to “Needed EXP”
Then you can set the two colors along the top to whatever colors you want your experience bar to be. The color on the left is the Current value of the bar while the other color is the missing value of the bar. You can get kind of creative here and make all sorts of combos. (great for doing Health, Mana, Experience, or whatever other bar needs to show on screen)
Last but not least, set the two texture samples in the bottom middle to the other material you migrated over (T_HUD_Bar_Generic)
Interface
For this project the only interface we will need is very basic.
Right click in the content browser.
Select Blueprint interface under the miscellaneous selection
-
Mine is named Experience
Once you open it, create a new function (Get Player Experience)
Create the following variables in the Outputs category (note they are all integers)
-
EXP IN – This is how much experience you are feeding into the bar
-
XP to Current Level
-
XP Gathered for Next Level
-
XP Required for Next Level
-
Level
-
Max Level
That should do it for now though we will revisit these variables in a bit.
HUD
Create a hud.
-
Right click content browser, create new blueprint, search the custom classes for HUD.
-
I named it MyHUD though name it whatever you’d like.
Assign your game blueprint to use this HUD.
-
Open MyGame BP and assign it there.
Create the following variables inside the HUD (Type, Name)
-
Vector 2D – Screen Dimensions
-
Bool – Menu Open
-
Object’MaterialinstanceDynamic’ – Experience Bar
In the construction script (Missing this step made me take forever to figure this whole thing out)
-
Set up the following graph. This is just setting the material we will use for the bar itself
In the event graph let’s set up the first part of the nodes. This just gets us the users screen size for later reference. The branch is not used in this tutorial but would be used in another project to pause the game if a menu was open. Since I plan to add that functionality later I included it in this tutorial.
For the next step, we need to enable the interface we created earlier.
-
Click Blueprint Props below the menu bar along the top of the screen.
-
In the bottom left under the interfaces section click Add -> Experience_C
For whatever reason at this point I had to close / re-open my project to get the interface to populate right. May have to do the same.
-
NOTE: these will not work as intended yet since we have not set their values, we will do that when we visit our Pawn B
This next part gets kind of annoy because of where you have to drag your points from.
Here is how it should look when the HUD is all said and done.
These steps are just to help set up the above image.
-
When you try to get the “Get Player Experience” Function, please make sure if you are searching for it from the node menu that you select the one under the Interface Messages category. If you do not, you will not be able to correctly set the target to “Get player character”
-
Set your Experience bar on the black board and drag from it to get the correct “Set Scalar Parameter value”
Get Player Experience
-
This is pretty easy, this gets the values we will assign in our pawn BP and passes them into our HUD BP for use.
Set Scalar Parameter Value 1
-
This finds the experience bar dynamic material we set in the construction script
-
Then it gets the parameter name we set for how much exp our pawn has
-
Then it gets the value from our interface
Set Scalar Parameter Value 2
-
Same as before but it grabs the parameter name and value for how much exp our pawn still needs
Draw Material Simple
-
Material – this gets the material we defined in the material editor earlier that we migrated from the other project
-
Screen Y – -20
-
Screen H – 30
Draw Text
-
Text – Kind of long winded but just makes it show your experience nicely centered in the middle of your experience bar.
-
Color – I suggest setting it to something that stands out against the colors you chose for your experience bar.
-
Screen X – Dividing it by 2 centers it on your screen
-
Screen Y - -20
That should do it for this section
Player Pawn (MyCharacter)
Enable the interface on this blueprint
Create the following variables:
-
EXP
-
XP to Current Level
-
XP Gathered for Next Level
-
XP Required for Next Level
-
Level (0)
-
Max Level (100)
-
EXP Increment (500)
-
Only EXP Increment is a float, the rest are integers.
-
Create the following function or macros, whichever you prefer to use.
-
Give Experience – We call this whenever the player should be given experience
-
Inputs (Type,Name)
-
Integer – EXP IN
-
Calculate Level Progress – has the pawn obtained enough experience for a level?
-
Level Up – Ding
Open the interface from the variables menu
-
Tie all your variables to their respective node.
Functions
Give Experience
Set your EXP to the amount of EXP you just earned
Calculate your level progress
After calculating your experience, check to see if you are not max level and if you are above the required experience amount to level.
If you meet the conditions, level up.
If you have leveled, recheck level progress
Print a level up message
Calculate level progress
Set your XP to current level
-
This block serves to make sure that if you’ve leveled, your experience resets to 0 and you start again. If you bypass this node you’re experience becomes cumulative. Based on the EXP system you want, you may like that, though you’ll need to change how much you increment your levels.
Set how much exp you have gathered.
Set the required amount of experience needed for your next level
Level Up
Level +1
Spawn an emitter to create an explosion – just the minimum required flair to get your attention
Now all that’s left is to create a trigger to add experience to your pawn through the Give Experience function and you’re good to go.
Cheers,
Hakabane
PS: A really easy way to your system at work is to just create the below in your character BP