Collision in code

Hello everybody, i apologize in advance for any grammatical errors i from poland and yet learn english.
So i have problem with collision in Unreal Engine 4, here code Item.h


UCLASS()
class INVENTORY_API AItem : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AItem();
	float wielkosc;
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
		FString ItemName;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
		int32 Value;

	UFUNCTION(BlueprintImplementableEvent, Category="DMG")
		virtual void Used();

	UFUNCTION(BlueprintImplementableEvent,Category="DMGS")
		virtual void Dropped();

	void ReceiveActorBeginOverlap(class AActor* OtherActor);


		UPROPERTY(EditAnywhere)
		USphereComponent* Mesh;
		void PickedUp();
		UPROPERTY(EditAnywhere)
		UStaticMeshComponent* SphereVisual;
};

Item.cpp


AItem::AItem()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	Mesh = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
	RootComponent = Mesh;
	Mesh->SetSphereRadius(90.0f);
	Mesh->SetCollisionProfileName(TEXT("Pawn"));
	Mesh->SetSimulatePhysics(true);
OnActorBeginOverlap.AddDynamic(this, &AItem::ReceiveActorBeginOverlap);
	SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
	SphereVisual->AttachTo(RootComponent);
	wielkosc = 0.8f;
	static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
	if (SphereVisualAsset.Succeeded())
	{
		SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
		SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
		SphereVisual->SetWorldScale3D(FVector(wielkosc));
	}
}
void AItem::ReceiveActorBeginOverlap(class AActor* OtherActor)
{
	if (Mesh)
	{
		Mesh->DestroyComponent(); // physical item has been picked up, destroy its visible component
	}
}


i have a lot of problem with there, i don’t know what i make bad… I try create collision from 9 days and i can’t.

It would be easier if we knew exactly what sort of problems you are having - Crashes? Doesn’t overlap? Not visible?

Here’s how I set up one of my meshes to overlap:




APlayerPawnCombat::APlayerPawnCombat(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	CollisionStaticMeshComponent = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("CollisionMeshComponent0"));
	CollisionStaticMeshComponent->SetCollisionProfileName(FName("BlockAll"));
	CollisionStaticMeshComponent->Mobility = EComponentMobility::Movable;
	CollisionStaticMeshComponent->bGenerateOverlapEvents = true;
	CollisionStaticMeshComponent->SetSimulatePhysics(true);
	CollisionStaticMeshComponent->SetEnableGravity(false);
	CollisionStaticMeshComponent->SetNotifyRigidBodyCollision(true);
	RootComponent = CollisionStaticMeshComponent;
}


In Particular note that I do it in a different constructor (not APlayerPawnCombat() ) , and that I have bGenerateOverlapEvents = true;

And if i wanna create overlap i have will create event function y? And dont must create new dynamit?

I’m not sure I understand what you’re asking, but i’ve just noticed your header (.h) function has:



void ReceiveActorBeginOverlap(class AActor* OtherActor);


That should be:



virtual void ReceiveActorBeginOverlap(AActor* OtherActor) override;


The “virtual” and “override” keywords are required.

That is afaik not true. “override” is only a c++11 hint for the compiler to check for an equal method in a baseclass which can be overriden, resulting in a compiler error if none can be found, and the “virtual” keyword has no effect if a ReceiveActorBeginOverlap method in a AItem-derived class does not exist. Nevertheless, it is better coding style.

To the topic:

You want to check if an actor overlaps with your item.

You can use events in blueprints, or C++ events with dynamic delegates or use overloaded methods in derived classes.

As you are implementing in c++, and deriving from AActor, the fastest way would be to overload a base-XYZOverlap-method.

There is:

virtual void AActor::NotifyActorBeginOverlap(AActor* OtherActor);

So, in your code, I would suggest to rename “ReceiveActorBeginOverlap” to “NotifyActorBeginOverlap” and remove the “OnActorBeginOverlap.AddDynamic(this, &AItem::ReceiveActorBeginOverlap);” line.

Hope this helps

You are 100% correct , thanks for keeping me honest :slight_smile:

Of course, the ‘virtual’ keyword is only preferred if the method is actually meant to be overridden in a deriving class - too often people will label functions virtual when they’re not actually intending for people to inherit from the class. IIRC you don’t need to mark a function virtual for it to override a base class method that is a virtual function.

Still nothing, i srsly don’t know what i do bad…


AItem::AItem(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	Mesh = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("CollisionMeshComponent0"));
	Mesh->Mobility = EComponentMobility::Movable;
	Mesh->SetNotifyRigidBodyCollision(true);
	Mesh->SetSphereRadius(90.0f);
	Mesh->SetCollisionProfileName(TEXT("Pawn"));
	Mesh->OnComponentBeginOverlap.AddDynamic(this, &AItem::ReceiveActorBeginOverlap);
	Mesh->SetSimulatePhysics(true);
	Mesh->bGenerateOverlapEvents= true;
	RootComponent = Mesh;
	SphereVisual = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualRepresentation"));
	SphereVisual->AttachTo(RootComponent);
	wielkosc = 0.8f;
	//OnActorBeginOverlap.AddDynamic(this, &AItem::ReceiveActorBeginOverlap);
	static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
	if (SphereVisualAsset.Succeeded())
	{
		SphereVisual->SetStaticMesh(SphereVisualAsset.Object);
		SphereVisual->SetRelativeLocation(FVector(0.0f, 0.0f, -40.0f));
		SphereVisual->SetWorldScale3D(FVector(wielkosc));
	}
}

void AItem::ReceiveActorBeginOverlap(class AActor* OtherActor)
{
	Destroy();
}

.H


UCLASS()
class INVENTORY_API AItem : public AActor
{
	GENERATED_BODY()
		DECLARE_DELEGATE(Collision);
public:	
	// Sets default values for this actor's properties
	AItem(const FObjectInitializer& ObjectInitializer);
	float wielkosc;
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
		FString ItemName;

	UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Item)
		int32 Value;

	UFUNCTION(BlueprintImplementableEvent, Category="DMG")
		virtual void Used();

	UFUNCTION(BlueprintImplementableEvent,Category="DMGS")
		virtual void Dropped();
	
	void ReceiveActorBeginOverlap(class AActor* OtherActor);

		UPROPERTY(EditAnywhere)
		USphereComponent* Mesh;
		void PickedUp();
		UPROPERTY(EditAnywhere)
		UStaticMeshComponent* SphereVisual;
};

I’m still not sure what the problem is - you say it isn’t working, but I don’t know WHAT isn’t working. Are you getting errors? Is it crashing? Is your overlap function not being called?

Overlap function not begin called. I wanna made that, if Character = actor collision it actor has be destroy

Do both of the colliding actors have bGenerateOverlapEvents = true? It won’t be called if only one of them has that as true.