I need help with loops?

Im trying to make a health thing for an fps and im trying to make it say “if health is less than 1 wait 5 seconds then add .01 health every .1 seconds until health equals 1” but it keeps infinite looping this image is what i have so far

Hey there,
I think you’re trying to over complicate it with loops. I have made a simple setup that should work for you. Made some comments above the notes, hopefully it will be of some use to you.

What happens here is when you press X you will wait 5 seconds then your health will start regenerating 1% every 0.2 seconds.
Setting the bool in the first branch will prevent from health regen being run more then once at any time, you have to wait until you are fully regenerated, at which point the bool is set to false again.
The rest is just a simple CustomEvent running over (looping) unless your health has fully regenerated.

[How it looks in game (YouTube)][2]

i dont want you to have to press anything i want it to auto regenerate, my end goal is “i want it to be if health drops below 100% wait 5 seconds and start healing 1% every .25 seconds, but if damage is taken while regenerating, stop and wait another 5 seconds to start regenerating… if health = 100% stop regenerating”

what you put wont mix with my end goal.

i dont want you to have to press
anything i want it to auto regenerate,
my end goal is “i want it to be if
health drops below 100% wait 5 seconds
and start healing 1% every .25
seconds, but if damage is taken while
regenerating, stop and wait another 5
seconds to start regenerating… if
health = 100% stop regenerating”

This has worked well for me in the past:

It waits for 3s to begin replenishing health and resets if more damage is dealt while regenerating or waiting to regenerate. You can adjust the percentage via the HealthRegen and Time interval is on the Set Timer node. The Retriggerable Delay controls regen cooldown.

Sorry to hear you didn’t find that useful. This was just an example, and if you would take another look at code and then read your comment you should know the answer. Instead of using X key as a trigger use Event Any Damage.
As long as you are taking damage the counter will reset to 5 and once you stop receiving damage and counter will run down to 0 your health will start regenerating at 0.01/s until fully restored or if you take damage again.

I have just made this in blueprints.

This is how I would do it.

I just realized I can remove the Blueprint grid in Editor Preferences → Appearance. That’s nice.

im new to blueprinting and this is confusing, how would i do this if i literally just made health as a variable

what is “set”

Assigning value to a variable.

That’s nice.

It made me obsess less about alignment ;p

like wth is event any damage

It is an Event that belongs to any Actor. If you call “ApplyDamage” on an Actor then Event AnyDamage is going to be called.

what is “set”

this is a new thing i got, it almost works how i need it to. it waits 10 seconds the first time its triggered instead of 5 and if its re-triggered then it waits less than 5 seconds

You should not use Tick for simple calculations like health. Tick should only be used for things like movement. The two examples that we have given you uses timers and will only be called when necessary. Tick not only waste performance but can be tricky to debug.

As a beginner myself I know it can sometimes be frustrating when you spend a better part of the day trying to do something that in your mind should be easy but then you hit the wall again and again. Don’t give up!

Have a look below. Copy and paste into your editor if you have to but this setup does work, maybe it will help you understand how basics work.

You will need to create a new blueprint class (actor). This will be a simple box collision actor. Name it whatever you want. When you walk into inside the box you will receive damage. You will see what I mean later.

Follow the steps in screenshots and then compile/save, place your actor somewhere in the world and you should get something like this.

Play with scale of the box in the world to make it big enough so it’s easy to walk into, place it next to an object in the world.

Next go into your character blueprint and create some variables, you need 3:

Health (float) - to store your current health value, set default to 1.
TakingDamage? (boolean) - to know if you are taking damage (default false)
RegenUseTime (integer) - this is your clock value (default 0)

I’m using intager for my clock because I’m displaying the countdown on screen too.

This may not be the best setup but I would say it’s new-starter friendly. Of course solutions from @GamerP57 and @Everynone are awesome and way better then mine so you should give them a go too!

You have the solutions here, all you have to do is copy it and play around to better understand what is going on. Have fun! :slight_smile:

277222-variables-character.png

Seconded. Spamming the latent request system with delays on Tick can be super hard to debug and just chugs memory unnecessary.

I’m pretty sure @DonBusso’s solution would also work well here after refactoring so it does not depend on key presses.


@rubiksmaster02: You’ve got 2.5 solid solutions here with Garner’s encapsulating a bit of additional functionality neatly.

But since you need to ask what ‘set’ is, perhaps it would be better to cover some of the basics first so you do not need to pull your hair out for hours.

On the other hand, everyone has their own favourite learning / failing method.

Good luck!

i already found a solution and it is way more simple than that
you went a very long way around

Great to hear you got it solved. Care to drop a few pointers how you dealt with the problem for newbies like myself? :slight_smile: