#Unreal Engine 5 C++ Multiplayer Shooter PART 43 NetworkRole

10 messages · Page 1 of 1 (latest)

vernal dove
#

Hello, i m on UE5.1 and try to get the OverheadWidget workung.
VS2022 tells me, that OnLevelRemovedFromWorld is not a member of UUserWidget.
what can i do?

OverheadWidget.cpp

#include "Components/TextBlock.h"



void UOverheadWidget::SetDisplayText(FString TextToDisplay)
{
    if (DisplayText)
    {
        DisplayText->SetText(FText::FromString(TextToDisplay));
    }
}

void UOverheadWidget::ShowPlayerNetRole(APawn* InPawn)
{
    ENetRole RemoteRole = InPawn->GetRemoteRole();
    FString Role;
    switch (RemoteRole)
    {
    case ENetRole::ROLE_Authority:
        Role = FString("Authority");
        break;
    case ENetRole::ROLE_AutonomousProxy:
        Role = FString("Autonomous Proxy");
        break;
    case ENetRole::ROLE_SimulatedProxy:
        Role = FString("Simulated Proxy");
        break;
    case ENetRole::ROLE_None:
        Role = FString("None");
        break;
    }
    FString LocalRoleString = FString::Printf(TEXT("Remote Role: %s"), *Role);
    SetDisplayText(LocalRoleString);
}

void UOverheadWidget::OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld)
{
    RemoveFromParent();
    Super::OnLevelRemovedFromWorld(InLevel, InWorld);
}```


OverheadWidget.h
```#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "OverheadWidget.generated.h"

/**
 * 
 */
UCLASS()
class BLASTERNEW_API UOverheadWidget : public UUserWidget
{
    GENERATED_BODY()
public:
    UPROPERTY(meta = (BindWidget))
    class UTextBlock* DisplayText;

    void SetDisplayText(FString TextToDisplay);

    UFUNCTION(BlueprintCallable)
    void ShowPlayerNetRole(APawn* InPawn);


protected:
    virtual void OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld) override; //Override or is final okay too?

};
twin quartz
#

he OnLevelRemovedFromWorld virtual function that we used to override was removed in 5.1.

From Chief: " So the reason that method was removed is they moved the widget add/remove logic to GameViewportSubsystem to allow more flexibility when working with widgets. It doesn't provide a convenient new method to do the same thing, so you can just tap into FWorldDelegates::LevelRemovedFromWorld directly."

In Menu.h remove "virtual" and "overrirde" so it looks like this:
void OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld);

In Menu.cpp
MenuSetup() - bind the callback to the delegate
FWorldDelegates::LevelRemovedFromWorld.AddUObject(this, &UMenu::OnLevelRemovedFromWorld);

In OnLevelRemovedFromWorld
Don't call // Super::OnLevelRemovedFromWorld(InLevel, InWorld);

Or use
virtual void NativeDestruct() override;

#

U Can Check The Pinned Post In #🔫|multiplayer-shooter There Are more details provided by Meags and other memebers

#

also i copy pasted these codes from that pinned post only which was created by Meags

vernal dove
#

okay i did as intended

#

but

#

where do i have to put

FWorldDelegates::LevelRemovedFromWorld.AddUObject(this, &UMenu::OnLevelRemovedFromWorld);

my menu.cpp looks like this:

#include "Components/TextBlock.h"

void UOverheadWidget::SetDisplayText(FString TextToDisplay)
{
    if (DisplayText)
    {
        DisplayText->SetText(FText::FromString(TextToDisplay));
    }
}

void UOverheadWidget::ShowPlayerNetRole(APawn* InPawn)
{
    ENetRole RemoteRole = InPawn->GetRemoteRole();
    FString Role;
    switch (RemoteRole)
    {
    case ENetRole::ROLE_Authority:
        Role = FString("Authority");
        break;
    case ENetRole::ROLE_AutonomousProxy:
        Role = FString("Autonomous Proxy");
        break;
    case ENetRole::ROLE_SimulatedProxy:
        Role = FString("Simulated Proxy");
        break;
    case ENetRole::ROLE_None:
        Role = FString("None");
        break;
    }
    FString LocalRoleString = FString::Printf(TEXT("Remote Role: %s"), *Role);
    SetDisplayText(LocalRoleString);
}


void UOverheadWidget::OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld)
{
    RemoveFromParent();
}

Where do i put my new code in?

twin quartz
# vernal dove where do i have to put ```FWorldDelegates::LevelRemovedFromWorld.AddUObject(thi...

In Menu.h remove "virtual" and "overrirde" so it looks like this:
void OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld);

In Menu.cpp
MenuSetup() - bind the callback to the delegate
FWorldDelegates::LevelRemovedFromWorld.AddUObject(this, &UMenu::OnLevelRemovedFromWorld);

In OnLevelRemovedFromWorld
Don't call // Super::OnLevelRemovedFromWorld(InLevel, InWorld);

Or use
virtual void NativeDestruct() override;

#

U can Tag Meags And Take Help of her because i completed the course in 5.0.2 so i dont know much about this

vernal dove
#

I did it. Thanks for the Help!