Reference to item in array no matter the item's ID in the array

Hi everyone!

I have a class called UItemsDB that extends UDataAsset. This class has five arrays:



/**This array contains all the items available in the game.*/
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="Arrays")
		TArray<FItem> itemsDBArr;

	/**This array contains all the bullets that are available in the game.*/
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Arrays")
		TArray<FBulletInfo> bulletsDBArr;

	/**This array contains all the wearables that are available in the game.*/
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Arrays")
		TArray<FWearable> wearablesDBArr;

	/**This array contains all the weapon mags that are available in the game.*/
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Arrays")
		TArray<FWeaponMag> weaponMagsDBArr;

	/**This array contains all the weapons that are available in the game.*/
	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Arrays")
		TArray<FWeaponInfo> weaponsDBArr;


The struct FItem has a field called itemIDinOtherDB and it is used to access the item’s specific info. For example: I have an item that is weapon in itemsDBArr, but itemsDBArr only stores basic information from the item (like name, description, icon, etc) so to get the weapon’s info (fire rate, caliber, etc) I access the item (referred to an array item, not game item) of index equal to itemIDinOtherDB in weaponsDBArr. If I have an item that is a bullet I would do the same for the weapon, but instead of accessing an item in weaponsDBArr I would access an item in bulletsDBArr. It is a little confusing, I know (sorry :p) but this is the best I can do to explain.

I use this class (UItemsDB) to create a DataAsset blueprint that will store all my game’s items.

The problem:

I have a variable in an item ® in array A that stores an item’s index in array B.
I have a item © in array B (lets say © is in the index of 10).
The variable in ® stores ©’s index in array B.
If I remove an item from B before ©’s index (lets say we remove the item at index 8) then ©’s index would change (to 9)
If ©’s index changed (to 9) then the variable in ® in array A would be pointing to a completely different item.

An image to explain it a little better:

How would I go around that?

Thanks in advance!

PS: I was trying to make a UE4 plugin to manage this Data Asset but it didn’t work.

1 Like

A) Dont delete it - only flag it as deleted so the memory can be reused for a new(?) item - or at least the memory won’t be shifted so your array size stays the same. Will result in an overflow or error if you add enough new items though.

B) Identify each Weapon and each Bullet with an ID (that’s how databases work usually btw.), search for each item from Array B with ID Z which is refered in the Array A item with Array B ID Z and actualize the new pointer.
(ArrayA*.ArrayB_ID == ArrayB*.ID)

Depending on your database size, B) will take alot performance. So don’t delete it, flag it as reusable, and if you add a new item, just use the unused indexes.

e/ Not sure how UDataAsset works, but you could probably use a pointer to the object of Array B Item X instead of using a pointer to Index X of Array B.

Thanks for your reply!

The thing is: I’m using this class to make a Data Asset BP and updating every single item’s “ArrayB_ID” by hand would take a LOT of time. I tried to make a plugin for this that would do this exact thing you said above, but it didn’t work :p. If there was some way of adding a button (plugin :p) to the Data Asset blueprint or even have an override function (C++) for when you click to add/remove an item in the Data Asset to loop through the items and update their “ArrayB_ID”'s…

But again, thanks for your reply! I think I might give the plugin another shot and see if I can make it work. If you got any other ideas please say them to me haha.

(My english is not that good. I have the feeling that I made a few mistakes regarding the grammar/syntax in this post… If I did, sorry)

EDIT:

I can’t do that because USTRUCTS can’t have exposed pointers.