Why can't I call onFire(); - c++

I'm trying to implement weapon firing in my game!
My inheritance chain looks like this:
ACharacter -> AMainCharacter
AActor -> AWeapon -> ALaserGun
I'm trying to call a function in the ALaserGun class, but for some reason Weapon isn't of type ALaserGun. How can I use SpawnActor<>() to spawn an actor that's the same type as WeaponAtSpawnClass if I'm setting WeaponAtSpawnClass in blueprint? Right now, I'm spawning an AWeapon actor. I want to spawn a child of AWeapon which has OnFire() in it. The type of Weapon being the class stored in WeaponAtSpawnClass.
Some things that I've noted that might be causing the issue:
Weapon in MainCharacter.h is of type AWeapon*
SpawnActor<>() in Main Character.cpp uses AWeapon as the spawned type.
Would I have to change AWeapon in these statements to something else?
Here's my code:
MainCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MainCharacter.generated.h"
class AWeapon;
UCLASS()
class TEST_API AMainCharacter : public ACharacter
{
GENERATED_BODY()
public:
AWeapon* Weapon;
UPROPERTY(EditAnywhere)
TSubclassOf<AWeapon> WeaponAtSpawnClass;
void OnFire();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
MainCharacter.cpp
#include "MainCharacter.h"
#include "Weapon.h"
// Called when the game starts or when spawned
void AMainCharacter::BeginPlay()
{
Super::BeginPlay();
Weapon = GetWorld()->SpawnActor<AWeapon>(WeaponAtSpawnClass);
}
void AMainCharacter::OnFire()
{
Weapon->OnFire();
}
// Called to bind functionality to input
void AMainCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &AMainCharacter::OnFire);
}
Weapon.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Weapon.generated.h"
UCLASS()
class TEST_API AWeapon : public AActor
{
GENERATED_BODY()
};
There is no code in Weapon.cpp
LaserGun.h
#pragma once
#include "CoreMinimal.h"
#include "Weapon.h"
#include "LaserGun.generated.h"
UCLASS()
class TEST_API ALaserGun : public AWeapon
{
GENERATED_BODY()
public:
void OnFire();
};
LaserGun.cpp
#include "LaserGun.h"
void ALaserGun::OnFire()
{
UE_LOG(LogTemp, Warning, TEXT("Gun fired"))
}

AWeapon* Weapon; declares a pointer to any AWeapon. How whould the compiler know, that it points to a instance of a class that has some specific method?
Of course you could start casting Weapon to ALaserGun, but that's going to result in a mess. The better alternative is to declare the method in the interface AWeapon:
UCLASS()
class TEST_API AWeapon : public AActor
{
GENERATED_BODY()
public:
virtual OnFire() = 0;
};
And for good style declare that you override that method:
UCLASS()
class TEST_API ALaserGun : public AWeapon
{
GENERATED_BODY()
public:
void OnFire() override;
};
Naming is of course up to you, for example, if not every weapon can fire, name the method OnAttack or something, then call OnFire from OnAttack or something like that.

Related

Getting unknown function specifier "BluePrintImplementableEvent" error while compiling my C++ code

I am using UnrealEngine 4.27.2. While Compiling my C++ code i am getting this error " Unknown function specifier "BluePrintImplementableEvent" after creating a UFUNCTION.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "damageableActor.generated.h"
UCLASS()
class BASICS_API AdamageableActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AdamageableActor();
UFUNCTION(BlueprintImplementableEvent, Category = "Damage")
void onTakeAttack();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void takeAttack();
};

How to rotate bones in UE4 with C++?

I just want to create a node that simply specifies the bone name of the character and rotates it.
I want to move multiple bones, not just one bone, using the naming method.
I wrote the following codes.
MyCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
class USkeletalMeshComponent;
class UPoseableMeshComponent;
UCLASS()
class TESTPLUGIN_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMyCharacter();
FRotator RotationValue;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
private:
UPoseableMeshComponent* PoseableMesh;
};
MyCharacter.cpp
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MyCharacter.generated.h"
class USkeletalMeshComponent;
class UPoseableMeshComponent;
UCLASS()
class TESTPLUGIN_API AMyCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMyCharacter();
FRotator RotationValue;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
private:
UPoseableMeshComponent* PoseableMesh;
};
When I compile the above code and open it with a level blueprint, I see a node like the one in the figure below.
This node is not what I wanted.
I want to create a node that can connect to FinalAnimationPose as shown in the image below.
How can I create a node like this?
All I want to do is simply rotate the bones every second. I couldn't find this information.
I am a beginner in both UE4 and C ++. Any answer will help.
I am new to UE but I think it was the same question which was asked in UE community to rotate but it was for a single bone:
Here's the link to that: https://answers.unrealengine.com/questions/47930/how-to-rotate-bone-in-c.html

UNREAL ENGINE V4.21: Components not being initialized during constructor call for Character

We tried to initialize components inside our character's constructor. The code worked on v4.15 but not v4.21.
Here is our code (.h file and .cpp file respectively):
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "MainCharacter.generated.h"
UCLASS()
class VRET_API AMainCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AMainCharacter();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
private:
UPROPERTY()
class UCameraComponent * camera;
UPROPERTY()
class USceneComponent * VRroot;
};
#include "MainCharacter.h"
#include "Camera/CameraComponent.h"
#include "MotionControllerComponent.h"
#include "Runtime/Engine/Classes/Components/StaticMeshComponent.h"
#include "Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h"
// Sets default values
AMainCharacter::AMainCharacter()
{
// 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;
VRroot = CreateDefaultSubobject<USceneComponent>(TEXT("VRroot"));
VRroot->SetupAttachment(GetRootComponent());
camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
camera->SetupAttachment(VRroot);
}
The code compiles, but when we run the game our character's components aren't being initialized and we cannot find them in the editor during gameplay BUT the default character components (eg:capsule component) work and show properly.
This happened to me. Try to unparent your blueprint class from AMainCharacter to just ACharacter, and then again parent it from AMainCharacter.
It worked for me!

Unreal Engine 4: UShapeComponent gives the error 'identifier is undefined'. What can I do?

I am using Unreal 4.18 and Visual Studio 2017. Using certain class like UShapeComponent, UBoxComponent, USphereComponent gives identifier is undefined error while likes of USceneComponent, UStaticMeshComponent works just fine. What's wrong?
Here is my code:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class UNREALTUROIAL_API AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(EditAnywhere)
UStaticMeshComponent* PickUpMesh;
UPROPERTY(EditAnywhere)
UShapeComponent* PickUpBox;
UPROPERTY(EditAnywhere)
USceneComponent* PickUpRoot;
};
enter code hereYou can try adding this include to your .h file
#include <Runtime/Engine/Classes/Components/SphereComponent.h>

Inheriting base class, defining the base class' prototype method but calling child class' method from third object

I have a class called "Game", with prototypes functions "Update" and "Draw" but they're not defined. It's up to the object inheriting the "Game" object to override them. Is this possible?
Contents of "Game.h"
class Game // does it have to abstract/virtual?
{
public:
//General stuff for all games here
}
void Update(Game *game) = 0; // or make it virtual in someway
Contents of "MyGame.h"
#include "Game.h"
class MyGame : public Game
{
public:
// General stuff for my game
}
void Update(MyGame *game);
// Contents of "MyGame.cpp"
#include "MyGame.h"
void Update(MyGame *game) // does it have to be overriden/overloaded?
{
}
// Contents of "GameManager.h"
#include "Game.h"
class GameManager
{
public:
Game *game;
}
void Update(GameManager *manager);
// Contents of "GameManager.cpp"
#include "Game.h"
void Update(GameManager *manager)
{
Update(manager->game);
}
The key is the last method:
Why can't GameManager call MyGame Update() method when GameManager's Game object = MyGame and not Game?
Define Draw and Update as virtual methods in the base class Game.
class Game
{
public:
virtual void Draw() {};
};
class MyGame : public Game
{
public:
virtual void Draw() {}
};
void callDraw(Game* game)
{
game->Draw();
}
//...
Game* game = new MyGame;
callDraw(game);
The last call will call the method in MyGame although it's called on a Game pointer.