I don’t know if it’s a stupid question, but let’s say that I wrote a code where, in the Tick function, you keep checking bCheck1, bCheck2, bCheck3, and so on. Most of the time, bCheck will be false and won’t execute what’s inside it. Does checking the boolean affect performance even if bCheck is false and won’t execute the rest of the code?
Hey there @Lucky_Pharaoh! I’d recommend leaving it out of tick unless it’s 100% necessary to be there. Try to keep stuff event based if possible, as doing anything on tick can add up really quickly. What’s your use case? I might be able to tell you an easier way to make it event based.
Checking a boolean is a constant operation, so assuming these were false, this would only add O(3) to your code every frame. It’s completely negligible.
Let’s do a back-of-the-napkin calculation for fun. Let’s say your computer is the average modern computer with 3400 MHz and an operation takes 1 Hz. That means your computer can carry out 56 million operations per frame if you are going for 60 FPS.
These checks in the best case would take 3 out of 56 million operations, which means it would affect performance by 0.00000536%.
Don’t worry about it lol.
However, if you want to cut out the tick, reduce your checks, and simplify your code, look into subscribing to delegates (ie. event systems).
I was developing a little prototype where the player would be in a minigame and then transition to fighting. In the minigame, I constantly use the match timer, but when the player enters the fighting state, I won’t use the timer anymore, and the if condition will always be false, so even though it has no use, it will always get checked because it is in the Tick function. So I started to wonder if leaving it like this is good or bad.
Yeah, at the beginning, I thought this wouldn’t harm, but if I were developing a large game, this would start to have an effect on the performance.
If this is just going to be a one off, and just a single check, it’s not bad at all and not a problem. Even a couple of bool checks as Xeno mentioned, have very little impact in most cases. Though if there’s more to evaluate, it can start to be an issue. I personally wouldn’t bother the refactor on this, and if it’s timer based, you’ll be iterating regardless.