I’m trying to implement weapon holding by monsters. The C++ compiler of UE4/XCode warn me, that AActor doesn’t have the WeaponHolder
attribute. That’s current state of my code:
-
MeleeWeapon.h
#pragma once
#include “GameFramework/Actor.h”
#include “MeleeWeapon.generated.h”class AMonster;
UCLASS()
class LEARNUE4CPPANDACTORS_API AMeleeWeapon : public AActor
{
GENERATED_UCLASS_BODY()// The amount of damage attacks do UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = MeleeWeapon) float AttackDamage; // A list of things the melee weapon already hit this swing? // Ensures each thing sword passes thru only gets hit once. TArray<AActor*> ThingsHit; // Prevents damage from occurring on frames where the sword is not swinging. bool Swinging; // "Stop hitting yourself" - used to check if the actor holding // the weapon is hitting himself AMonster* WeaponHolder; // The sphere you collide with to pick item up UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = MeleeWeapon) UBoxComponent* ProxBox; UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = MeleeWeapon) UStaticMeshComponent* Mesh; UFUNCTION(BlueprintNativeEvent, Category = Collision) void Prox( AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult ); void Swing(); void Rest();
};
-
Monster.h
#pragma once
#include “GameFramework/Character.h”
#include “Monster.generated.h”UCLASS()
class LEARNUE4CPPANDACTORS_API AMonster : public ACharacter
{
GENERATED_UCLASS_BODY()UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=MonsterProperties) float Speed; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=MonsterProperties) float HitPoints; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=MonsterProperties) int32 Experience; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=MonsterProperties) UClass* BPLoot; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=MonsterProperties) UClass* BPMeleeWeapon; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=MonsterProperties) float BaseAttackDamage; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=MonsterProperties) float AttackTimeout; UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = MonsterProperties) float TimeSinceLastStrike; UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Collision) USphereComponent* SightSphere; UPROPERTY(VisibleDefaultsOnly, BlueprintReadOnly, Category = Collision) USphereComponent* AttackRangeSphere; // The melee weapon instance (set if the character is using a melee weapon) AActor* MeleeWeapon; // Called every frame virtual void Tick(float DeltaSeconds) override; // Utility functions inline bool isInAttackRange(float d) {return d < AttackRangeSphere->GetScaledSphereRadius();} inline bool isInSightRange(float d) {return d < SightSphere->GetScaledSphereRadius();} virtual void PostInitializeComponents() override;
};
-
Monster.cpp
#include “LearnUE4CppAndActors.h”
#include “Monster.h”
#include “Avatar.h”
#include “MeleeWeapon.h”AMonster::AMonster(const class FObjectInitializer& PCIP): Super(PCIP)
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;Speed = 20; HitPoints = 20; Experience = 0; BPLoot = nullptr; BPMeleeWeapon = nullptr; AttackTimeout = 1.5f; TimeSinceLastStrike = 0; MeleeWeapon = nullptr; SightSphere = PCIP.CreateDefaultSubobject<USphereComponent>(this, TEXT("SightSphere")); SightSphere->AttachTo(RootComponent); AttackRangeSphere = PCIP.CreateDefaultSubobject<USphereComponent>(this, TEXT("AttackRangeSphere")); AttackRangeSphere->AttachTo(RootComponent);
}
// some implemented functionality …
void AMonster::PostInitializeComponents()
{
Super::PostInitializeComponents();// Instantiate the melee weapon if a bp was selected if( BPMeleeWeapon ) { MeleeWeapon = ()->SpawnActor<AMeleeWeapon>(BPMeleeWeapon, FVector(0), FRotator(0)); if(MeleeWeapon) { MeleeWeapon->WeaponHolder = this; // <-- there compiler generates an error const USkeletalMeshSocket *socket = GetMesh()->GetSocketByName("RightHandSocket"); socket->AttachActor( MeleeWeapon, GetMesh()); } else { FString message = GetName() + FString(" cannot instantiate meleeweapon ") + BPMeleeWeapon->GetName(); GEngine->AddOnScreenDebugMessage(0, 5.f, FColor::Yellow, message); } }
}
So, this is the error, which I was taken from the compiler:
Info Total build time: 60,11 seconds
Info Setting up Mono
Info /Users/Shared/UnrealEngine/4.11/Engine /Users/Shared/UnrealEngine/4.11/Engine/Binaries/Mac
Info Compiling game modules for hot reload
Info Compiling with MacOSX SDK 10.11
Info Parsing headers for LearnUE4CppAndActorsEditor
Info Running UnrealHeaderTool "/Users/savicvalera/Documents/Unreal Projects/LearnUE4CppAndActors/LearnUE4CppAndActors.uproject" "/Users/savicvalera/Documents/Unreal Projects/LearnUE4CppAndActors/Intermediate/Build/Mac/LearnUE4CppAndActorsEditor/Development/UnrealHeaderTool.manifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -installed
Info Reflection code generated for LearnUE4CppAndActorsEditor in 12,3825563 seconds
Info Performing 5 actions (4 in parallel)
Info [3/5] Compile MeleeWeapon.cpp
Info [2/5] Compile Monster.cpp
Info [1/5] Compile LearnUE4CppAndActors.generated.cpp
Info /Users/savicvalera/Documents/Unreal Projects/LearnUE4CppAndActors/Source/LearnUE4CppAndActors/Monster.cpp:70:26: error: no member named 'WeaponHolder' in 'AActor'
Info MeleeWeapon->WeaponHolder = this;
Info ~~~~~~~~~~~ ^
Info 1 error generated.
Info -------- End Detailed Actions Stats -----------------------------------------------------------
Info ERROR: UBT ERROR: Failed to produce item: /Users/savicvalera/Documents/Unreal Projects/LearnUE4CppAndActors/Binaries/Mac/UE4Editor-LearnUE4CppAndActors-3898.dylib
Info
Info Running Mono...
Info
What I doing wrong there? All required fields were described for AMeleeWeapon and Monster classes.
P.S. “How to” for this game features are described in the “Learning C++ by creating games with UE4” by William Sherif. However that functionality described for older versions of Unreal Engine.