Download

Global App Instance handle - Network State

For the game I am working on, I need to know the network state in real time (AKA via a delegate).
On mobile devices this is likely to change during game play (as user physically moves between Wifi hot spots/Cellular coverage)

I have written the network state detection for various back ends (iOS/Android) and wired this to FGenericApplication ie.
GenericApplication.h



class GenericApplication 
{
...
public:
        DECLARE_EVENT_OneParam(GenericApplication, FOnNetworkConnectivityChanged, const ENetworkConnectivity::Type);

	/** Notifies subscribers when network connectivity status changed (online/offline/ethernet/wifi et al.) */
	FOnNetworkConnectivityChanged& OnNetworkConnectivityChanged() { return OnNetworkConnectivityChangedEvent; }
...
protected:
FOnNetworkConnectivityChanged OnNetworkConnectivityChangedEvent;
...
}


However, subscribing to this event in C++ construction code seems to cause crashes during packaging eg.
Blueprint menu base class .cpp



UPhzUIMenuBase::UPhzUIMenuBase(const FObjectInitializer& FO)
	: Super(FO)
{
	if (FSlateApplication::Get().GetPlatformApplication().Get())
	{
		GenericApplication::FOnNetworkConnectivityChanged& Event = FSlateApplication::Get().GetPlatformApplication()->OnNetworkConnectivityChanged();		
		NetworkStateHandle = Event.AddUObject(this, &UPhzUIMenuBase::OnNetworkStateChanged);
	}
}


Is there a ‘best practices’ way of doing this sort of thing (assigning a global app handle delegate in a class constructor)?
UUserWidget::AddToViewport() doesn’t seem to be virtual (while RemoveFromParent is).

Is there a better way to get a handle to the global application instance than FSlateApplication::Get() that won’t crash packaging?

For those trying to solve similar issues, I worked around this by binding to a custom delegate in FGameDelegates rather than directly to FGenericApplication.