But what error do you get?
Hi guys)) I’m working on my project with some help from book “Learning C++ by creating games in unreal engine 4”. Book was written for older versions of engine and right now I have a problem with MeleeWeapon.cpp file in lines
if (Swinging && OtherActor != WeaponHolder && !ThingsHit.Contains(OtherActor)) and OtherActor->TakeDamage(AttackDamage + WeaponHolder->BaseAttackDamage, FDamageEvent(), NULL, this);
// Fill out your copyright notice in the Description page of Project Settings.
#include "Viking_Test.h"
#include "MeleeWeapon.h"
AMeleeWeapon::AMeleeWeapon(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.
PrimaryActorTick.bCanEverTick = true;
AttackDamage = 16;
Swinging = false;
WeaponHolder = NULL;
Mesh = ObjectInitializer.CreateDefaultSubobject<UStaticMeshComponent>(this,TEXT("Mesh"));
RootComponent = Mesh;
ProxBox = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this,TEXT("ProxBox"));
ProxBox->OnComponentBeginOverlap.AddDynamic(this, &AMeleeWeapon::Prox);
ProxBox->AttachTo(RootComponent);
}
// Called when the game starts or when spawned
void AMeleeWeapon::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMeleeWeapon::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void AMeleeWeapon::Prox_Implementation(AActor* OtherActor, UPrimitiveComponent* OtherComp,
int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
{
//dont hit non root components
if (OtherComp != OtherActor->GetRootComponent())
{
return;
}
//avoid hitting things while sword isnt swinging
//avoid hitting yourself
//avoid hitting OtherActor twice
if (Swinging && OtherActor != WeaponHolder && !ThingsHit.Contains(OtherActor))
{
OtherActor->TakeDamage(AttackDamage + WeaponHolder->BaseAttackDamage, FDamageEvent(), NULL, this);
ThingsHit.Add(OtherActor);
}
}
void AMeleeWeapon::Swing()
{
ThingsHit.Empty(); // empty the list
Swinging = true;
}
void AMeleeWeapon::Rest()
{
ThingsHit.Empty();
Swinging = false;
}
Here is BotCharacter.cpp code:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Viking_Test.h"
#include "BotCharacter.h"
#include "Viking_TestCharacter.h"
ABotCharacter::ABotCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
Speed = 20;
HitPoints = 20;
Experience = 0;
BPLoot = NULL;
BaseAttackDamage = 16;
AttackTimeout = 2.f;
TimeSinceLastStrike = 0;
SightSphere = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this,
TEXT("SightSphere"));
SightSphere->AttachTo(RootComponent);
AttackRangeSphere = ObjectInitializer.CreateDefaultSubobject <USphereComponent>(this,
TEXT("AttackRangeSphere"));
AttackRangeSphere->AttachTo(RootComponent);
}
// Called when the game starts or when spawned
void ABotCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ABotCharacter::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
// basic intel : move the monster towards the player
AViking_TestCharacter *viking = Cast<AViking_TestCharacter>(UGameplayStatics::GetPlayerPawn((), 0));
if (!viking) return;
FVector toPlayer = viking->GetActorLocation() - GetActorLocation();
float distancetoPlayer = toPlayer.Size();
// If the player is not in the SightSphere of the monster, go back
if (distancetoPlayer > SightSphere->GetScaledSphereRadius())
{
return;
}
//toPlayer.Normalize(); // reduce to unit vector
toPlayer /= distancetoPlayer; // normalizes the vector
// Actually move the monster towards the player a bit
AddMovementInput(toPlayer, Speed*2);
// At least face the target
//Gets you the rotator to turn something that looks in the `toPlayer` direction
FRotator toPlayerRotation = toPlayer.Rotation();
toPlayerRotation.Pitch = 0; // 0 off the pitch
RootComponent->SetWorldRotation(toPlayerRotation);
}
// Called to bind functionality to input
void ABotCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
}
Sorry for late answer.
С2446 !=: no conversion “ABotCharacter*” in “AActor*”
C2440 !=: can not be converted “ABotCharacter*” in “AActor*”
С2027 the use of indeterminate type “ABotCharacter*”
С2227 the expression on the left of “->BaseAttackDamage” should indicate the type of class, structure or union or a generic type
If your WeaponHolder is a ABotCharacter you need to include BotCharacter.h in MeleeWeapon.cpp.
Otherwise your compiler can’t find the definition of BaseAttackDamage
You should also have a forward declaration in your .h like: class ABotCharacter;
Thank you))) I just forgot to add #include “BotCharacter.h” and that’s all. Thank you again))