Dear community, dear developers,
I am struggling for a day now with binding events from the Augmented Reality system into my game. I am trying to use the delegate’s BindUObject function to bind my function to the AR System.
ARBlueprint provides us with:
UARBlueprintLibrary::AddOnTrackableAddedDelegate_Handle(const &FOnTrackableFoundDelegate);
Am I right using this function to register my delegate for the event?
If so, would I then go ahead and bind a function to my delegate like this?
m_GeometryFoundHandle.BindUObject(this, &AMushroomRushARPawn::OnGeometryFound);
Sadly, despite the increase in numbers of tracked planes, properly presented by a console log in my Tick() function, &AMushroomRushARPawn::OnGeometryFound never gets called.
Can anybody point me out to a proper way of doing this, what am I missing?
#pragma once
#include "CoreMinimal.h"
#include "ARBlueprintLibrary.h"
#include "GameFramework/Pawn.h"
#include "MushroomRushARPawn.generated.h"
UCLASS()
class MUSHROOMRUSH_API AMushroomRushARPawn : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AMushroomRushARPawn();
FOnTrackableAddedDelegate m_GeometryFoundHandle;
UPROPERTY(EditAnywhere)
UARSessionConfig* m_SesseionConfig;
FOnTrackableRemovedDelegate m_GeometryLostHandle = FOnTrackableRemovedDelegate();
FOnTrackableUpdatedDelegate m_GeometryUpdatedHandle = FOnTrackableUpdatedDelegate();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION()
virtual void OnGeometryFound(UARTrackedGeometry* _obj);
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "MushroomRushARPawn.h"
// Sets default values
AMushroomRushARPawn::AMushroomRushARPawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
m_GeometryFoundHandle.BindUObject(this, &AMushroomRushARPawn::OnGeometryFound);
UARBlueprintLibrary::AddOnTrackableAddedDelegate_Handle(m_GeometryFoundHandle);
}
// Called when the game starts or when spawned
void AMushroomRushARPawn::BeginPlay()
{
Super::BeginPlay();
UARBlueprintLibrary::StartARSession(m_SesseionConfig);
}
// Called every frame
void AMushroomRushARPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (UARBlueprintLibrary::GetARSessionStatus().Status == EARSessionStatus::Running) {
GEngine->AddOnScreenDebugMessage(0, 0.1f, FColor::Green, FString::Printf(TEXT("ARSession is running")));
GEngine->AddOnScreenDebugMessage(1, 0.1f, FColor::Orange, FString::Printf(TEXT("Planes tracked %d"), UARBlueprintLibrary::GetAllTrackedPlanes().Num()));
GEngine->AddOnScreenDebugMessage(2, 0.1f, FColor::Orange, FString::Printf(TEXT("Total geometries tracked %d"), UARBlueprintLibrary::GetAllGeometries().Num()));
}
}
void AMushroomRushARPawn::OnGeometryFound(UARTrackedGeometry * _obj)
{
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, FString("Plane found: "+ _obj->GetName()));
}
// Called to bind functionality to input
void AMushroomRushARPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}