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 VIVEPORT_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 MyDownloadStatsStatus : public ViveportApiStatus
{
public:
void OnSuccess(
) override;
void OnFailure(int errorCode
) override;
};
class MyUploadStatsStatus : 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::MyDownloadStatsStatus myDownloadStatsStatus;
UViveportUserStatsDemo::MyUploadStatsStatus myUploadStatsStatus;
FString stat_key_1 = "ID_Stat1";
FString stat_key_2 = "ID_Stat2";
int stat_key_default_1 = 0;
float stat_key_default_2 = 0.0f;
FString achievement_name = "ID_Achievement1";
// 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 features
// off 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::DownloadStats(&myDownloadStatsStatus);
}
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::MyDownloadStatsStatus::OnSuccess()
{
FString fstring("MyDownloadStatsStatus success.");
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
int stat_1 = UViveportUserStats::GetStat(stat_key_1, stat_key_default_1);
float stat_2 = UViveportUserStats::GetStat(stat_key_2, stat_key_default_2);
fstring = FString::Printf(TEXT("getStats, %s=%d, %s=%f"), *stat_key_1, stat_1, *stat_key_2, stat_2);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
stat_1++; stat_2++;
UViveportUserStats::SetStat(stat_key_1, stat_1);
UViveportUserStats::SetStat(stat_key_2, stat_2);
fstring = FString::Printf(TEXT("setStats, %s=%d, %s=%f"), *stat_key_1, stat_1, *stat_key_2, stat_2);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
bool achieved = UViveportUserStats::GetAchievement(achievement_name);
fstring = FString::Printf(TEXT("GetAchievement, name=%s, achieved=%s"),
*achievement_name,
achieved ? *FString("true") : *FString("false"));
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
if (achieved)
UViveportUserStats::ClearAchievement(achievement_name);
else
UViveportUserStats::SetAchievement(achievement_name);
UViveportUserStats::UploadStats(&myUploadStatsStatus);
}
void UViveportUserStatsDemo::MyDownloadStatsStatus::OnFailure(int error)
{
const FString fstring = FString::Printf(TEXT("DownloadStats failure. error=%d"), error);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}
void UViveportUserStatsDemo::MyUploadStatsStatus::OnSuccess()
{
FString fstring("UploadStats success.");
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::White, fstring);
}
void UViveportUserStatsDemo::MyUploadStatsStatus::OnFailure(int error)
{
const FString fstring = FString::Printf(TEXT("UploadStats 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);
}