Hello everyone,
I have written an UInventoryComponent class which inherits from UActorComponent. I register it to my character in its constructor this way:
Inventory = CreateDefaultSubobject<UInventoryComponent>(TEXT("InventoryComponent"));
InventoryComponent.h:
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Item.h"
#include "InventoryItem.h"
#include "InventoryComponent.generated.h"
class AItemActor;
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class RPS_API UInventoryComponent : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UInventoryComponent();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Inventory")
TArray<UInventoryItem*> items;
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
UFUNCTION(BlueprintCallable)
void AddItemActor(AItemActor * itemActor);
UFUNCTION(BlueprintCallable)
void AddItem(TSubclassOf<UItem> item,int number=1);
UFUNCTION(BlueprintCallable)
void DropItem(UInventoryItem *iItem);
UFUNCTION(BlueprintCallable)
void RemoveItem(UItem *item);
UFUNCTION(BlueprintCallable)
TArray<UInventoryItem*> GetItems(TSubclassOf<UItem> item);
UFUNCTION(BlueprintCallable)
int GetItemCount(TSubclassOf<UItem> item);
UFUNCTION(BlueprintCallable)
bool HasItem(TSubclassOf<UItem> item);
};
however, when I restart the engine or change my characters c++ class, this Inventory component becomes None in blueprint. It throws error when I try to use it.
The solution I found was to remove the CreateDefaultSubObject line, then compile, then re-add the line again, and compile, and everything works fine. This, as you notice, is a boring process and I believe this is not the expected behavior of the engine. If I add the component from blueprint (not from c++ constructor) I never have any problem.
What may be the solution?
Thanks