Below snippet is part of a game application's logic and the Update
function is called every frame (about 60 times a second).
void Update()
{
handleInputEvents();
if (bShow == true)
{
if (bOnce)
{
button.show(true);
bOnce = false;
}
}
else
{
if (bOnce == false)
{
button.show(false);
bOnce = true;
}
}
}
Is there a better way to do what the above snippet is doing?
What the snippet is trying to achieve?
This snippet uses a button.show()
function to update a virtual button's state. Everytime a physical button is pressed the state of virtual button is set to true
(where true means the virtual button shows on screen and false means it doesn't) , as the physical button press is released, the state of the virtual button needs to be updated to false
, meaning the virtual button will not show on the screen.
Problem: If bOnce
flag check is not used, then the button.show()
will be called every frame leading to more CPU cycles wasted.
The snippet shows my attempt to solve the problem.
bShow
defined? Can you show us the code where you are setting it? \$\endgroup\$b
stands for "button" :P \$\endgroup\$b
inbShow
stands for it's type which isbool
, and it's defined as part of the class member. Also to answer your concern, yes because the cpu cycles are used in calling a function, if the logic is present there then it's not very useful as the call instruction will be utilized and then there will be a return instruction taking up significant CPU cycles instead if I just check before calling the function then I can avoid using of CPU cycles every frame \$\endgroup\$