I am brand new to C++ as of this weekend, and I'm struggling on a very core concept, as I'm unfamiliar with header files and the implementation files being separated like this.
I want to have a Class, let's call it "WorldCharacter".
And this Class, should contain lots of information about a character within a level.
However, for the sake of conciseness, I would like to split up this Class to include a number of other classes, like "WorldCharacterHealth", and "WorldCharacterSkills" etc.
What I would like to do then, is attach this WorldCharacter class to an Actor, as an ActorCompoent, and it should display WorldCharacterHealth and WorldCharaacterSkills also.
However, after a day and a bit of trying to study ANY documentation to find this, I cannot find the answer to this solution.
Header File
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "WorldCharacter.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class GAME_API_NAME UWorldCharacter : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UWorldCharacter();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};
CPP File
#include "WorldCharacter.h"
// I thought perhaps I could include "WorldCharacterHealth.h" here (see below)
// Sets default values for this component's properties
UWorldCharacter::UWorldCharacter()
{
// and then manipulate values like health or armour down here?
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UWorldCharacter::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void UWorldCharacter::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}
Related
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
So I am a beginner with game programming in UE4, just starting out with c++. The code below worked perfectly before I attached a springcomponent to my mesh. This is a simple Pawn class that I created. I should also mention that once I included the spring component and tested it and it did not work, my computer shut down accidently. It give the same error repeatedly for any class I create, I have tried making a new class and applying the code.
Please see Image for the error VS 2017 gives when I try to debug
Below is the RollingStone.cpp file code.
#include "RollingStone.h"
#include "Classes/Components/InputComponent.h"
#include "Classes/GameFramework/FloatingPawnMovement.h"
#include "Classes/Camera/CameraComponent.h"
// Sets default values
ARollingStone::ARollingStone()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
FloatingPawnMovement = CreateDefaultSubobject<UFloatingPawnMovement>("PawnMoevement");
StaticMesh = CreateDefaultSubobject<UStaticMeshComponent>("StaticMeshComponeent");
Camera = CreateDefaultSubobject<UCameraComponent>("CameraComponeent");
Camera->SetRelativeLocation(FVector(-500.f, 0.f, 0.f));
Camera->SetupAttachment(StaticMesh);
SetRootComponent(StaticMesh);
bUseControllerRotationYaw = true;
bUseControllerRotationPitch = true;
}
// Called when the game starts or when spawned
void ARollingStone::BeginPlay()
{
Super::BeginPlay();
}
void ARollingStone::MoveForward(float Amount)
{
FloatingPawnMovement->AddInputVector(GetActorForwardVector()* Amount);
}
void ARollingStone::MoveRight(float Amount)
{
FloatingPawnMovement->AddInputVector(GetActorRightVector()* Amount);
}
void ARollingStone::Turn(float Amount)
{
AddControllerYawInput(Amount);
}
void ARollingStone::Lookup(float Amount)
{
AddControllerPitchInput(Amount);
}
// Called every frame
void ARollingStone::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void ARollingStone::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &ARollingStone::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &ARollingStone::MoveRight);
PlayerInputComponent->BindAxis("Turn", this, &ARollingStone::Turn);
PlayerInputComponent->BindAxis("LookUp", this, &ARollingStone::Lookup);
}
Below is the RollingStone.h file code:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "RollingStone.generated.h"
UCLASS()
class MYPROJECT_API ARollingStone : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
ARollingStone();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
void MoveForward(float Amount);
void MoveRight(float Amount);
void Turn(float Amount);
void Lookup(float Amount);
class UFloatingPawnMovement* FloatingPawnMovement;
UPROPERTY(EditAnywhere, Category = "Components")
UStaticMeshComponent* StaticMesh;
UPROPERTY(EditAnywhere, Category = "Components")
class UCameraComponent* Camera;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
Will appreciate you taking the time to help me. I am now very sure that the accidental shutdown caused this since its not working even after I stop using the springarmcomponent, you can see it is not there in the code and this previously working code now does not even debug.
I am using visual studio 2017 by the way!
Thank you!
Looking forward to a solution.
I'm new to Unreal Engine. I'm trying to create a basic game where you control a ball and roll around, trying not to fall out of a tube and stuff. My ball looks like this:
PlayerBall.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "PlayerBall.generated.h"
UCLASS()
class ROLLINGBALL_API APlayerBall : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
APlayerBall();
UPROPERTY(VisibleAnywhere, Category="Mesh")
UStaticMeshComponent* Mesh;
UPROPERTY(VisibleAnywhere, Category="Camera")
class UCameraComponent* Camera;
protected:
// Called when the game starts or when spawned
void BeginPlay() override;
public:
// Called every frame
void Tick(float DeltaTime) override;
// Called to bind functionality to input
void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
PlayerBall.cpp
#include "PlayerBall.h"
#include "Camera/CameraComponent.h"
// Sets default values
APlayerBall::APlayerBall() {
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
Mesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
const ConstructorHelpers::FObjectFinder<UStaticMesh> SphereRef(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere'"));
Mesh->SetStaticMesh(SphereRef.Object);
Mesh->SetSimulatePhysics(true);
Mesh->SetEnableGravity(true);
SetRootComponent(Mesh);
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetRelativeLocation(FVector(-500.0f, 0.0f, 0.0f));
Camera->SetupAttachment(RootComponent);
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
void APlayerBall::BeginPlay() {
Super::BeginPlay();
}
// Called every frame
void APlayerBall::Tick(float DeltaTime) {
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void APlayerBall::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) {
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
I also have some other obstacles and floor. However, I also want some obstacle that kills you if it hits you. I want some sort of method in the APlayerBall class that is called when it is hit, then I can check the type of actor that is hit. What can I do? What component do I use?n I looked for an hour in the documentation, but I couldn't find anything.
I tried adding the RecieveHit method, however it says that it doesn't override from a super class. I added it like this:
void ReceiveHit
(
class UPrimitiveComponent * MyComp,
AActor * Other,
class UPrimitiveComponent * OtherComp,
bool bSelfMoved,
FVector HitLocation,
FVector HitNormal,
FVector NormalImpulse,
const FHitResult & Hit
) override;
What you want to be doing is binding to the AActor::OnActorHit event with OnActorHit.AddDynamic(this, &FunctionPointer).
The RecieveHit function is internal and will be what broadcasts this event.
That would be AActor::ReceiveHit, since PlayerBall inherits from APawn which inherits from AActor.
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!
I know this might be a question for gamedev but since Im having the problem in Visual studio Im putting it here.
first, here are 2 source code snippets
code chunk 1
#pragma once
#include "Components/SceneComponent.h"
#include "CanonLauncher.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BLOCKASSAULT_API UCanonLauncher : public USceneComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UCanonLauncher();
std::vector<int> canon;
// Called when the game starts
virtual void BeginPlay() override;
// Called every frame
virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
int canonTime = 120;
canonTime = canonTime - 1;
};
code chunk 2
#pragma once
#include <vector>
#include <iostream>
#include "Components/SceneComponent.h"
#include "CanonLauncher.generated.h"
**UCLASS** ( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BLOCKASSAULT_API UCanonLauncher : public USceneComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UCanonLauncher();
std::vector<int> canon;
// Called when the game starts
virtual void BeginPlay() override;
// Called every frame
virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
int canonTime = 120;
canonTime = canonTime - 1;
};
the first one throws no errors, but obviously my vector code wont work, nor will my variable manipulation for that matter. so I added the bit you see in bold. now the variable manipulations, flow controls, and vectors work but now the bit surrounded by double Asterisks ("**") throws the error
this declaration has no storage class or type specifier
ADDENDUM 1:
Yeah, I know iostream and vector are creating a code collision with one of the UE4 headers, that bit seems obvious, I just don't know how to work around it