Skip to content

Commit

Permalink
refactor agones component to subsystem
Browse files Browse the repository at this point in the history
  • Loading branch information
GloryOfNight committed Nov 11, 2024
1 parent 10a8bca commit 2ca6bc7
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 218 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
#include "CoreMinimal.h"
#include "Interfaces/IHttpRequest.h"
#include "IWebSocket.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "TimerManager.h"

#include "AgonesComponent.generated.h"
#include "AgonesSubsystem.generated.h"

DECLARE_DYNAMIC_DELEGATE_OneParam(FAgonesErrorDelegate, const FAgonesError&, Error);

Expand Down Expand Up @@ -103,16 +105,21 @@ class FHttpVerb
};

/**
* \brief UAgonesComponent is the Unreal Component to call to the Agones SDK.
* \brief UAgonesSubsystem is the Unreal Component to call to the Agones SDK.
* See - https://agones.dev/ for more information.
*/
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent), Config = Game, defaultconfig)
class AGONES_API UAgonesComponent final : public UActorComponent
UCLASS(Config = Game, defaultconfig)
class AGONES_API UAgonesSubsystem : public UGameInstanceSubsystem, public FTSTickerObjectBase
{
GENERATED_BODY()

public:
UAgonesComponent();
/**
* \brief Retrive subsystem component from game instance. Subsystem exist only on server builds! See ShouldCreateSubsystem.
* \param WorldContext - context of the world
*/
UFUNCTION(BlueprintPure, meta = (WorldContext = "WorldContext"), DisplayName = "Get Agones Subsystem", Category = "Agones | Utility")
static UAgonesSubsystem* Get(const UObject* WorldContext);

/**
* \brief HttpPort is the default Agones HTTP port to use.
Expand All @@ -123,13 +130,19 @@ class AGONES_API UAgonesComponent final : public UActorComponent
/**
* \brief HealthRateSeconds is the frequency to send Health calls. Value of 0 will disable auto health calls.
*/
UPROPERTY(EditAnywhere, Category = Agones, Config)
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Agones, Config)
float HealthRateSeconds = 10.f;

/**
* \brief bDisableAutoConnect will stop the component auto connecting (calling GamesServer and Ready).
* \brief bDisableAutoHealthPing will stop call to HealhPing() during initialization
*/
UPROPERTY(EditAnywhere, Category = Agones, Config)
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Agones, Config)
bool bDisableAutoHealthPing;

/**
* \brief bDisableAutoConnect will stop auto connecting (calling GamesServer and Ready) during initialization.
*/
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Agones, Config)
bool bDisableAutoConnect;

/**
Expand All @@ -139,15 +152,25 @@ class AGONES_API UAgonesComponent final : public UActorComponent
FConnectedDelegate ConnectedDelegate;

/**
* \brief BeginPlay is a built in UE4 function that is called as the component is created.
* \brief ShouldCreateSubsystem is a built in subsystem function that is called before Initialize.
*/
virtual void BeginPlay() override;
virtual bool ShouldCreateSubsystem(UObject* Outer) const override;

/**
* \brief EndPlay is a built in UE4 function that is called as the component is destroyed.
* \param EndPlayReason reason for Ending Play.
*/
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
* \brief Initialize is a built in subsystem function for initilization of subsystem.
* \param Collection can help initialize subsystem dependencies
*/
virtual void Initialize(FSubsystemCollectionBase& Collection) override;

/**
* \brief Deinitialize is a built in subsystem function called during destruction of GameInstance.
*/
virtual void Deinitialize() override;

/**
* \brief Tick is a built in ticker function called every frame.
*/
bool Tick(float DeltaTime) override;

/**
* \brief HealthPing loops calling the Health endpoint.
Expand Down Expand Up @@ -353,6 +376,8 @@ class AGONES_API UAgonesComponent final : public UActorComponent
void SetCounterCapacity(FString Key, int64 Capacity, FSetCounterCapacityDelegate SuccessDelegate, FAgonesErrorDelegate ErrorDelegate);

private:
FTimerManager* GetTimerManager() const;

DECLARE_DELEGATE_OneParam(FUpdateCounterDelegate, const FEmptyResponse&);
void UpdateCounter(const FString& Key, const int64* Count, const int64* Capacity, const int64* CountDiff, FUpdateCounterDelegate SuccessDelegate, FAgonesErrorDelegate ErrorDelegate);

Expand All @@ -371,6 +396,8 @@ class AGONES_API UAgonesComponent final : public UActorComponent

FTimerHandle EnsureWebSocketTimerHandler;

TUniquePtr<FTimerManager> TimerManager;

TSharedPtr<IWebSocket> WatchWebSocket;

TArray<UTF8CHAR> WatchMessageBuffer;
Expand Down
46 changes: 13 additions & 33 deletions site/content/en/docs/Guides/Client SDKs/unreal.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,48 +123,27 @@ PublicDependencyModuleNames.AddRange(
"Agones",
});
```
- Add component in header
- Add subsytem in header
```c++
#include "AgonesComponent.h"

UPROPERTY(EditAnywhere, BlueprintReadWrite)
UAgonesComponent* AgonesSDK;
#include "AgonesSubsystem.h"
```
- Initialize component in GameMode
```c++
#include "AgonesComponent.h"
#include "Classes.h"

ATestGameMode::ATestGameMode()
{
AgonesSDK = CreateDefaultSubobject<UAgonesComponent>(TEXT("AgonesSDK"));
}
```

- Use the Agones component to call PlayerReady
- Use the Agones subsystem to call PlayerReady
```c++
void APlatformGameSession::PostLogin(APlayerController* NewPlayer)
{
// Empty brances are for callbacks on success and errror.
AgonesSDK->PlayerConnect("netspeak-player", {}, {});
UAgonesSubsystem* AgonesSDK = UAgonesSubsystem::Get(this);
if (AgonesSDK) // Check for nullptr is a must.
{
// Empty brances are for callbacks on success and errror.
AgonesSDK->PlayerConnect("netspeak-player", {}, {});
}
}
```
#### Using Blueprints (UE5)
- Add Component to your Blueprint GameMode
![component](../../../../images/unreal5_bp_component.png)
- This will automatically call `/health` every 10 seconds and once `/gameserver` calls are succesful it will call `/ready`.
- Accessing other functionality of Agones can be done via adding a node in Blueprints.
![actions](../../../../images/unreal5_bp_actions.png)
#### Using Blueprints (UE4)
- Add Component to your Blueprint GameMode
![component](../../../../images/unreal_bp_component.png)
- This will automatically call `/health` every 10 seconds and once `/gameserver` calls are succesful it will call `/ready`.
- Accessing other functionality of Agones can be done via adding a node in Blueprints.
![actions](../../../../images/unreal_bp_actions.png)
#### Using Blueprints
- Accessing other functionality of Agones can be done via Blueprints.
![actions](../../../../images/unreal_bp_usage.png)
## Configuration Options
Expand All @@ -174,6 +153,7 @@ A number of options can be altered via config files in Unreal these are supplied
[/Script/Agones.AgonesComponent]
HttpPort=1337
HealthRateSeconds=5.0
bDisableAutoHealthPing=false
bDisableAutoConnect=true
```
Expand Down
Binary file removed site/static/images/unreal5_bp_actions.png
Binary file not shown.
Binary file removed site/static/images/unreal5_bp_component.png
Binary file not shown.
Binary file removed site/static/images/unreal_bp_actions.png
Binary file not shown.
Binary file removed site/static/images/unreal_bp_component.png
Binary file not shown.
Binary file added site/static/images/unreal_bp_usage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2ca6bc7

Please sign in to comment.