Download

How to check a variable for update constantly?

I’m trying to create a “can do things” variable, which should constantly update “can do things” based on stamina. What is the best way to go about this? I was thinking “event tick” and using a branch node to check for stamina and setting the variable true or false from the branch. Will that work correctly?

And if so, it will be easy to use for things that require an input every time (I.e. attacking), but how do I create an action or input action that constantly checks the variable? If I’m blocking, I would add a branch node check on “can do things” and then proceed to enable the block. However, even though “can do things” is being updated constantly, the block input action is not checking constantly to read those updates. How would I make it check every .05 seconds or something? I’ve created a few messy workarounds, but I just feel like I’m missing an obvious solution. Is there some sort of “reset” node that would do this?

Sorry for the noob question, but hey, I am one.

There are two methods. Ideally, you will want booleans IE:

CanAttack
CanBlock
CanDrinkPot
CanEct

Now, you want a function called ChangeStamina with an input of a float. The function will do two things:

  1. Modify the stamina through the standard Stamina = Stamina + Function Input
  2. Set CanAttack, CanBlock, CanWhatever, based upon the branch Stamina check.

In this way, you are not constantly tick checking, and consequently not running branches on your tick.

Alternatively, you can stack the stamina check right on your tick, but in this case you want to use custom events and separate out the checks into well named events. In this way you can fire them in the right order. The function is better because you only do the check when you actually need to.

If there are multiple things that can modify those booleans, you can instead make a function for each one:

FuncCanAttack
FuncCanBlock
FuncCanDrinkPot

Now, you setup your function for stamina, health, ect, called ModifyStam. The input being the same, and do this:

  1. Stamina = Stamina + Function Input
  2. FuncCanAttack, FuncCanBlock, FuncCanDrinkPot, ect.

Those functions, FuncCanAttack for example, will do all the checks to set the CanAttack boolean. So for example, if you can’t attack when your health is 0, you can call FuncCanAttack from your ModifyHealth variable as well. In this way, you are only calling that check and setting the variables when you need them, and not every tick.

Also, just as a side note, ModifyStamina, ModifyHealth, ModifyWhatever, uses addition, but you can input a negative number. Remember, adding is always faster than subtracting and multiplying is always faster than dividing.

If you don’t understand any of this and want an example, let me know, I could possibly throw something together.

Thanks! Super helpful. I’m excited to try get this working. I’d never heard that about the addiction and multiplication being faster, good to know!

Disclaimer: I read this from something on Doom, and how it was revolutionary due to some coding techniques that avoided subtracting and dividing. I don’t know this as fact, and I may be wrong. It is just something that I’ve done out of habit since.

Mcgrill, had trouble understanding Britain’s answer (maybe it is the same as mine), but it would be much more efficient to instead of check for when it changes, just execute something every-time you change it. This eliminates constant checking. Basically after you set the variable, execute the same code you would’ve if “variable has changed” was true in your other idea.

That’s the distilled jist. They want booleans controlling whether or not he/it can perform an action, take for example attacking. Let’s say you can’t attack if your health is at, or below 0, or if your stamina is 0. So you make a boolean called CanAttack.

Now you create a function call FuncCanAttack, in that function you put your check. If Stamina <= 0 or Health <= 0 then CanAttack = false else CanAttack = true.

Now you create two more functions called ModifyHealth and ModifyStamina. They both have a float input, and you can input a negative value to take away or a positive value to add. The second part of those functions would be to run the FuncCanAttack function, because both the health and stamina effect whether or not you can attack based on their values.

In this way, you are only doing the check when you modify a value that affects a certain state bool.

Just implemented this. Works perfectly.