Tset help needed

Howdy, summer has ended, but I face new problem with using TSet.

I checked TSet’s Unreal Document. But I cannot find right function to get what I want.

I was trying to access just specific data in TSet like… array[2].

However, I cannot find how to access specific data than iterate whole data to find.

hmmm…

Can anyone give me good example of TSet and accessing it rather than whole iteration?

Second thing, I really need help about transferring Data effectively from TMap to TSet…?

I cannot think better solution for when I use different containers to transfer datas.

A Set and Map are two slightly different containers. While a Map assigns a Key to a Value (e.g. (“Eggs”, 12), (“Bananas”, 5)), a Set only contains Key values (e.g. “Eggs”, “Bananas”).

The point of a Set is when you have a lot of unique values and you want to quickly search through those values (more often than not to see if you have a value there already):




// Check to see if the player already has Bananas in their inventory
// Assume the PlayerInventory is a TSet<FString>
if (PlayerInventory.Find(TEXT("Bananas")) == NULL)
{
   // Player doesn't have Bananas yet! Add them.
   PlayerInventory.Add(TEXT("Bananas"));
}



While you can use the ] operator to access a value in a set, it’s a bit tricky as you have to store the FSetElementId value returned by the Add method:




// Check to see if the player already has Bananas in their inventory
// Assume the PlayerInventory is a TSet<FString>
if (PlayerInventory.Find(TEXT("Bananas")) == NULL)
{
   // Player doesn't have Bananas yet! Add them.
   FSetElementId& BananasId = PlayerInventory.Add(TEXT("Bananas"));
   PlayerInventory[BananasId]; // this would return "Bananas"
}



Make sense? Given the behavior of Sets, going from a Map to a Set you have to decide what values you want to keep since a Set can’t contain both the Key and Value data of a Map - only one or the other.




TMap<FString, int32> FoundItems;

FoundItems.Add(TEXT("Eggs"), 12);
FoundItems.Add(TEXT("Bananas"), 5);

TSet<FString> PlayerInventory;

for (auto& Iterator = FoundItems.CreateIterator(); Iterator; ++Iterator)
{
   PlayerInventory.Add(Iterator.Key());
}

// PlayerInventory now contains: "Eggs", "Bananas"



Hope that helps.

Thank you so much. i will try to use this in my code. FSetElementID& sounds a bit hard, but that will do.

Wait… I must use Add function to find out id number? oh my god… it might makes my code more complicated.

You can also use “FindId” I believe, but really you probably shouldn’t be accessing a Set with the ] operator like a Map or Vector/Array.

if you just want an array of all your keys or values, you can use TMap’s GenerateKeyArray / GenerateValueArray.

hmmm… I really gonna have bad time by that person who recommend me set for Command List in my character. But TSet is pretty important to my concept. anyway thank you.

My TSet does not contain simple data, it contains classes, which makes tricky to implement.

Tried your code… but which one is better for sorting my Action List?

top is my original one by whole iteration, but bottom is the one that I tried with your Add function.



TSet<class ADefault_Item*> temp;
	TArray<int> tempIndexArray;
	int MaxNumber = 0;
	for (const auto& Entry : CurrentAvailableActionList)
	{
		if (MaxNumber < Entry->GetIDNumber())
			MaxNumber = Entry->GetIDNumber();
		tempIndexArray.Add(Entry->GetIDNumber());
	}
	int tempIndex = 0;
	for (tempIndex; tempIndex < MaxNumber +1; ++tempIndex)
	{
		if(tempIndexArray.Find(tempIndex) == INDEX_NONE )
			continue;
		else
		{
			for (const auto& Entry : CurrentAvailableActionList)
			{
				if (Entry->GetIDNumber() == tempIndex)
					temp.Add(Entry);
			}
		}
	}
	CurrentAvailableActionList = temp;




TSet<class ADefault_Item*> tempActions;
	TMap<int, FSetElementId> tempIDMap;
	int MaxNumber = 0;
	for (const auto& Entry : CurrentAvailableActionList)
	{
		if (MaxNumber < Entry->GetIDNumber())
			MaxNumber = Entry->GetIDNumber();
		FSetElementId currentID = tempActions.Add(Entry);
		tempIDMap.Add(Entry->GetIDNumber(), currentID);
	}
	TSet<class ADefault_Item*> tempActions2;
	int tempIndex = 0;
	for (tempIndex; tempIndex < MaxNumber + 1; ++tempIndex)
	{
		if (tempIDMap.Find(tempIndex) == NULL)
			continue;
		FSetElementId currentID = tempIDMap[tempIndex];
		tempActions2.Add(tempActions[currentID]);
	}
	CurrentAvailableActionList = tempActions2;


What are you trying to do, exactly? It looks like you’re trying to sort your set by “GetIDNumber”, but I’m not sure what MaxNumber and all that is for.

Set has a sort, so you could do something like:



CurrentAvailableActionList.Sort(] (const ADefault_Item& A, const ADefault_Item& B)
{
   return A.GetIDNumber() < B.GetIDNumber();
}


Laugh out loud. wow you saved my agony. Thank you sir!

after fiddling with your code. it works! but i have to get rid of const and add new bracket to make it work.