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.
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?
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.
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.
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!
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)