Do Once not Doing Once?

So I’ve been looking for a solution to a situation for a few hours now and Do Once sounds like what I need. I basically am looking to see if something is above or below 50 and if so, add 1 or subtract 1 from another value (this is used for damage calculations in my setup). I figured Do Once would work fine for that, except that its doing all the time! It doesn’t fire once and then stop. If I check the “start closed” option, that works, but that obviously doesn’t help me.

Has anyone else experienced this?

2 Likes

Hmm I think I might have had an issue with that node, but im pretty sure it was my own fault. Can you share a screenshot of your blueprint?

So I did some testing, and it looks like if DoOnce is inside a Function, it will not do once it will do each time the function is called. Is this something related to the nature of how functions work that I’m not aware of?

6 Likes

When the function is called, the code inside is executed as it stands. “DoOnce” means when the execution pin hits it, it runs once and then stops. Assuming blueprint functions are the same as standard functions, anything inside can be considered to be local to the scope of that function. This means that they are created when the function is called and pushed onto the stack and destroyed when pushed off.

Your solution for this would be to have a branch before the DoOnce to store a blueprint wide “static” variable boolean that can record whether or not it needs to be run again.

8 Likes

Maybe 7 years late… however, if anyone is looking for a simple fix:

You can also run a branch node at the start en promote the condition to a variable. After your branch node (false) set the promoted variable to true. It’s a small setup but works like a charm! If you want to reset the function, just set the promoted variable back to false.

9 Likes

Yeah, I found this to be true as well. Do Once inside a function does not work which is really stupid, but it is what it is.

5 Likes

It does within the scope of the function.

:point_up_2:

Try this:

Everything inside the function gets thrown away when its done executing.

Try using a macro if you don’t want to have scope issues.

4 Likes

Oh, I get it. So a Do Once inside a function is essentially reset every time, because the function is being called for the first time every time it’s used. I didn’t know functions operated like this. Good to know, thank you!

1 Like

I ended up creating a non-local boolean variable instead that can be set manually, the problem is using a Do Once inside of a function. (For the next person who encounters this)

When and if they get wonky try using a gate instead.