Hi,
I know how to create constructors in other languages but it’s particularly difficult in C++.
The idea here is that each InventoryItem has an itemType, meaning the item which is in that particular inventory slot.
Here’s my code:
ItemType.cpp
#include "NocturnalSynergy.h"
#include "ItemType.h"
UItemType::UItemType(const FObjectInitializer &ObjectInitializer) : UObject()
{
}
UItemType::UItemType(FText name)
{
UItemType();
this->name = name;
}
ItemType.h
#include "ItemType.generated.h"
UCLASS()
class NOCTURNALSYNERGY_API UItemType : public UObject
{
GENERATED_BODY()
private:
FText name;
UItemType(const FObjectInitializer &ObjectInitializer);
public:
UItemType(FText name);
};
InventoryItem.cpp
#include "NocturnalSynergy.h"
#include "InventoryItem.h"
UInventoryItem::UInventoryItem(const FObjectInitializer &ObjectInitializer) : UObject()
{
}
UInventoryItem::UInventoryItem(UItemType itemType)
{
InventoryItem();
this->itemType = itemType;
}
InventoryItem.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "InventoryItem.generated.h"
UCLASS()
class NOCTURNALSYNERGY_API UInventoryItem : public UObject
{
GENERATED_BODY()
private:
UItemType itemType;
UInventoryItem(const FObjectInitializer &ObjectInitializer);
public:
UInventoryItem(UItemType itemType);
};
It’s a mess, I know.
What I was thinking:
- I need custom constructors. Init functions are insane considering I’m going to have loads of instances and it’s silly doing something like:
MyObj o1 = new MyObj();
o1.init(args);
MyObj o2 = new MyObj();
o2.init(args);
MyObj o3 = new MyObj();
o3.init(args);
So I used a private, plain constructor and called it inside my custom one. I need it private because it should only be accessible within the custom constructor; the only public constructor should be the custom one.
However, I noticed that UObject has an optional parameter meaning my plain constructor effectively had the same signature and can’t be used.
Also, FText is hideous. Why can’t I just use String?
How can I have a custom constructor which compiles without 20+ errors?
The first error of many is in InventoryItem.h, on the line:
UItemType itemType;
and states:
‘itemType’: unknown override specifier
Thanks,