I’m having a problem where I run into unexpected behaviour if I change the order in which the member variables in one of my classes are declared in the header file.
First off, I’m a Java programmer, and I’ve just recently started delving into C++, so I apologize in advance for any mistakes I make regarding the naming conventions in the C++ language. Please bear with me if I’m not using all the correct terms.
I started working through the FloatingActor example found at https://docs.unrealengine.com/latest/INT/Programming/QuickStart/4/index.html. I attached a component to my actor, and everything worked like a charm. My actor was floating, bobbing up and down!
I decided I wanted to expand this code a bit, so I added a couple of uproperties which allows you to determine how far the component would float and in which direction. I also added a couple of member variables that stores the components initial x and z values.
The issue I encounter is the following: If I rearrange the order in which the member variables are declared in the header file, the components attached to the actor won’t move. I get no errors or exceptions as far as I can tell, the code just doesn’t work as intended any more for some reason.
The .cpp code looks like this:
#include "Brawler25D.h"
#include "FloatingSineWaveActor.h"
// Sets default values
AFloatingSineWaveActor::AFloatingSineWaveActor() : StartLocationX(0.0), StartLocationZ(0.0), HorizontalMovement(false), FloatDistance(0.0), RunningTime(0.0)
{
// 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;
}
// Called when the game starts or when spawned
void AFloatingSineWaveActor::BeginPlay()
{
StartLocationX = GetActorLocation().X;
StartLocationZ = GetActorLocation().Z;
Super::BeginPlay();
}
// Called every frame
void AFloatingSineWaveActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
RunningTime += DeltaTime;
FVector NewLocation = GetActorLocation();
if (HorizontalMovement) {
NewLocation.X = StartLocationX + ((FMath::Sin(RunningTime) + 1.0) * 0.5 * FloatDistance);
} else {
NewLocation.Z = StartLocationZ + ((FMath::Sin(RunningTime) + 1.0) * 0.5 * FloatDistance);
}
SetActorLocation(NewLocation);
}
And the header file:
#pragma once
#include "GameFramework/Actor.h"
#include "FloatingSineWaveActor.generated.h"
UCLASS()
class BRAWLER25D_API AFloatingSineWaveActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFloatingSineWaveActor();
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)
bool HorizontalMovement;
UPROPERTY(EditAnywhere)
float FloatDistance;
float RunningTime;
float StartLocationX;
float StartLocationZ;
};
When the member variables are sorted in the order they are above, everything works!
The code silently breaks, however, if I sort them like this:
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
float RunningTime;
UPROPERTY(EditAnywhere)
bool HorizontalMovement;
UPROPERTY(EditAnywhere)
float FloatDistance;
float StartLocationX;
float StartLocationZ;
I’ve tried ordering them several ways to see if I could find a pattern of sorts, but I can’t say I have. Also, I may have misunderstood something, but isn’t part of the reason why header files exists to give you a way of declaring functions and member variables without worrying about the order in which you declare them? I thought maybe it was related to uproperties being declared first or last, but that doesn’t seem to add up either when testing different orders…
I’ve thoroughly searched for information about this issue, but I can’t seem to find anything related. I’m also not sure if I’m using the right terms while searching, which isn’t helping. Any feedback would be greatly appreciated!