Download

Not Calling my function when another actor enters the collision sphere

Hey I am trying to make it so when another actor enters my collision sphere it does 2 functions, but it doesn’t call the second one.

Code for the character.h


// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "GameFramework/Character.h"
#include "Pickup.h"
#include "Interactables.h"
#include "RPGCharacter.generated.h"

UCLASS(Blueprintable)
class ARPGCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	ARPGCharacter();

	// Called every frame.
	virtual void Tick(float DeltaSeconds) override;

	/** Returns TopDownCameraComponent subobject **/
	FORCEINLINE class UCameraComponent* GetTopDownCameraComponent() const { return TopDownCameraComponent; }
	/** Returns CameraBoom subobject **/
	FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
	/** Returns CursorToWorld subobject **/
	FORCEINLINE class UDecalComponent* GetCursorToWorld() { return CursorToWorld; }

//------------------------------------------------------------ COLLISION SPHERE CODE-------------------------------------------------
	//Loot Sphere
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Collision Sphere")
		USphereComponent* PCollisionSphere;
	UFUNCTION()
		void TZ_Enter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

	UFUNCTION()
		void Loot_Pickup(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);



The CPP File



	//Player Collision Sphere
	PCollisionSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Player Collision Sphere")); // Declares the Collision Sphere
	PCollisionSphere->bGenerateOverlapEvents = true; // Allows the sphere to generate overlap events
	PCollisionSphere->SetSphereRadius(200.f); // Sets the radius
	PCollisionSphere->OnComponentBeginOverlap.AddDynamic(this, &ARPGCharacter::TZ_Enter); //Function called from this class
	PCollisionSphere->OnComponentBeginOverlap.AddDynamic(this, &ARPGCharacter::Loot_Pickup);
	PCollisionSphere->SetupAttachment(RootComponent); // Attaches the Collision Sphere to the root component what is it in this case I HAVE NO IDEA :D
	PCollisionSphere->SetHiddenInGame(false); // Shows the sphere in game (For debugging purposes)

void ARPGCharacter::TZ_Enter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (!OtherActor->IsA(ACharacter::StaticClass())) // Checks to see if the other actor is not a character
	{
		GEngine->AddOnScreenDebugMessage(1, 5.f, FColor::Purple, "Overlapped Some Shiz"); // Displays a debug message
	}
}

void ARPGCharacter::Loot_Pickup(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (!OtherActor->IsA(ACharacter::StaticClass()) && OtherActor->IsA(APickup::StaticClass()) )
	{
		GEngine->AddOnScreenDebugMessage(1, 5.f, FColor::Yellow, "Enterd");
		APickup* CurrentPickup = Cast<APickup>(OtherActor);
		CurrentPickup->onPickedup();
	}
}


The function i am trying to get working but isent :confused:




PCollisionSphere->OnComponentBeginOverlap.AddDynamic(this, &ARPGCharacter::Loot_Pickup);

void ARPGCharacter::Loot_Pickup(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (!OtherActor->IsA(ACharacter::StaticClass()) && OtherActor->IsA(APickup::StaticClass()) )
	{
		GEngine->AddOnScreenDebugMessage(1, 5.f, FColor::Yellow, "Enterd");
		APickup* CurrentPickup = Cast<APickup>(OtherActor);
		CurrentPickup->onPickedup();
	}
}


Even if i comment out this line of code (A function call) it still outputs the code for the function !!!




	PCollisionSphere->OnComponentBeginOverlap.AddDynamic(this, &ARPGCharacter::TZ_Enter); //Function called from this class



Thanks for the help in advanced
I get no compile errors btw :stuck_out_tongue:

Hey there,

i dont know if the functionpointer OnComponentBeginOverlap is intended to be used to assign multiple callbacks to it (really not sure). But what is the problem with the following code?




void ARPGCharacter::TZ_Enter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (!OtherActor->IsA(ACharacter::StaticClass())) // Checks to see if the other actor is not a character
	{
		GEngine->AddOnScreenDebugMessage(1, 5.f, FColor::Purple, "Overlapped Some Shiz"); // Displays a debug message
	}
        
        // Simply call additional functions ?!?
        Loot_Pickup(OverlappedComponent, OtherActor, OtherComp, OtherBodyIndex, bFromSweep, SweepResult);
}

void ARPGCharacter::Loot_Pickup(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (!OtherActor->IsA(ACharacter::StaticClass()) && OtherActor->IsA(APickup::StaticClass()) )
	{
		GEngine->AddOnScreenDebugMessage(1, 5.f, FColor::Yellow, "Enterd");
		APickup* CurrentPickup = Cast<APickup>(OtherActor);
		CurrentPickup->onPickedup();
	}
}



It never calls the second function and I
don’t know why,
please help. And yes I intended to have 2 callbacks.

Hey, your code works fine on my end. Though i use the Ctor for the SphereObject instantiation and i dont know where you put it.

However, doing a multicast to call the same object but different functions doesnt feel very useful. If you bind several other objects to it its fine then.




// in AMyClass.h

/* 
   UFUNCTION() 
    void Loot_Pickup(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

    UFUNCTION()
    void TZ_Enter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);

    UPROPERTY()
    USphereComponent* PCollider;
 */
AMyClass::AMyClass() // you could even use the objectinitializer version
{
        
        PCollider = CreateDefaultSubobject<USphereComponent>(TEXT("Collider")); // Declares the Collision Sphere	
        PCollider->bGenerateOverlapEvents = true; // Allows the sphere to generate overlap events
	PCollider->SetSphereRadius(200.f); // Sets the radius
	PCollider->OnComponentBeginOverlap.AddDynamic(this, &AMyClass::TZ_Enter); //Function called from this class
	PCollider->OnComponentBeginOverlap.AddDynamic(this, &AMyClass::Loot_Pickup);
	PCollider->SetupAttachment(RootComponent); // Attaches the Collision Sphere to the root component of this actor
	PCollider->SetHiddenInGame(false); // Shows the sphere in game (For debugging purposes)
}

void AMyClass::TZ_Enter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1, 3, FColor::Red, TEXT("TZ_ENTER"));
}

void AMyClass::Loot_Pickup(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	GEngine->AddOnScreenDebugMessage(-1, 3, FColor::Blue, TEXT("Loot_Pickup"));
}




Btw. are you sure the second is not called? Did you set breakpoints to make sure? Because you print in case your conditions is true otherwise nothing happened.

Alright thanks for the feedback, i am still new to UE4 so i dont know all the terms and what not, the code provided is just the code i added to the file none of the boiler plate i am going to get rid of the pickup function and move it all into the TZ_enter, I was not being very smart when i wrote this not realizing i could just use IF loops to branch the FNC. Thanks for the help :).

Thanks man i fixed the issue by taking your advice, i moved all the code into one FNC

code that works:



void ARPGCharacter::TZ_Enter(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{
	if (!OtherActor->IsA(ACharacter::StaticClass())) // Checks to see if the other actor is not a character
	{
		GEngine->AddOnScreenDebugMessage(1, 5.f, FColor::Purple, "NON-PICKUP-CLASS"); // Displays a debug message

		if(OtherActor->IsA(APickup::StaticClass()))
		{
			GEngine->AddOnScreenDebugMessage(1, 5.f, FColor::Yellow, "PICKUP CLASS");
			APickup* CurrentPickup = Cast<APickup>(OtherActor); // Converts OtherActor form class Actor to Pickup
			CurrentPickup->onPickedup(); // calls the Pickup class function onPickedup
		}

	}
}