Hey guys, I am trying to make a base data asset, then have a child class with items in it. I am declaring a TArray in the base class shown below.
How do I add items to the TArray in its child class?
This is the InventoryItemDef.h (this is truncated kinda psuedocode/snippet)
#include "CoreMinimal.h"
#include "Engine/DataAsset.h"
#include "InventoryItemDef.generated.h"
class UItemFragment;
UCLASS(Blueprintable)
class INVENTORY_API UInventoryItemDef : public UPrimaryDataAsset
{
GENERATED_BODY()
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category="Fragments", Instanced)
TArray<UItemFragment*> Fragments;
}
Then the child class is ID_BaseItem which I created with InventoryItemDef as a parent. How do I access “Fragments” and add items to its TArray?
#include "CoreMinimal.h"
#include "InventoryItemDef.h"
#include "ID_BaseItem.generated.h"
UCLASS()
class UID_BaseItem : public UInventoryItemDef
{
GENERATED_BODY()
};
This is a bit convoluted. I’m not sure what fragments are supposed to do in a data asset, but I’m not sure what they are in the first place, so it’s probably fine.
Why do you have two separate classes for items- both UInventoryItemDef and UID_BaseItem?
From what I can tell, you can just completely delete the UID_BaseItem class.
But to answer your original question:
Since UID_BaseItem inherits from UInventoryItemDef (which contains TArray<> Fragments), UID_BaseItem also has a field called Fragments, you just can’t see the declaration. You can access it just as you would in the UInventoryItemDef class.
InventoryItemDef.h is truncated, there are functions in it also that sort and return data.
UID_BaseItem needs those functions but will have fragments assigned to it.
Fragments are just pieces of data like the skeletal mesh for that item, an icon that is shown for that item, stuff like that… not really important.
Then for say medical items there would be a UID_MedicalItem that is a child of UID_BaseItem which has all the standard stuff plus any fragments that are needed for medical items.
“You can access it just as you would in the UInventoryItemDef class.”
Right, I dont know how to do that, I dont know how to add Fragments to the TArray<> Fragments in the InventoryItemDef class from the UID_BaseItem class.
Inheritance is like adding chocolate syrup onto an ice-cream cone. You still have access to the ice-cream cone, but now you also have chocolate syrup. In terms of code, this would be something like ChocolateSyrupIcecreamCone: public IcecreamCone.
(of course you’d never actually program syrups like that, it’s just an example)
If this IcecreamCone class has, say, an automatic melting function and a temperature variable, ChocolateSyrupIcecreamCone will also have them. Who knows, the chocolate may help shield the icecream from the sun, changing the TimeToMelt variable declared in IcecreamCone. But if nothing is overriden, it will just melt normally.
Inheritance expands on the parent classes (aka classes they inherit from).
UID_BaseItem inherits from InventoryItemDef, which itself inherits from Primary Data Asset.
Your class UID_BaseItem has access to everything- the methods and variables in both InventoryItemDef and PrimaryDataAsset. InventoryItemDef has access to itself and PrimaryDataAsset. PrimaryDataAsset just has access to itself (and of course whatever it inherits from, but idk what that is).
UID_BaseItem ***IS*** InventoryItemDef, just as a Chocolate Syrup Icecream Cone ***IS*** an IcecreamCone.
It already has the Array in the Base class, I was just trying to figure out how to add to that array in the child classes.
I seem to have figured it out.
Originally I was just trying to access Fragments like
Fragments.Add(newfragment)
and it was erroring out on compile.
I did this and it now works as expected.