Download

I'm needing a little bit of guidance to get started

Hey all,

Now I’m not new to programming, just new to Unreal having used unity since I left uni and feel I need some help getting into the Unreal engine.
I’ve read LOADS of the documentation, done tutorials, but still need a bit of help getting over the initial hump.

What I’m looking for is someone to walk me through a simple class, and from there I’ll have a hack about trying things.

The class I want is a sphere/hemisphere, that can’t be collided with but will act as a trigger. And would like it’s radius to be adjustable from the editor/blueprint.

Here is a Seriously basic start I’ve made, I tried a number of things myself and ended up crashing the editor and never recovering the project :smiley:

.h :


+// Fill out your copyright notice in the Description page of Project Settings.
+
+#pragma once
+
+#include "GameFramework/Actor.h"
+#include "Trap.generated.h"
+
+UCLASS()
+class SURVIVAL_API ATrap : public AActor
+{
+	GENERATED_BODY()
+	
+public:	
+	// Sets default values for this actor's properties
+	ATrap();
+
+	//Create the the containers for the base trap class
+	UPROPERTY(EditAnywhere, Category = "Trap Properties")
+		bool isActive;
+
+	UPROPERTY(EditAnywhere, Category = "Trap Properties")
+		float trapRadius;
+
+	// Called when the game starts or when spawned
+	virtual void BeginPlay() override;
+	
+	// Called every frame
+	virtual void Tick( float DeltaSeconds ) override;
+
+	
+	
+};

.cpp


+// Fill out your copyright notice in the Description page of Project Settings.
+
+#include "Survival.h"
+#include "Trap.h"
+
+
+// Sets default values
+ATrap::ATrap()
+{
+ 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
+	PrimaryActorTick.bCanEverTick = true;
+
+	//Set all the Default values
+	isActive = true;
+
+	trapRadius = 40.0f;
+
+}
+
+// Called when the game starts or when spawned
+void ATrap::BeginPlay()
+{
+	Super::BeginPlay();
+	
+}
+
+// Called every frame
+void ATrap::Tick( float DeltaTime )
+{
+	Super::Tick( DeltaTime );
+
+}
+

I honestly cannot see anything in there that would crash the editor. Do you have the error log on hand perhaps?

Hey,
This code isn’t what crashed the editor sorry, I abandoned that code and made a new class and got as far as I know what I was doing.
I was hoping people could give me direction as to how to proceed rather than debugging the crash

Oh okay, your best bet is to make a trap that works around a collision volume. Box, Sphere or Capsule component and have it link to a delegate that fires off the event you want.
You’ll need to look into delegates and adding components through C++ (both fairly easy, just take a bit of reading). I would post an example, but I’m away from my PC.

Thanks for your response,
Right now I’m also away from my development PC, but I’d like to get my head round this for tonight.

So I’ve changed the .cpp code to create a Sphere component to trigger the trap:


// Fill out your copyright notice in the Description page of Project Settings.

#include "Survival.h"
#include "Trap.h"


// Sets default values
ATrap::ATrap()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	//Set all the Default values
	isActive = true;

	trapRadius = 40.0f;

	// Collision Sphere for our player interaction.
        USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
        RootComponent = SphereComponent;
        SphereComponent->InitSphereRadius(trapRadius);
        SphereComponent->SetCollisionProfileName(TEXT("Trigger"));
	
}

// Called when the game starts or when spawned.
void ATrap::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame.
void ATrap::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}


I believe there’s no change to the .h for this.
Now the delegate is to trigger something in the colliding pawn yes?
I’ve read This with regards to implementing that, is that along the right lines?

Right, so I read through A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums

I Think I’ve got something:

.h:


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "GameFramework/Actor.h"
#include "Trap.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam( FActorBeginOverlapSignature, class AActor*, OtherActor );

UCLASS()
class SURVIVAL_API ATrap : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ATrap();

	//Create the the containers for the base trap class
	UPROPERTY(EditAnywhere, Category = "Trap Properties")
		bool isActive;

	UPROPERTY(EditAnywhere, Category = "Trap Properties")
		float trapRadius;
		
	//Function to be called on overlap
	UFUNCTION()
		void OnOverlap(AActor* OtherActor)

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	
	
};

.cpp:


// Fill out your copyright notice in the Description page of Project Settings.

#include "Survival.h"
#include "Trap.h"


// Sets default values
ATrap::ATrap()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	//Set all the Default values
	isActive = true;

	trapRadius = 40.0f;

	// Collision Sphere for our player interaction.
	USphereComponent* SphereComponent = CreateDefaultSubobject<USphereComponent>(TEXT("RootComponent"));
	RootComponent = SphereComponent;
	SphereComponent->InitSphereRadius(trapRadius);
	SphereComponent->SetCollisionProfileName(TEXT("Trigger"));
	
	//Delegate declaration.
	OnActorBeginOverlap.AddDynamic(this, &ATrap::OnOverlap);
}

// Called when the game starts or when spawned.
void ATrap::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame.
void ATrap::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );

}

//Do something with the other actor.
void ATrap::OnOverlap(AActor* OtherActor)
{
	
	
}