Hi,
My post on a previous lesson makes things not work so smoothly here, but it’s realtively easy to fix. We would basically need our TimerEvent()
function to do different things depending on win or lose (in this case just updating the text box of a different UUserWidget). There are several ways of solving this. For simplicity and clairty, I opted to pass the bIsWinner
boolean along to the TimerEvent() function as well. I got the gist of calling timer functions with arguments from here.
First of all, we’ll declare our TimerEvent
as a UFUNCTION():
UFUNCTION()
void TimerEvent(bool bIsWinner);
We need to bind this function separately before we call the SetTimer()
method. We’ll call it with an FTimerDelegate
this time, with our function bound to it with the appropriate parameter value.
We can do it like this, before setting our timer:
//Create timer delegate
FTimerDelegate timerDelegate;
//Bind function to it, with specific parameter value
//Takes the object the function is called on, the UFUNCTION name, and a list of parameters
timerDelegate.BindUFunction(this, FName(TEXT("TimerEvent")), bIsWinner);
Then we set the timer with a slightly diffrent signature (note the lack of this
here):
//Takes timer handle, timer delegate to use, interval between calls, and whether to loop or not
GetWorldTimerManager().SetTimer(restartTimer, timerDelegate, 1.0, true);
Then, finally, let’s update our TimerEvent()
function. I just modified it to use this to get the bottom text within the function:
UUserWidget * currentWidget;
if(bIsWinner) {
currentWidget = winScreen;
} else {
currentWidget = loseScreen;
}
UTextBlock * bottomText = Cast<UTextBlock>(currentWidget->GetWidgetFromName(TEXT("TB_RestartTimer")));
And now it works again! Note that it’s importnat to get the text box name right, in my case “TB_RestartTimer”. Since I have the same name in both the win and lose widgets this works nicely. If you don’t, however, you can just do the whole text box getting bit within the if-statement as well
Kind regards,
Håkan