0

I'm trying to create an event handler, which affects stamina regeneration in-game, for example every 0.75s tick CurrentStamina variable should be increased by BasicStaminaRegen variable's value. I wanted to include it into a while loop to check at every tick if CurrentStamina == MaxStamina.

Concept C++ code:

...
float currentStamina = 25.0, maxStamina = 100.0, basicStaminaRegen = 5.0;
while(currentStamina != maxStamina) {
(something related to handle a timer)
...
currentStamina += basicStaminaRegen;
}
...

Question is, how can I accomplish it in UE blueprint editor?

StaminaRegen defined function

enter image description here

Part of code with an appropriate event handler

enter image description here

TheInvisibleMan
  • 56
  • 1
  • 11
  • Comparing float values that way (for exact equality) is probably a [bad idea](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – user0042 Oct 04 '17 at 20:08
  • Then how would you like to do that? I'm afraid there isn't many other ways to check if one variable is equal to another. – TheInvisibleMan Oct 04 '17 at 21:05
  • Regarding C++ code you can use the [_epsilon_ approximation](http://en.cppreference.com/w/cpp/types/numeric_limits/epsilon). – user0042 Oct 04 '17 at 21:08
  • How much could it impact on the code's overall stability or performance (assuming there's a larg amount of in-game features using this technique)? I'm excluding a potential human factor within code's structure (eg. instead of comparing, re-writing current variable's value - a famous mistake of using = instead of ==). – TheInvisibleMan Oct 04 '17 at 21:57
  • _"How much could it impact on the code's overall stability or performance"_ It would impact your performance as much as your `while()` loops condition stays true (it could possibly run infinite). But I'm not sure what really applies here. Are you providing that exact C++ code you posted in your question, or does the Unreal Engine generate something for you? (I'd suspect in the latter case it should be done correctly). – user0042 Oct 04 '17 at 22:03
  • Anyway, thank you for that advise, it's worth taking into consideration. In this specific case while wouldn't run forever, because it's only related to temporary states, like previously given stamina/health regeneration, which would obviously finish just after leveling off to its initial value, stored in a separated variable. – TheInvisibleMan Oct 04 '17 at 22:27

1 Answers1

0

I recommend when the set timer event is running on loop there is no need for a while loop and complicate things just use a simple branch , again I can be wrong.

LumbusterTick
  • 1,067
  • 10
  • 21
  • Anyway, thanks for reply. I just got my hands on paper tutorial, which will include this type of operations as well (I hope so :p), so sooner or later I'll find out :) – TheInvisibleMan Oct 10 '17 at 12:36