Skip to content

Commit

Permalink
Added gameplay debugger code
Browse files Browse the repository at this point in the history
  • Loading branch information
orfeasel committed May 8, 2017
1 parent 92353fe commit 8a7dbd0
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 0 deletions.
11 changes: 11 additions & 0 deletions GameplayDebugger/GDBlogPost.Build.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

using UnrealBuildTool;

public class GDBlogPost : ModuleRules
{
public GDBlogPost(TargetInfo Target)
{
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "OrfeasModule" });
}
}
131 changes: 131 additions & 0 deletions GameplayDebugger/GDBlogPost/GDBlogPostCharacter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

#include "GDBlogPost.h"
#include "Kismet/HeadMountedDisplayFunctionLibrary.h"
#include "GDBlogPostCharacter.h"

//////////////////////////////////////////////////////////////////////////
// AGDBlogPostCharacter

AGDBlogPostCharacter::AGDBlogPostCharacter()
{
// Set size for collision capsule
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);

// set our turn rates for input
BaseTurnRate = 45.f;
BaseLookUpRate = 45.f;

// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;

// Configure character movement
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
GetCharacterMovement()->JumpZVelocity = 600.f;
GetCharacterMovement()->AirControl = 0.2f;

// Create a camera boom (pulls in towards the player if there is a collision)
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller

// Create a follow camera
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm

// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
// are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
}

//////////////////////////////////////////////////////////////////////////
// Input

void AGDBlogPostCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
// Set up gameplay key bindings
check(PlayerInputComponent);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);

PlayerInputComponent->BindAxis("MoveForward", this, &AGDBlogPostCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AGDBlogPostCharacter::MoveRight);

// We have 2 versions of the rotation bindings to handle different kinds of devices differently
// "turn" handles devices that provide an absolute delta, such as a mouse.
// "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("TurnRate", this, &AGDBlogPostCharacter::TurnAtRate);
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
PlayerInputComponent->BindAxis("LookUpRate", this, &AGDBlogPostCharacter::LookUpAtRate);

// handle touch devices
PlayerInputComponent->BindTouch(IE_Pressed, this, &AGDBlogPostCharacter::TouchStarted);
PlayerInputComponent->BindTouch(IE_Released, this, &AGDBlogPostCharacter::TouchStopped);

// VR headset functionality
PlayerInputComponent->BindAction("ResetVR", IE_Pressed, this, &AGDBlogPostCharacter::OnResetVR);
}


void AGDBlogPostCharacter::OnResetVR()
{
UHeadMountedDisplayFunctionLibrary::ResetOrientationAndPosition();
}

void AGDBlogPostCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)
{
Jump();
}

void AGDBlogPostCharacter::TouchStopped(ETouchIndex::Type FingerIndex, FVector Location)
{
StopJumping();
}

void AGDBlogPostCharacter::TurnAtRate(float Rate)
{
// calculate delta for this frame from the rate information
AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
}

void AGDBlogPostCharacter::LookUpAtRate(float Rate)
{
// calculate delta for this frame from the rate information
AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
}

void AGDBlogPostCharacter::MoveForward(float Value)
{
if ((Controller != NULL) && (Value != 0.0f))
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);

// get forward vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
}

//GLog->Log("Dummy str:" + MyClass::DummyString);
}

void AGDBlogPostCharacter::MoveRight(float Value)
{
if ( (Controller != NULL) && (Value != 0.0f) )
{
// find out which way is right
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);

// get right vector
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement in that direction
AddMovementInput(Direction, Value);
}
}
75 changes: 75 additions & 0 deletions GameplayDebugger/GDBlogPost/GDBlogPostCharacter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "GameFramework/Character.h"
#include "GDBlogPostCharacter.generated.h"

UCLASS(config=Game)
class GDBLOGPOST_API AGDBlogPostCharacter : public ACharacter
{
GENERATED_BODY()

/** Camera boom positioning the camera behind the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;

/** Follow camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;
public:
AGDBlogPostCharacter();

/** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
float BaseTurnRate;

/** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
float BaseLookUpRate;

UPROPERTY(EditAnywhere)
float Health;

UPROPERTY(EditAnywhere)
float MaxDamage;

protected:

/** Resets HMD orientation in VR. */
void OnResetVR();

/** Called for forwards/backward input */
void MoveForward(float Value);

/** Called for side to side input */
void MoveRight(float Value);

/**
* Called via input to turn at a given rate.
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
*/
void TurnAtRate(float Rate);

/**
* Called via input to turn look up/down at a given rate.
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
*/
void LookUpAtRate(float Rate);

/** Handler for when a touch input begins. */
void TouchStarted(ETouchIndex::Type FingerIndex, FVector Location);

/** Handler for when a touch input stops. */
void TouchStopped(ETouchIndex::Type FingerIndex, FVector Location);

protected:
// APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// End of APawn interface

public:
/** Returns CameraBoom subobject **/
FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
/** Returns FollowCamera subobject **/
FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
};

23 changes: 23 additions & 0 deletions GameplayDebugger/OrfeasModule/OrfeasModule.Build.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using UnrealBuildTool;

public class OrfeasModule : ModuleRules
{
public OrfeasModule(TargetInfo Target)
{
PublicIncludePaths.AddRange(new string[] { "OrfeasModule/Public" });

PrivateIncludePaths.AddRange(new string[] { "OrfeasModule/Private" });

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "GDBlogPost" });

if (UEBuildConfiguration.bBuildDeveloperTools || (Target.Configuration != UnrealTargetConfiguration.Shipping && Target.Configuration != UnrealTargetConfiguration.Test))
{
PrivateDependencyModuleNames.Add("GameplayDebugger");
Definitions.Add("WITH_GAMEPLAY_DEBUGGER=1");
}
else
{
Definitions.Add("WITH_GAMEPLAY_DEBUGGER=0");
}
}
}
51 changes: 51 additions & 0 deletions GameplayDebugger/OrfeasModule/Private/CustomGameplayDebugger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Fill out your copyright notice in the Description page of Project Settings.

#include "OrfeasModule.h"
#include "CustomGameplayDebugger.h"
#include "GDBlogPostCharacter.h"

#if WITH_GAMEPLAY_DEBUGGER

#include "DebugRenderSceneProxy.h"

FGameplayDebuggerCategory_Orfeas::FGameplayDebuggerCategory_Orfeas()
{
bShowOnlyWithDebugActor = false;
}

TSharedRef<FGameplayDebuggerCategory> FGameplayDebuggerCategory_Orfeas::MakeInstance()
{
return MakeShareable(new FGameplayDebuggerCategory_Orfeas());
}

void FGameplayDebuggerCategory_Orfeas::CollectData(APlayerController* OwnerPC, AActor* DebugActor)
{

if (DebugActor)
{
AGDBlogPostCharacter* Char = Cast<AGDBlogPostCharacter>(DebugActor);

if (Char)
{
//Store the data to our struct
Data.HP = Char->Health;
Data.Damage = Char->MaxDamage;
}
}
}

void FGameplayDebuggerCategory_Orfeas::DrawData(APlayerController* OwnerPC, FGameplayDebuggerCanvasContext& CanvasContext)
{
//Test print with white text
CanvasContext.Printf(TEXT("Test Print"));

CanvasContext.Printf(FColor::Yellow, TEXT("Yet again another print!"));

//Print the health data with green color
CanvasContext.Printf(TEXT("{green}HP: %s"), *FString::SanitizeFloat(Data.HP));

//Print the damage data with red color
CanvasContext.Printf(TEXT("{red}Damage: %s"), *FString::SanitizeFloat(Data.Damage));
}

#endif
45 changes: 45 additions & 0 deletions GameplayDebugger/OrfeasModule/Private/OrfeasModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "OrfeasModule.h"

DEFINE_LOG_CATEGORY(OrfeasModule);

#if WITH_GAMEPLAY_DEBUGGER
#include "GameplayDebugger.h"
#include "CustomGameplayDebugger.h"
#endif


#define LOCTEXT_NAMESPACE "FOrfeasModule"

void FOrfeasModule::StartupModule()
{
UE_LOG(OrfeasModule, Warning, TEXT("Orfeas module has started!"));

#if WITH_GAMEPLAY_DEBUGGER

//If the gameplay debugger is available, register the category and notify the editor about the changes
IGameplayDebugger& GameplayDebuggerModule = IGameplayDebugger::Get();

GameplayDebuggerModule.RegisterCategory("OrfeasCustomCategory", IGameplayDebugger::FOnGetCategory::CreateStatic(&FGameplayDebuggerCategory_Orfeas::MakeInstance), EGameplayDebuggerCategoryState::EnabledInGameAndSimulate);

GameplayDebuggerModule.NotifyCategoriesChanged();

#endif
}

void FOrfeasModule::ShutdownModule()
{
UE_LOG(OrfeasModule, Warning, TEXT("Orfeas module has shut down"));

#if WITH_GAMEPLAY_DEBUGGER
//If the gameplay debugger is available, unregister the category
if (IGameplayDebugger::IsAvailable())
{
IGameplayDebugger& GameplayDebuggerModule = IGameplayDebugger::Get();
GameplayDebuggerModule.UnregisterCategory("OrfeasCustomCategory");
}
#endif
}

#undef LOCTEXT_NAMESPACE

IMPLEMENT_MODULE(FOrfeasModule,OrfeasModule)
46 changes: 46 additions & 0 deletions GameplayDebugger/OrfeasModule/Public/CustomGameplayDebugger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"

#if WITH_GAMEPLAY_DEBUGGER

#include "GameplayDebuggerCategory.h"

//Forward declarations
class AActor;
class APlayerController;
class FGameplayDebuggerCanvasContext;

//The data we're going to print inside the viewport
struct FDataToPrint
{
float HP;
float Damage;
};

//The class of our custom gameplay debugger category
class ORFEASMODULE_API FGameplayDebuggerCategory_Orfeas : public FGameplayDebuggerCategory
{

protected:

//The data that we're going to print
FDataToPrint Data;

public:

FGameplayDebuggerCategory_Orfeas();

/** Creates an instance of this category - will be used on module startup to include our category in the Editor */
static TSharedRef<FGameplayDebuggerCategory> MakeInstance();

/** Collects the data we would like to print */
virtual void CollectData(APlayerController* OwnerPC, AActor* DebugActor) override;

/** Displays the data we collected in the CollectData function */
virtual void DrawData(APlayerController* OwnerPC, FGameplayDebuggerCanvasContext& CanvasContext) override;
};

#endif
Loading

0 comments on commit 8a7dbd0

Please sign in to comment.