# Need help writing a mechanic that will accumulate a sum of lost stamina

At the moment I have a stamina drain system for when the player moves then he will drain stamina. I’m trying to make a mechanic that will keep adding up the value of stamina drained. For example, If I were to drain 50 stamina, regen, drain 20, regen, then drain 40 stamina then the variable the stamina drain sum is assigned to should be 110 stamina. Would appreciate any help!

Defining a float variable, initializing it to 0 and adding the same number you subtract from stamina to it should give you the result, unless you are asking for something else and I misunderstood your question…
In the case you are doing a clamp and this is the problem, just save the stamina value befor subtraction and then add the difference after the subtraction to the accumulated drain value.

1 Like

I have tried that before but because Stamina is a live value then it normally adds the stamina regen as well in another function

I can help better if you post a picture of your drain system as well, but assuming somehow your regen is also being fed into the same system you can add a condition (branch) that only adds the number if it is negative (assuming you are using negative numbers for drain) or if the after change stamina is smaller than before change.

1 Like

Oh well this was regen system, the drain system is the same thing but with a minus.

Ok now I see what you are trying to do. There are multiple ways to do this. I would personally do this :
1 - Define a new blueprint variable called TotalStaminaDrain (or some other name)
3 - Pass stamina value to your custom event InitialStamina parameter wherever you call it.
3 - Add a new branch where you get stamina and check if it is smaller than the InitialStamina parameter.
4- Connect all of your Set Stamina nodes to the branch you created.
5- If the branch condition is true, Add the difference of Get Stamina and InitialStamina to TotalStaminaDrain. If it is false, don’t do anything.

This should correctly calculate the total amount of stamina drain.

If you don’t want to change your custom event signature (because it is already used a lot and would be difficult to fix everywhere or for some other reason), You can create a new blueprint variable (float) called InitialStamina and set it to Get Stamina at the start of your custom event and the rest is basically the same as the above method.

P.S : By the way, I would personally recommend creating another custom event called UpdateStamina with one parameter (float) called StaminaChange. And then do all the calculation there (Delta Time multiplication and clamping and setting stamina, etc.) then inside your current custom event only call the UpdateStamina event and pass the correct value (Running depletion/walking regen/…) to it. Makes your graphs a lot cleaner.

For your third step, Your instructions sound like pass the stamina value to the parameter value. I’m not sure what you meant by the third step

I mean, for example lets say you call DrainOrRegenStamina in your tick event, you’d want to use “get stamina” node there and pass it to the DrainOrRegenStamina event like this picture :

Inside the DrainOrRegenStamina itself you’d want something like this :

I obviously don’t have the logic for the set stamina nodes in the picture. Just put your own logic there.

Ah I get what you mean now, That worked perfectly. I appreciate your answer! I will do optimization later, I just want functioning mechanics to begin with. Thank you so much!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.