UE4 C++ 清单项目随时间变化
UE4 C++ Inventory Items mutating after time
我是 C++ 和 UE4 的新手。我来自 Java 背景。所以,请在这里温柔点。
我目前正在尝试构建一个允许玩家拾取物品的游戏(我知道......完全独特的想法......)。这些项目存储在自定义库存项目对象的 TArray 中。每个库存项目对象代表项目属性以及当前库存中的项目数量。
到目前为止,一切似乎都按设计运行。当我在游戏关卡中点击拾取物品时,它会将一个物品栏物品对象放入物品栏组件中。我已经通过断点等进行了观察,并且看到它正在正确分配库存项目和计数。
然而,大约一分钟后,TArray 中的库存项目对象突然发生变异并包含所有垃圾数据(默认值甚至随机值)。
我猜这是由于垃圾收集或其他原因造成的。我不知道的是如何解决它。我已经尝试更改库存项目的创建方式,但似乎没有任何区别。
以下是来自 InventoryComponent.h 的相关信息的声明:
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 的代码:
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;
}
如何才能使库存物品在组件期间持续存在?
提前致谢
好的!因此,在网上搜索解决方案后,我发现了这个网页:
https://www.programmersought.com/article/40121301538/
当我通读它时,我遇到了一些关于 UPROPERTY() 处理 TArray 的 GC 的内容。然后我意识到我没有将 UPROPERTY() 添加到 TArray(我认为这无关紧要,因为它是私有的 属性)。所以,我想我会把它加进去,看看它是否有所作为,瞧!它似乎奏效了!
所以,通过改变
/* These are the items that are currently in the inventory */
class TArray<class UInventoryItem*> Items;
到
/* These are the items that are currently in the inventory */
UPROPERTY()
class TArray<class UInventoryItem*> Items;
数组似乎正确地保存了值,并且在一分钟左右后没有将它们变成随机值。
我是 C++ 和 UE4 的新手。我来自 Java 背景。所以,请在这里温柔点。 我目前正在尝试构建一个允许玩家拾取物品的游戏(我知道......完全独特的想法......)。这些项目存储在自定义库存项目对象的 TArray 中。每个库存项目对象代表项目属性以及当前库存中的项目数量。 到目前为止,一切似乎都按设计运行。当我在游戏关卡中点击拾取物品时,它会将一个物品栏物品对象放入物品栏组件中。我已经通过断点等进行了观察,并且看到它正在正确分配库存项目和计数。 然而,大约一分钟后,TArray 中的库存项目对象突然发生变异并包含所有垃圾数据(默认值甚至随机值)。 我猜这是由于垃圾收集或其他原因造成的。我不知道的是如何解决它。我已经尝试更改库存项目的创建方式,但似乎没有任何区别。
以下是来自 InventoryComponent.h 的相关信息的声明:
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 的代码:
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;
}
如何才能使库存物品在组件期间持续存在?
提前致谢
好的!因此,在网上搜索解决方案后,我发现了这个网页: https://www.programmersought.com/article/40121301538/
当我通读它时,我遇到了一些关于 UPROPERTY() 处理 TArray 的 GC 的内容。然后我意识到我没有将 UPROPERTY() 添加到 TArray(我认为这无关紧要,因为它是私有的 属性)。所以,我想我会把它加进去,看看它是否有所作为,瞧!它似乎奏效了!
所以,通过改变
/* These are the items that are currently in the inventory */
class TArray<class UInventoryItem*> Items;
到
/* These are the items that are currently in the inventory */
UPROPERTY()
class TArray<class UInventoryItem*> Items;
数组似乎正确地保存了值,并且在一分钟左右后没有将它们变成随机值。