Thanks for the understanding.
I just modified my codes, and I got some errors. I put my codes below.
Also, I am not sure the last part of what you told me. (from After then in your PickupActor BeginOverlap,) What should I do.
As I said above, I put my codes here. PLease check them and tell me what I should do next. The codes I just added are with some comments above the code like ‘I just added Oct 4.’ Actually, I have not deleted some codes. If possible, could you tell me which codes I should delete. I am kind of afraid delete all of the commented off code.
Character header
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "InputActionValue.h"
#include "CoinPickupTutorialCharacter.generated.h"
UCLASS(config=Game)
class ACoinPickupTutorialCharacter : public ACharacter
{
GENERATED_BODY()
/** Camera boom positioning the camera behind the character */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;
/** Follow camera */
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;
/** MappingContext */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputMappingContext* DefaultMappingContext;
/** Jump Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* JumpAction;
/** Move Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* MoveAction;
/** Look Input Action */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Input, meta = (AllowPrivateAccess = "true"))
class UInputAction* LookAction;
//code I just added(Oct4th)
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = “Counter”)
uint16 CoinAmount;
public:
ACoinPickupTutorialCharacter();
protected:
/** Called for movement input */
void Move(const FInputActionValue& Value);
/** Called for looking input */
void Look(const FInputActionValue& Value);
protected:
// APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
// To add mapping context
virtual void BeginPlay();
public:
/** Returns CameraBoom subobject **/
FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
/** Returns FollowCamera subobject **/
FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
};
character cpp
#include "CoinPickupTutorialCharacter.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
//////////////////////////////////////////////////////////////////////////
// ACoinPickupTutorialCharacter
ACoinPickupTutorialCharacter::ACoinPickupTutorialCharacter()
{
// Set size for collision capsule
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Configure character movement
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
GetCharacterMovement()->RotationRate = FRotator(0.0f, 500.0f, 0.0f); // ...at this rotation rate
// Note: For faster iteration times these variables, and many more, can be tweaked in the Character Blueprint
// instead of recompiling to adjust them
GetCharacterMovement()->JumpZVelocity = 700.f;
GetCharacterMovement()->AirControl = 0.35f;
GetCharacterMovement()->MaxWalkSpeed = 500.f;
GetCharacterMovement()->MinAnalogWalkSpeed = 20.f;
GetCharacterMovement()->BrakingDecelerationWalking = 2000.f;
// Create a camera boom (pulls in towards the player if there is a collision)
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 400.0f; // The camera follows at this distance behind the character
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
// Create a follow camera
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
// are set in the derived blueprint asset named ThirdPersonCharacter (to avoid direct content references in C++)
}
void ACoinPickupTutorialCharacter::BeginPlay()
{
// Call the base class
Super::BeginPlay();
//i just added code(Oct 4) got error
ColliderComponent->OnComponentBeginOverlap.AddDynamic(
this, &ACPT_CoinPickupActor::OnBeginOverlapComponentEvent
);
//Add Input Mapping Context
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
{
Subsystem->AddMappingContext(DefaultMappingContext, 0);
}
}
}
//////////////////////////////////////////////////////////////////////////
// Input
void ACoinPickupTutorialCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
// Set up action bindings
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(PlayerInputComponent)) {
//Jumping
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Triggered, this, &ACharacter::Jump);
EnhancedInputComponent->BindAction(JumpAction, ETriggerEvent::Completed, this, &ACharacter::StopJumping);
//Moving
EnhancedInputComponent->BindAction(MoveAction, ETriggerEvent::Triggered, this, &ACoinPickupTutorialCharacter::Move);
//Looking
EnhancedInputComponent->BindAction(LookAction, ETriggerEvent::Triggered, this, &ACoinPickupTutorialCharacter::Look);
}
}
void ACoinPickupTutorialCharacter::Move(const FInputActionValue& Value)
{
// input is a Vector2D
FVector2D MovementVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
// find out which way is forward
const FRotator Rotation = Controller->GetControlRotation();
const FRotator YawRotation(0, Rotation.Yaw, 0);
// get forward vector
const FVector ForwardDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
// get right vector
const FVector RightDirection = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
// add movement
AddMovementInput(ForwardDirection, MovementVector.Y);
AddMovementInput(RightDirection, MovementVector.X);
}
}
void ACoinPickupTutorialCharacter::Look(const FInputActionValue& Value)
{
// input is a Vector2D
FVector2D LookAxisVector = Value.Get<FVector2D>();
if (Controller != nullptr)
{
// add yaw and pitch input to controller
AddControllerYawInput(LookAxisVector.X);
AddControllerPitchInput(LookAxisVector.Y);
}
}
//i just added code(Oct 4) got error
void ACPT_CoinPickupActor::AddCoinByValue(uint8 Value)
{
Coin += Value;
}
coinpickupactor header
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraComponent.h"
#include "CPT_CoinPickupActor.generated.h"
class UStaticMeshComponent;
class URotatingMovementComponent;
class USphereComponent;
class UNiagaraSystem;
class USoundBase;
UCLASS()
class COINPICKUPTUTORIAL_API ACPT_CoinPickupActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ACPT_CoinPickupActor();
UFUNCTION()
void OnBeginOverlapComponentEvent(
UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult
);
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Counter")
uint16 CoinAmount;
///???
//add player coin amount by some value
//void AddCoinByValue(uint8 Value);
/*
UFUNCTION()
virtual void void OnSphereBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, Uprimi...)
In my case, what should it look like??
*/
protected:
//Constructor
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
TObjectPtr<UStaticMeshComponent> MeshComponent;
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
TObjectPtr<URotatingMovementComponent> RotatingMovementComponent;
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
TObjectPtr<USphereComponent> ColliderComponent;
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
TObjectPtr<UNiagaraSystem> OnPickupEffect;
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
TObjectPtr<USoundBase> PickSound;
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
float VolumeMultiplier{ 0.5 };
UPROPERTY(EditDefaultsOnly, Category = "Coin Pickup Tutorial")
float PickEffectSpawnOffset{ 90 };
//coint that already picked up by player
//UPROPERTY(VisibleAnywhere, Category = "Coin Pickup Tutorial")
//uint32 Coin;
//The codes I added for the counter
//the conin value
//UPROPERTY(EditAnywhere, Category = "Coin")
//uint8 CoinValue;
//Apickup_Coin();
/*
void OnSphereBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, Uprimi...)
In my case, what should it look like???*/
};
coinpickup cpp
#include "CPT_CoinPickupActor.h"
#include "Components/SphereComponent.h"
#include "GameFramework/Character.h"
#include "GameFramework/RotatingMovementComponent.h"
#include "Kismet/GameplayStatics.h"
#include "NiagaraFunctionLibrary.h"
#include "NiagaraComponent.h"
// Sets default values
ACPT_CoinPickupActor::ACPT_CoinPickupActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
ColliderComponent = CreateDefaultSubobject<USphereComponent>("ColliderComponent");
SetRootComponent(ColliderComponent);
ColliderComponent->SetGenerateOverlapEvents(true);
ColliderComponent->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
ColliderComponent->SetCollisionResponseToAllChannels(ECR_Ignore);
ColliderComponent->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>("MeshComponent");
MeshComponent->SetupAttachment(ColliderComponent);
MeshComponent->SetCollisionEnabled(ECollisionEnabled::NoCollision);
MeshComponent->SetCollisionResponseToAllChannels(ECR_Ignore);
MeshComponent->SetGenerateOverlapEvents(false);
RotatingMovementComponent = CreateDefaultSubobject<URotatingMovementComponent>("RotatingMovementComponent");
//coin counter
//Coin = 0;
}
void ACPT_CoinPickupActor::OnBeginOverlapComponentEvent(
UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult
)
{
if (!Cast<ACharacter>(OtherActor)) return;
if (PickSound)
{
UGameplayStatics::PlaySoundAtLocation(
this, PickSound, GetActorLocation(), VolumeMultiplier);
}
if (OnPickupEffect)
{
const FVector Offset = GetActorUpVector() * PickEffectSpawnOffset;
UNiagaraFunctionLibrary::SpawnSystemAtLocation(
this, OnPickupEffect, OtherActor->GetActorLocation() + Offset);
}
Destroy();
}
//void ACPT_CoinPickupActor::AddCoinByValue(uint8 Value)
//{
// Coin += Value;
//}
/*
void ACPT_CoinPickupActor::OnBeginOverlapComponentEvent(
UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult
)
{
Super::OnSphereBegiOveralap(OverlappedComponent, OtherActor,OtherComp, OtherBodyIndex, bFromSweep, SweepResult)
AE_PickupableCharacter* Char = Cast<AE_PickupableCharacter>(OtherActor);
if(Char)
{
Char-> AddCoinByValue(CoinValue);
Destroy();
}
}
CPT_CoinPickupActor::Apickup_Coin()
{
CoinValue = 1;
}
*/
build file
using System;
using System.IO;
using UnrealBuildTool;
public class CoinPickupTutorial : ModuleRules
{
public CoinPickupTutorial(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "EnhancedInput" });
PrivateDependencyModuleNames.AddRange(new string[] { "Niagara" });
}
}