In my project, I have an abstract base class “ATerrain” which will be the basis for terrain tiles in my game. The world will generate an large grid of these terrain tiles based on this abstract base class, which comes with some intrinsic data. Let’s say for the sake of this question that my intrinsic variables are ClimateHarshness, ResourceAbundance, and ExpMultiplier.
I want to be able to instantiate this class like so:
ATerrain HarshDesert(
9, // ClimateHarshness: Scales the effects of this environment on the player
7, // ResourceAbundance: Scales the amount of resources that can be found here
2, // ExpMultiplier: Player gains twice as much experience here
);
However, my problem is that there doesn’t seem to be an intuitive way to declare a parameterized constructor in a UCLASS. Here is my code in the class header:
UCLASS()
class MYGAME_API ATerrain : public AActor
{
GENERATED_BODY()
public:
ATerrain(int32 ClimateHarshness, int32 ResourceAbundance, int32 ExpMultiplier);
private:
int32 ClimateHarshness_;
int32 ResourceAbundance_;
int32 ExpMultiplier_;
};
And here is the constructor definition in my class .cpp:
ATerrain::ATerrain(int32 ClimateHarshness, int32 ResourceAbundance, int32 ExpMultiplier) :
int32 ClimateHarshness_(ClimateHarshness),
int32 ResourceAbundance_(ResourceAbundance),
int32 ExpMultiplier_(ExpMultiplier)
{
// ...
}
I get the following compiler error:
You have to define ATerrain::ATerrain() or ATerrain::ATerrain(const FObjectInitializer&). This is required by UObject system to work correctly.
Which is strange because earlier I was getting a separate error with the same code:
Error: no instance of constructor “ATerrain::ATerrain” matches the argument list