Hi,
I have perhaps a relatively simple question about breaking out of a for loop.
​My current code does the following:
- loop through a std::vector of input contexts
​- if the requested button can be mapped as an action or state in a context, the for loop stops.
​So basically I map the button to an action, in the 1st context the loop runs into and then stop (a button can only lead to 1 action, in the 1st context it finds a mapping). The contexts are sorted on priority [0] being the highest priority.
The code:
void CInputManager::SetRawButtonState(const RAW_BUTTON pButton, const bool pState, const bool pPrevState)
{
for(auto &context : mContexts)
{
if(context.IsActive())
{
if(context.MapButtonToAction(pButton)) break;
else
{
if(context.MapButtonToState(pButton, pState)) break;
}
}
}
}
Now, what I want to do is add another check:
​- I only want to try to map button to action in a certain condition (pState = true, pPrevState = false)
After trying to add that to the code, my mind gets confused, is it correct how I tried this, or is the theory on the 'break' no langer valid (because of if statement within if statement).
Back to the basics: I think that the break simply takes me out of the for loop. If so, all is good.
​Can someone confirm this or tell me where my mind confuses me? :)
The attempt:
void CInputManager::SetRawButtonState(const RAW_BUTTON pButton, const bool pState, const bool pPrevState)
{
for(auto &context : mContexts)
{
if(context.IsActive())
{
bool actionDone = false;
if(pState && !pPrevState)
{
if(context.MapButtonToAction(pButton)) actionDone = true;
}
if(actionDone) break;
else
{
if(context.MapButtonToState(pButton, pState)) break;
}
}
}
}