首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UE4 C++ Inventory项目在时间后发生变化

UE4 C++ Inventory项目在时间后发生变化
EN

Stack Overflow用户
提问于 2021-04-08 02:58:07
回答 1查看 78关注 0票数 0

我是C++和UE4的新手。我来自Java背景。所以,请在这里温柔点。我目前正在尝试建立一个游戏,允许玩家拾取物品(我知道…完全独特的想法...)。这些项目存储在自定义库存项目对象的TArray中。每个inventory item对象表示项目属性以及当前库存中项目的数量。到目前为止,一切似乎都在按设计进行。当我在游戏级别中点击一个拾取物品时,它会将一个库存物品对象放入库存组件中。我已经查看了断点等,并且已经看到它正在正确地分配库存项目和计数。但是,大约一分钟后,TArray中的inventory item对象突然发生变化,并包含所有垃圾数据(默认值甚至是随机值)。我猜这是由于垃圾回收之类的原因造成的。我不知道的是如何修复它。我已经尝试更改创建清单项目的方式,但似乎没有什么不同。

以下是InventoryComponent.h中相关信息的声明:

代码语言:javascript
复制
private:
    /* This is a pointer to the character that owns the inventory component */
    class APilferCharacter* OwningCharacter;

    /* These are the items that are currently in the inventory */
    class TArray<class UInventoryItem*> Items;

    class UInventoryItem* CreateInventoryItem(TSubclassOf<class UInventoryItem> InventoryItemClass);

public:
    /* 
    Adds a given number of the specified item to the inventory 
    Returns the actual number of items added (May be less than the requested amount if there is not enough space)
    */
    UFUNCTION(BlueprintCallable, Category = "Inventory")
        uint8 AddItem(TSubclassOf<class UInventoryItem> InventoryItemClass, uint8 count = 1);

下面是将库存项目添加到UInventoryComponent中的代码:

代码语言:javascript
复制
UInventoryComponent::UInventoryComponent()
{
    MaxArrows = 15;
    MaxPotions = 3;
    this->Items.SetNum(0);
}

UInventoryItem* UInventoryComponent::CreateInventoryItem(TSubclassOf<UInventoryItem> InventoryItemClass)
{
    UInventoryItem* InventoryItem = NewObject<UInventoryItem>(this, InventoryItemClass);
    InventoryItem->OwningInventory = this; 
    InventoryItem->World = GetWorld();
    return InventoryItem;
}

uint8 UInventoryComponent::AddItem(TSubclassOf<UInventoryItem> InventoryItemClass, uint8 count)
{
    uint8 actualAddedCount = 0;
    if (count > 0) {
        UInventoryItem* itemToAdd = CreateInventoryItem(InventoryItemClass);
        UInventoryItem* existingItem = GetItemBySubType(itemToAdd->GetInventoryItemSubType());
        uint8 MaxItemCount = GetMaxItemsByType(itemToAdd->GetInventoryItemType());
        uint8 CurrentItemCount = 0;
        if (existingItem) {
            CurrentItemCount = existingItem->GetItemCount();
        }
        uint8 availableSpace = MaxItemCount - CurrentItemCount;
        actualAddedCount = std::min(count, availableSpace);
        itemToAdd->SetItemCount(actualAddedCount);

        if (existingItem)
        {
            actualAddedCount = existingItem->Add(actualAddedCount);
        }
        else
        {
            Items.Add(itemToAdd);
        }

        if (actualAddedCount > 0) {
            // Call the delegate to update the UI
            if (OnInventoryUpdated.IsBound())
            {
                OnInventoryUpdated.Broadcast();
            }
        }
    }
    return actualAddedCount;
}

如何才能使库存项目在组件的持续时间内有效?

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2021-04-08 08:24:23

好的!因此,在互联网上搜索解决方案后,我发现了这个网页:https://www.programmersought.com/article/40121301538/

当我通读它的时候,我遇到了一些关于UPROPERTY()处理TArrays GC的内容。然后我意识到我没有将UPROPERTY()添加到TArray中(我认为这没有关系,因为它是私有属性)。所以,我想我应该把它加进去,看看是否有什么不同,瞧!它似乎起作用了!

所以,通过改变

代码语言:javascript
复制
/* These are the items that are currently in the inventory */
    class TArray<class UInventoryItem*> Items;

代码语言:javascript
复制
/* These are the items that are currently in the inventory */
    UPROPERTY()
    class TArray<class UInventoryItem*> Items;

数组似乎正确地保存了这些值,并且不会在一分钟左右的时间内将它们转换为随机值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66992509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档