x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Comparing Structs within an array

Hi,

I was wondering how best to handle an array of structs. Basically, I have a struct that stores information about n item in the inventory, I then store this in an array, but i noticed that the standard array functions dont work as it cant compare two structs. I am assuming I need to create a custom function too compare the properties of the structs and then use tthat when trying too add/remove. i.e. iterate over the array and compare each one before determining if it can be added or not.

I was just wondering if this is the best way?

Also is there a way to make sure each instance of the struct has a uniqueID, i.e. when the struct is established it gets a unique number.

Product Version: UE 4.12
Tags:
more ▼

asked Jun 24 '16 at 01:16 PM in C++ Programming

avatar image

johnparkes
22 12 11 16

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

Ok. It seems you have to add const after your Argumentlist.

 bool operator==(const FInventoryItem& OtherItem) const
 {
     return ...
 }

This worked in my project :)

Good luck

more ▼

answered Jun 25 '16 at 12:04 PM

avatar image

Cloudy McStrife
465 13 16 38

avatar image johnparkes Jun 25 '16 at 02:37 PM

That worked perfect, thank you so much for your help

avatar image Cloudy McStrife Jun 25 '16 at 10:13 PM

No Problem ;) Good luck and have fun

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Hey John,

can you provide the criteria which allows the struct to be added into the Array?

If the criteria is that you don't want to put one struct twice into the Array you can simply use the AddUnique Method of TArray.

If the criteria is more complex I would suggest you to write an overloaded operator for your struct which compares them.

If you don't know how to do this:

 bool operator==(const FStructType& OtherStruct)
 {
        return //...criteria
 }

To give your Struct Unique IDs:

 USTRUCT()
 struct FStructType
 {
     GENERATED_BODY()
     uint32 UniqueID;
 
     FStructType()
     {
         static uint32 ID = 0;
         UniqueID = ID++;
     }
 
     //Maybe you need this???
     bool operator==(const FStructType& Type)
     {
         return UniqueID == Type.UniqueID;
     }
 }


I hope I could help you :) If not, I'm glad to give some more Input ;)

Greetings Cloudy

more ▼

answered Jun 24 '16 at 01:59 PM

avatar image

Cloudy McStrife
465 13 16 38

avatar image johnparkes Jun 24 '16 at 09:35 PM

No I didn't have the override for the ==, but I have added it now. However, it is still not letting me use stuff like Array.AddUnique(); giving the reason binary operator == not found that takes left hand of InventoryItem. Am I doinf something wrong in the way I add the operator to the struct?

 USTRUCT()
 struct FInventoryItem
 {
     GENERATED_USTRUCT_BODY()
 
     // Unique ID for the item
     UPROPERTY(EditDefaultsOnly, Category = Item)
         int32 ItemID;
 
     // Item name
     UPROPERTY(EditDefaultsOnly, Category = Item)
         FText ItemName;
 
     // Item class to spawn
     UPROPERTY(EditDefaultsOnly, Category = Item)
         TSubclassOf<AActor> ItemClass;
 
     // Item image to be displayed within the inventory
     UPROPERTY(EditDefaultsOnly, Category = Item)
         TAssetPtr<UTexture2D>  ItemImage;
 
     // Flag to capture if the item has been selected
     UPROPERTY(EditDefaultsOnly, Category = Item)
         bool bIsSelected;
 
     // The slot the item is stored in
     UPROPERTY(EditDefaultsOnly, Category = Item)
         int32 InSlot;
 
     bool operator==(const FInventoryItem& InventoryItem)
     {
         return ItemID == InventoryItem.ItemID 
             && ItemName.EqualTo(InventoryItem.ItemName)
             && ItemClass == InventoryItem.ItemClass;
     }
 
 };

Thanks for the help

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question