0
\$\begingroup\$

I was wondering if someone could help me resolve a small roadblock I encountered aha... Essentially, in my Pickup.cpp file you can see that I'm trying to establish a behaviour wherein the object being observed destructs under direct observation.

The issue I'm having at the moment is that the values returned by the "actor being hit" and "actor meant to be hit" are different. The actor meant to be hit returns the value for an InteractableObject, whereas the actor hit returns the value, I assume, for a Pickup. I was wondering, for what reason is the cast actor value an InteractableObject-type, rather than a Pickup-type? (Is it because I haven't explicitly declared a constructor for the Pickup?)

Interactable .H file:

UCLASS()
class SANDBOXGAME_API AInteractableObject : public AActor
{
    GENERATED_BODY()

public: 
    // Sets default values for this actor's properties
    AInteractableObject();

        // Called on game start
        virtual void BeginPlay() override;
        // Called every frame
        virtual void Tick(float DeltaTime) override;

    // Macros for being editable in the interface builder (WYSIWYG)
    UPROPERTY(EditAnywhere, Category = "InteractableProperties")
        USceneComponent *interactableRoot;
    UPROPERTY(EditAnywhere, Category = "InteractableProperties")
        class UStaticMeshComponent *interactableMesh;
    UPROPERTY(EditAnywhere, Category = "InteractableProperties")
        class UBoxComponent *interactableBox;
    UPROPERTY(EditAnywhere, Category = "InteractableProperties")
        FString interactableHelpText;

    /*
    - Configures player interaction with the object (When a player enters the objects interaction radius)
    - Adds a macro for enabling function specifiers (Such as server-only functions)
    */
    UFUNCTION()
        void playerInteraction(UPrimitiveComponent *hitComponent, AActor *interactingActor, UPrimitiveComponent *otherComponent, int32 interactingActorBodyIndex, bool isDetected, const FHitResult & detectionResult);
    /*
    - Configures player uninteraction with the object (When a player leaves the objects interaction radius)
    - Adds a macro for enabling function specifiers (Such as server-only functions)
    */
    UFUNCTION()
        void playerUninteraction(UPrimitiveComponent *hitComponent, AActor *interactingActor, UPrimitiveComponent *otherComponent, int32 interactingActorBodyIndex);

        // Toggles item glow
        void toggleGlow(bool glowing);

        // Supports additional interaction behaviours implemented by the base and derived classes
        virtual void interactImplementation();

        // Used to start raycasting
        bool checkingForDirectView;
};

Interactable .CPP File:

AInteractableObject::AInteractableObject()
{
    PrimaryActorTick.bCanEverTick = true;

    // Declares the string value of the help text
    interactableHelpText = "Press E";

    // Initializes the root component
    interactableRoot = CreateDefaultSubobject<USceneComponent>(TEXT("interactableRoot"));
    RootComponent = interactableRoot;

    // Initializes the static mesh component (mesh) and attaches it to the root component
    interactableMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("interactableMesh"));
    interactableMesh->AttachToComponent(interactableRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);

    // Initializes the box component (box trigger) and adds the dynamic functions
    interactableBox = CreateDefaultSubobject<UBoxComponent>(TEXT("interactableBox"));
    interactableBox->SetGenerateOverlapEvents(true);
    interactableBox->SetWorldScale3D(FVector(2.0f, 2.0f, 3.0f));
    interactableBox->AttachToComponent(interactableRoot, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
    interactableBox->OnComponentBeginOverlap.AddDynamic(this, &AInteractableObject::playerInteraction);
    interactableBox->OnComponentEndOverlap.AddDynamic(this, &AInteractableObject::playerUninteraction);
}

// Defines how the prop outline (glow) is attached to an in-game prop, when the outline (glow) is activated.
void AInteractableObject::toggleGlow(bool isGlowing)
{
    interactableMesh->SetRenderCustomDepth(isGlowing);
}

// Defines the behaviour caused by the player when they overlap the props influence cube. 
void AInteractableObject::playerInteraction(UPrimitiveComponent *hitComponent, AActor *interactingActor, UPrimitiveComponent *otherComponent, int32 interactingActorBodyIndex, bool isDetected, const FHitResult & detectionResult)
{
    toggleGlow(true);
    checkingForDirectView = true;
}

// Defines the behaviour caused by the player when they exit the props influence cube.
void AInteractableObject::playerUninteraction(UPrimitiveComponent *hitComponent, AActor *interactingActor, UPrimitiveComponent *otherComponent, int32 interactingActorBodyIndex)
{
    toggleGlow(false);
    checkingForDirectView = false;
}

void AInteractableObject::interactImplementation() 
{
}

void AInteractableObject::BeginPlay()
{
    Super::BeginPlay();
}

void AInteractableObject::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

    interactImplementation();
}

Pickup .H File:

UCLASS()
class SANDBOXGAME_API APickup : public AInteractableObject
{
    GENERATED_BODY()

public:
    // Supports additional interaction behaviours implemented by the base and derived classes
    virtual void interactImplementation() override;
};

Pickup .CPP File:

// Defines the behaviour caused when the player interacts with the prop.
void APickup::interactImplementation()
{
    if (checkingForDirectView)
    {
        ASandboxGameCharacter *interactingCharacter = Cast<ASandboxGameCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));

        if (interactingCharacter->returnRaycastActor() != nullptr)
        {
            GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, FString::Printf(TEXT("The actor being hit is a: %s"), interactingCharacter->returnRaycastActor()));
            GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, FString::Printf(TEXT("The actor being hit is meant to be a: %s"), Cast<AActor>(this)));

            if (Cast<AActor>(this) == interactingCharacter->returnRaycastActor())
            {
                Destroy();
            }
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.