So I am trying to learn how to use online beacons in UE4, and there seems to be scarce information and documentation on how to use it. I’m trying to get information from a client listen server that’s waiting idle and keep running into issues trying to write the beacon in my game instance.
MyGameInstance.h
#include "Engine/GameInstance.h"
#include "DefconOnlineBeaconHost.h"
#include "DefconOnlineBeaconHostObject.h"
#include "MyGameInstance.generated.h"
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = GlobalCommands)
ADefconOnlineBeaconHost* Beacon;
UFUNCTION(BlueprintCallable, Category = GlobalCommands)
void InitializeBeaconHost();
##MyGameInstance.cpp
void UNWGameInstance::InitializeBeaconHost()
{
if (Beacon)
{
Beacon->DestroyBeacon();
Beacon = NULL;
}
// Create the hosting connection
ADefconOnlineBeaconHost* BeaconHost = GetWorld()->SpawnActor<ADefconOnlineBeaconHost>(ADefconOnlineBeaconHost::StaticClass());
if (BeaconHost && BeaconHost->InitHost())
{
// Register a class to handle traffic of a specific type
ADefconOnlineBeaconHostObject* BeaconHostObject = GetWorld()->SpawnActor<ADefconOnlineBeaconHostObject>(ADefconOnlineBeaconHost::StaticClass());
if (BeaconHostObject)
{
BeaconHost->RegisterHost(BeaconHostObject);
Beacon = BeaconHost;
}
else
GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, TEXT("There was an error: BeaconHostObject is a nullptr."));
}
else
GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, TEXT("Beacon->Init() failed."));
if (Beacon)
Beacon->DefGetListenPort();
else
GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Red, TEXT("There was an error: Beacon is a null ptr."));
}
##MyOnlineBeaconHost.h
#include "CoreMinimal.h"
#include "OnlineBeaconHost.h"
#include "MyOnlineBeaconHost.generated.h"
/**
*
*/
UCLASS()
class DEFCON5DEV_API ADefconOnlineBeaconHost : public AOnlineBeaconHost
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, Category = GlobalCommands)
void MyCustomGetListenPort();
};
##MyOnlineBeacon.cpp
#include “MyProject.h”
#include “MyOnlineBeaconHost.h”
void AMyOnlineBeaconHost::MyCustomGetListenPort()
{
GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Green, TEXT("Getting Listen Port... Please Wait."));
GEngine->AddOnScreenDebugMessage(-1, 10, FColor::Green, FString::Printf(TEXT("Beacon Listening Port: %d"), GetListenPort()));
}
###In MyGameInstance.cpp, when I try to call BeaconHost->InitHost(), it returns false (meaning that it failed to complete successfully). Any help would be greatly appreciated. I’m on a time constrained project right now, so there are no wrong answers on this one.
I’ll take a guess or anything, so even if your not sure, through up your ideas. I’ve spent a few days trying to work out this issue and I’m way behind on this project now because of this.