Unreal
ViveportUserStatsDemo.h
#include "Components/ActorComponent.h"
#include "ViveportApi.h"
#include "ViveportUserStats.h"
#include "ViveportType.h"
#include "ViveportUserStatsDemo.generated.h"
UCLASS(ClassGroup = (Viveport), meta = (BlueprintSpawnableComponent))
class VIVEPORTSDK_API UViveportUserStatsDemo : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UViveportUserStatsDemo();
// Called when the game starts
void BeginPlay() override;
void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
// Called every frame
void TickComponent(float DeltaTime,
LevelTick TickType,
FActorComponentTickFunction* ThisTickFunction
) override;
/** The APP ID for auth verify */
FString VIVEPORT_ID = "bd67b286-aafc-449d-8896-bb7e9b351876";
public:
class MyInitCallback : public ViveportApiStatus
{
public:
void OnSuccess(
) override;
void OnFailure(int errorCode
) override;
};
class MyIsReadyStatus : public ViveportApiStatus
{
public:
void OnSuccess(
) override;
void OnFailure(int errorCode
) override;
};
class MyDownloadLeaderboardScoreStatus : public ViveportApiStatus
{
public:
void OnSuccess(
) override;
void OnFailure(int errorCode
) override;
};
class MyUploadLeaderboardScoreStatus : public ViveportApiStatus
{
public:
void OnSuccess(
) override;
void OnFailure(int errorCode
) override;
};
class MyShutdownCallback : public ViveportApiStatus
{
public:
void OnSuccess(
) override;
void OnFailure(int error_code
) override;
};
};
ViveportUserStatsDemo.cpp
#include "ViveportSDKPrivatePCH.h"
#include "ViveportUserStatsDemo.h"
UViveportUserStatsDemo::MyInitCallback myInitCallback;
UViveportUserStatsDemo::MyShutdownCallback myShutdownCallback;
UViveportUserStatsDemo::MyIsReadyStatus myIsReadyStatus;
UViveportUserStatsDemo::MyDownloadLeaderboardScoreStatus myDownloadLeaderboardScoreStatus;
UViveportUserStatsDemo::MyUploadLeaderboardScoreStatus myUploadLeaderboardScoreStatus;
//Leaderboard information
FString fLeaderBoardName = "ID_Leaderboard1";
int upload_score = 10;
//Download Leaderboard conditions
UELeaderboardDataDownload leaderboard_scope = u_ELeaderboardDataDownloadLocal;
UELeaderboardDataTimeRange leaderboard_time_range = u_ELeaderboardDataScropeAllTime;
int range_start = 0;
int range_end = 10;
// Sets default values for this component's properties
UViveportUserStatsDemo::UViveportUserStatsDemo()
{
// Set this component to be initialized when the game starts, and to be ticked every
// frame. You can turn these featuresoff to improve performance if you don't need them.
bWantsBeginPlay = true;
PrimaryComponentTick.bCanEverTick = true;
}
// Called when the game starts
void UViveportUserStatsDemo::BeginPlay()
{
Super::BeginPlay();
UViveportApi::Init(&myInitCallback, VIVEPORT_ID);
}
void UViveportUserStatsDemo::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
UViveportApi::Shutdown(&myShutdownCallback);
}
// Called every frame
void UViveportUserStatsDemo::TickComponent(float DeltaTime,
ELevelTick TickType,
FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}
void UViveportUserStatsDemo::MyInitCallback::OnSuccess()
{
FString fstring("Init success.");
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
UViveportUserStats::IsReady(&myIsReadyStatus);
}
void UViveportUserStatsDemo::MyInitCallback::OnFailure(int error)
{
FString fstring("Init failure. error code=" + error);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}
void UViveportUserStatsDemo::MyIsReadyStatus::OnSuccess()
{
FString fstring("IsReady success.");
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
UViveportUserStats::UploadLeaderboardScore(&myUploadLeaderboardScoreStatus,
fLeaderBoardName,
upload_score);
}
void UViveportUserStatsDemo::MyIsReadyStatus::OnFailure(int error)
{
const FString fstring = FString::Printf(TEXT("IsReady failure. error=%d"), error);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}
void UViveportUserStatsDemo::MyDownloadLeaderboardScoreStatus::OnSuccess()
{
FString fstring("DownloadLeaderboardScore success.");
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
fstring = FString::Printf(TEXT("GetLeaderboardSortMethod=%d\nGetLeaderboardDisplayType=%d"),
(int) UViveportUserStats::GetLeaderboardSortMethod(),
(int) UViveportUserStats::GetLeaderboardDisplayType()
);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
int size = UViveportUserStats::GetLeaderboardScoreCount();
for (int i = 0; i AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}
}
void UViveportUserStatsDemo::MyDownloadLeaderboardScoreStatus::OnFailure(int error)
{
const FString fstring = FString::Printf(TEXT("DownloadLeaderboardScore failure. error=%d"),
error);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}
void UViveportUserStatsDemo::MyUploadLeaderboardScoreStatus::OnSuccess()
{
FString fstring("UploadLeaderboardScore success.");
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
UViveportUserStats::DownloadLeaderboardScores(&myDownloadLeaderboardScoreStatus,
fLeaderBoardName,
leaderboard_scope,
leaderboard_time_range,
range_start,
range_end
);
}
void UViveportUserStatsDemo::MyUploadLeaderboardScoreStatus::OnFailure(int error)
{
const FString fstring = FString::Printf(TEXT("UploadLeaderboardScore failure. error=%d"),
error);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}
void UViveportUserStatsDemo::MyShutdownCallback::OnSuccess()
{
FString fstring("Shutdown success.");
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}
void UViveportUserStatsDemo::MyShutdownCallback::OnFailure(int error_code)
{
FString fstring = FString::Printf(TEXT("Shutdown failure. error=%d"), error_code);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}