# AddUnique || TArray\<FTransform\>?

**URL:** <https://forums.unrealengine.com/t/addunique-tarray-ftransform/632460>\
**Category:** C++\
**Tags:** UE4-20, unreal-engine, UE5-0, UE4, question, CPP\
**Created:** [August 22, 2022, 6:45pm UTC](https://forums.unrealengine.com/t/addunique-tarray-ftransform/632460 "2022-08-22T18:45:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 22, 2022, 6:45pm UTC](https://forums.unrealengine.com/t/addunique-tarray-ftransform/632460/1 "2022-08-22T18:45:20Z")

</div>

How to add unique elements from FTransform Arrray to another FTransform Array?

```auto
TArray<FTransform> Available;
TArray<FTransform> UniqueArr;

```

```auto
FTransform& Location = GetRandomLocation(Available);
UniqueArr.AddUnique(Location);

```

```auto
FTransform& AMyGameMode::GetRandomLocation(TArray<FTransform>& Location)
{
	return Location[FMath::RandRange(0, Location.Num() - 1)];
}

```

> ERROR:: Binary “==” No operator found

---

<div class="post-metadata">

**Author:** ![barracius-AOne](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/barracius-aone/32/548049_2.png) [@barracius-AOne](https://forums.unrealengine.com/u/barracius-AOne)\
**Post date:** [August 22, 2022, 10:50pm UTC](https://forums.unrealengine.com/t/addunique-tarray-ftransform/632460/2 "2022-08-22T22:50:09Z")

</div>

Hi @Alexa.Ki !

There are a few things that are weird in the code you sent, I’m not sure you are using the Ampersand `&` correctly, what it does is use the value of the reference and if it is edited, it edits the reference. And it is usually used for functions that return or change more than one value.

What I would try is to remove all ampersands from the GetRandomLocation, so it ends up something like this:

```auto
FTransform AMyGameMode::GetRandomLocation(const TArray<FTransform> Locations)
{
	return Location[FMath::RandRange(0, Location.Num() - 1)];
}

```

I added a `const` too, which means that the variable Locations won’t be modified on the function, as it is only getting one random value from that array, it is correct that it won’t alter the array of locations.

Then I would change these

> [@Alexa.Ki](#):
>
> ```auto
> FTransform& Location = GetRandomLocation(Available);
> UniqueArr.AddUnique(Location);
> 
> ```

To this:

```auto
FTransform Location = GetRandomLocation(Available);
UniqueArr.AddUnique(Location);

```

For Tarrays, you can also call the `.Add()`, so it ends up as `UniqueArr.Add(Location)`

As for this

> [@Alexa.Ki](#):
>
> ERROR:: Binary “==” No operator found

The error doesn’t match with what you uploaded, so maybe it’s somewhere else.

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 22, 2022, 11:05pm UTC](https://forums.unrealengine.com/t/addunique-tarray-ftransform/632460/3 "2022-08-22T23:05:42Z")

</div>

Thank you Sir for replying and correcting my code according to you.  
But the error: `ERROR:: Binary “==” No operator found` is ralated to the code because while adding the FTransform values are checked for `==` or `!=` to compare before adding a unique value because the operator `==` is not available for Comparing Transforms.

I can show an example: [FTransform Comparison Issue](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877)  
This is related to this Issue and I have a solution on this post for this issue but it freez the engine.

---

<div class="post-metadata">

**Author:** ![barracius-AOne](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/barracius-aone/32/548049_2.png) [@barracius-AOne](https://forums.unrealengine.com/u/barracius-AOne)\
**Post date:** [August 23, 2022, 12:15am UTC](https://forums.unrealengine.com/t/addunique-tarray-ftransform/632460/4 "2022-08-23T00:15:58Z")

</div>

Now I understand what the problem was, the ampersand part is true btw 😅

All right, then to solve that problem you have a few options:

1. Edit the file FTransform on the engine and add the == and != operators directly to that struct. (This could be troublesome if you can’t edit the engine).

2. Create another array of FTransforms named something like `NotUniqueLocations` and add every location to that array, then create a function that checks if that array has unique transforms, declaring that they are equal if their scale, location, and rotations are the same. With that function you could clean up that array of all the data and then retrieve the unique locations into the UniqueArr array (You would need to do this each time you know they could be different, like at the end of when that code is run)

3. Another idea would be to check if the array already has that transform with the Contains method:

```auto
if (!UniqueArr.Contains(Location))
{
    UniqueArr.Add(Location)
}

```

Although I’m not sure if you can use Contains on a thing that is not an UObject

1. Make your custom ContainsTransform method something like this:

```auto
bool ContainsTransform(TArray<FTransform> TransformArray, FTransform Transform)
{
    for (FTransform Trans : TransformArray)
    {
         if (Trans.GetScale3D() == Transform.GetScale3D() && 
             Trans.GetLocation() == Transform.GetLocation() && 
             Trans.GetRotation() == Transform.GetRotation())
         {
              return true;
         }
    }
    return false;
} 

```

And then you call it like the above method:

```auto
if (!ContainsTransform(UniqueArr, Location))
{
    UniqueArr.Add(Location)
}

```

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 23, 2022, 12:34am UTC](https://forums.unrealengine.com/t/addunique-tarray-ftransform/632460/5 "2022-08-23T00:34:03Z")

</div>

> [@barracius-AOne](#):
>
> Edit the file FTransform on the engine and add the == and != operators directly to that struct. (This could be troublesome if you can’t edit the engine).

I would like to do this but it will not need to re-build the engine?

---

<div class="post-metadata">

**Author:** ![Emaer](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/emaer/32/570877_2.png) [@Emaer](https://forums.unrealengine.com/u/Emaer)\
**Post date:** [August 23, 2022, 8:19am UTC](https://forums.unrealengine.com/t/addunique-tarray-ftransform/632460/6 "2022-08-23T08:19:52Z")

</div>

You can use `TSet`.

```cpp
struct KeyFuncsForTransform : public DefaultKeyFuncs<FTransform, false> 
{
  static bool Matches(KeyInitType Left, KeyInitType Right)
  {
    return Left.Equals(Right, 0.001);
  }
  static uint32 GetKeyHash(KeyInitType Key)
  {
    return GetTypeHash(Key.GetTranslation());
  }
};

```

Usage:

```cpp
using UniqueTransformSet = TSet<FTransform, KeyFuncsForTransform>;

void Example()
{
  auto UniquesOnly = UniqueTransformSet{};
  for (auto i = 0; i < 10; ++i) {
    UniquesOnly.Add(FTransform::Identity);
  }
  UE_LOG(LogTemp, Warning, TEXT("%i"), UniquesOnly.Num()); // print `1`
```

---

<div class="post-metadata">

**Author:** ![Alexa.Ki](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexa.ki/32/562753_2.png) [@Alexa.Ki](https://forums.unrealengine.com/u/Alexa.Ki)\
**Post date:** [August 23, 2022, 12:18pm UTC](https://forums.unrealengine.com/t/addunique-tarray-ftransform/632460/7 "2022-08-23T12:18:47Z")

</div>

Can i use this instead of do/while loop which is freezing the engine? [Issue Link](https://forums.unrealengine.com/t/editor-crash-if-getnumber-name-no-number-internal/630488/68) i posted `mygamemode` please check what i a doing wrong in the code

---

<div class="post-metadata">

**Author:** ![barracius-AOne](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/barracius-aone/32/548049_2.png) [@barracius-AOne](https://forums.unrealengine.com/u/barracius-AOne)\
**Post date:** [August 23, 2022, 3:41pm UTC](https://forums.unrealengine.com/t/addunique-tarray-ftransform/632460/8 "2022-08-23T15:41:49Z")

</div>

That’s right, you would need to compile it again (that takes roughly 45 min - 2 hours depending on your PC)
