Created
November 23, 2023 01:18
-
-
Save kirby561/b08cb5f7c9c233b2db59839ebd76cf40 to your computer and use it in GitHub Desktop.
Using TransferComponent in a PlayerController example in Unreal Engine.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "TestPlayerController.h" | |
#include "Components/TransferComponent.h" | |
DEFINE_LOG_CATEGORY_STATIC(TestPlayerControllerSub, Log, All); | |
ATestPlayerController::ATestPlayerController() { | |
_transferComponent = CreateDefaultSubobject<UTransferComponent>(TEXT("TransferComponent")); | |
_transferComponent->SetVerboseLoggingEnabled(true); | |
} | |
ATestPlayerController::~ATestPlayerController() { | |
} | |
void ATestPlayerController::BeginPlay() { | |
Super::BeginPlay(); | |
if (GetNetMode() != ENetMode::NM_Client) { | |
int bufferLength = 1000000; | |
uint8* buffer = new uint8[bufferLength]; | |
for (int i = 0; i < bufferLength; i++) | |
buffer[i] = i % 16; | |
uint64 transferId = _transferComponent->SendBufferToClient( | |
buffer, | |
bufferLength, | |
[this] (uint64 completedTransferId) { | |
UE_LOG(TestPlayerControllerSub, Display, TEXT("We finished sending a buffer with id %llu from server to client"), completedTransferId); | |
ServerNotifiesClientTransferCompleted(completedTransferId); | |
} | |
); | |
UE_LOG(TestPlayerControllerSub, Display, TEXT("Server started sending a buffer with id %llu"), transferId); | |
} | |
} | |
void ATestPlayerController::ServerNotifiesClientTransferCompleted_Implementation(uint64 transferId) { | |
UE_LOG(TestPlayerControllerSub, Display, TEXT("Client received a buffer with id %llu"), transferId); | |
Transfer* transfer = _transferComponent->ClientGetsCompletedTransfer(transferId); | |
uint8* buffer = transfer->Buffer; | |
int length = transfer->Length; | |
_transferComponent->ClientFreesCompletedTransfer(transferId); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include "CoreMinimal.h" | |
#include "TestPlayerController.generated.h" | |
UCLASS() | |
class ATestPlayerController : public APlayerController { | |
GENERATED_BODY() | |
public: | |
ATestPlayerController(); | |
virtual ~ATestPlayerController(); | |
virtual void BeginPlay() override; | |
private: // Network methods | |
UFUNCTION(Client, Reliable) | |
void ServerNotifiesClientTransferCompleted(uint64 transferId); | |
void ServerNotifiesClientTransferCompleted_Implementation(uint64 transferId); | |
private: | |
class UTransferComponent* _transferComponent = nullptr; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment