I’ve been trying to figure this out for a few days now with no luck. Basically I’m trying to bind to a delegate defined in the PlayerState, from a UUserWidget. I’m running a listen server setup with 1 host and 1 other player. The other player works fine but the host fails to bind to the delegate. When I add multiple other players, it works fine for them as well, just the host fails.
UUserWidget.cpp:
bool UBLPUWGameMenu::Initialize()
{
// Call parent version of function and store result in variable
const bool Success = Super::Initialize();
if (!Success) return false;// Bind button callbacks to OnClicked delegate
if (!PropertyMenuBtn) return false;
PropertyMenuBtn->OnClicked.AddDynamic(this, &UBLPUWGameMenu::PropertyMenuBtnClicked);
if (!BuyBtn) return false;
BuyBtn->OnClicked.AddDynamic(this, &UBLPUWGameMenu::BuyBtnClicked);
if (!RollBtn) return false;
RollBtn->OnClicked.AddDynamic(this, &UBLPUWGameMenu::TakeTurnBtnClicked);
if (!FinishTurnBtn) return false;
FinishTurnBtn->OnClicked.AddDynamic(this, &UBLPUWGameMenu::FinishTurnBtnClicked);// Bind functions to delegate
ABLPPlayerState* PlayerStatePtr = GetOwningPlayerState();
if (!PlayerStatePtr) { UE_LOG(LogTemp, Warning, TEXT(“BLPUWGameMenu: PlayerStatePtr is null”)); return false; }PlayerStatePtr->ItsMyTurnDelegate.BindUObject(this, &UBLPUWGameMenu::ItsMyTurn);
return true;
}void UBLPUWGameMenu::ItsMyTurn()
{
YourTurnText->SetVisibility(ESlateVisibility::Visible);
YourTurnText->SetText(FText::FromString(“It’s your turn”));
RollBtn->SetVisibility(ESlateVisibility::Visible);
}
PlayerState.h
DECLARE_DELEGATE(FItsMyTurnSignature);
FItsMyTurnSignature ItsMyTurnDelegate;
PlayerState.cpp
// Notifies UI if its this players turn, so the turn UI buttons appear (roll, finish turn, etc.)
void ABLPPlayerState::OnRep_IsItMyTurn() const
{
if (IsItMyTurn)
{
UE_LOG(LogTemp, Warning, TEXT(“%s: It’s my turn!”), *GetPlayerName());
ItsMyTurnDelegate.ExecuteIfBound();
}
else
{
UE_LOG(LogTemp, Warning, TEXT(“%s I finished my turn”), *GetPlayerName());
ItsNotMyTurnDelegate.ExecuteIfBound();
}
}
Things to note:
- The rep notify function in PlayerState.cpp is called because the log message appears.
- I debugged PlayerState.cpp and the delegate is not bound for the host (I also get a crash when I use Execute instead of ExecuteIfBound).
- The button delegates in the UUserWidget bind successfully because the UI works on the host and all clients.
- I tried making the delegate multicast, didn’t work.
- I’m thinking this has something to with the PlayerState since the buttons work fine.
Has anyone else ever encountered a similar issue? Sorry if the formatting is weird, this is my first time posting to this.