I am trying to create a countdown timer using Text. When the user presses a certain key, I want the seconds value to update on the screen as it counts down from 5. The code below uses a Urho3D::Timer, but it doesn’t display the text on the screen until after the timer has counted all the way down. Any suggestions would be appreciated.
ResourceCache* cache = GetSubsystem<ResourceCache>();
UI* ui = GetSubsystem<UI>();
// Construct new Text object, set string to display and font to use
Text* instructionText = ui->GetRoot()->CreateChild<Text>();
instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 48);
instructionText->SetColor(Color(0.0f, 1.0f, 0.0f));
instructionText->SetTextAlignment(HA_CENTER);
instructionText->SetHorizontalAlignment(HA_CENTER);
instructionText->SetVerticalAlignment(VA_CENTER);
instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
instructionText->SetText("Countdown");
// Add Text instance to the UI root element
GetSubsystem<UI>()->GetRoot()->AddChild(instructionText);
for (int i = 5; i >= 1; i--)
{
instructionText->SetText("Countdown " + String(i));
Timer tmr = Timer();
do {}
while (tmr.GetMSec(false) < 1000);
// reset the timer
tmr.Reset();
if (DEBUG) URHO3D_LOGINFOF("Tick %d", i);
}