How would you make energy regenerate using blueprints?

Hello,

I am fairly new to the Unreal Engine. I have been watching videos on Plural Sight and the official YouTube channel to understand blueprints better. I really love the idea of not having to code every little detail.

I would like to understand how to make energy (as shown in the Unreal Engine 4 documentation) regenerate. I have searched most of the internet but have yet to find something that does what I need it to do. If someone could please post screenshots of how to do this, it would be greatly appreciated.

Kind regards,

Felnaar

Hello Felnar!

Consider you have a variable ‘Energy’ as a number between 0 and 1 for simplicity’s sake. A method of forcing this variable to “Regenerate” from 0 back to 1 would be to add some code into the ‘Tick’ event of a Character blueprint.
The ‘Tick’ event is called every frame, and is useful for updating things that need to be updated every frame, but probably shouldn’t be used for everything.
Now that we have the basics covered, we can get to the good stuff.

43232-energy.png

When using the ‘Tick’ event we get a variable called ‘Delta Seconds’ back with it that basically tells us the amount of time that has passed since the last tick. This is very useful because ‘Tick’ isn’t perfectly uniform in it’s timing, and after a while, or depending on CPU load can be quite different in timing.
We are using this ‘Tick’ Function to run a short math equation adding the value of ‘Delta Seconds’ to our ‘Energy’ every time we ‘Tick’, we then limit the value of ‘Energy’ by using the ‘Min’ function, which basically picks the lower of the two values it is provided, ‘Energy’ is constantly changing and can actually be any value, but with Min we make sure it never goes over 1, or if it does, set it back to 1.
I recommend you find a different way to reduce your energy aside from pressing a button, but if you want you can try using a key press in your Player Controller blueprint for a simple debugging tool. Doing so will require setting the character’s energy through a reference to your character, which is a different task.

Hello Spiris,

Thank you so much! This is exactly what I required. However, I was wondering if there was a way to use the timeline (or whatever else would work) for this project. In another UE4 AnswerHub post a person was explaining how a timeline should always be used instead of the event tick. I require three regenerating nodes for Mana, Health, and Stamina. It does not appear you can duplicate the event tick properly. The event tick also seems to be a bit fast for what I require. As you have stated, and as I have read online, tick rate causes a bit of CPU lag.

I hope I’m not asking for too much.

Kind regards,

Felnaar

Not at all, your hunger for understanding will take you a long way.
You can use a timeline node for this sort of thing, and it is in some senses superior to tick, albeit a little more complicated, and possibly inferior in others. I will mock up an example for you in a little bit.
A few things to keep in mind:
You don’t need to duplicate the tick event, the blueprint nodes shown can be turned into a function and/or simply called alongside similar health, xp, or mana bars. So long as they are wired, they execute in order.
Also, though superior in some senses, a timeline node can have some drawbacks as well, it is important to strive for elegance in what you do, however when you are learning be sure to dabble a little bit with even seemingly less than optimal solutions, they can be rewarding even if not your ideal, and in the best case scenarios you can find a new ideal for your particular situation.
Will check back in soon.

So here is a little more complex of a solution, I will do my best to explain. We start by creating a timeline, which acts as a component of the blueprint we are using, in this example we are using the character blueprint.

When this timeline updates, we want our energy to regenerate, so we add a small value to it every time the timeline updates. These timelines are set to looping so they will update until stopped. Notice we add delta seconds to them, because like tick, we do not know when they last updated, World Delta Seconds is our fallback here, because it knows the time between the world’s last tick and it’s current tick, it may be off by a little bit regarding our timeline’s delta, but it will give us an acceptable value regardless. Also note the top value is being multiplied by a scalar, which may or may not be a good way for you to ‘slow down’ the regeneration.
You will also notice that I am updating two energy values each. This is not entirely necessary, but it is driving a value I have attached to a widget that tells me it is working as intended in my test environment, you may have something similar.
Finally, when the bar is full (equal to 1) I call Stop on my timeline, which is how we get improved performance over Tick, if we just let these things run forever, they will actually be considerably more expensive.

Now we can look at the PlayerController, just in case you want some debugging keys.

Here we take our default pawn, which is set to our character in our GameMode, and we set its energy, followed by a call to play its regen timeline, which starts the regeneration, these things could occur separately, in case you wanted a slight delay between loss and begin regen.
It is important to note that the get controlled pawn and cast node will only work if the controlled pawn of this player controller is the character we need to adjust the values on, otherwise we will need to grab a different reference somewhere in our code.

I hope this answered the heck out of your question. It gave me a chance to think about a part of game design I hadn’t thought about in a long while. =)

I am sorry it took so long to get a response. I went to bed right before you posted. I tested the original system (already having a way to lose mana, stamina, and health but it is always good to learn new things. This debugging feature will definitely help me in the future) and it did not seem to work. The only thing I did differently was the Set Mana Target node. I could not figure out how to make that.

Also, would stat bars be the same as ‘HUD Reference’? I took it directly out of the UMG guide in the documentation. If this is not the case, how would I go about making one? What is the difference? The mana/energy/health regen nodes do they have a special property? Or did I misunderstand something?

Thank you so much once again! Your help is getting my project ever closer to being completed.

I am not entirely certain what you are referring to for the debugging functionality. Perhaps you could elaborate?
My variable “Stat Bars” is a reference to a UMG widget that I created, it is not necessary for you to create these, but if you have a variable that allows you to visualize the data in a Widget or in your HUD, you should update that variable to represent your current health/mana/energy.
The health/mana/energy regen nodes are simply timeline components attached to your Character, you can access them so long as you have access to your character.
My player controller possesses a ‘Character With Energy’, so I am able to get it’s controlled pawn, then cast it to the object that has the variables and components I need access to.

Super awesome post I got it to work using the timeline like you suggested. I set a bool for is regenerating which I used to control the sequence node with a branch and I have this timeline called on the event any damage. That way it’s not firing on tick… I’m a newb but I feel like it’s starting to make sense!!!

If this system is used for stamina or mana drain, then what changes would be needed?
Also, the regeneration timelines fire up only one time, and not on repeat. Even when it is looped.