# Randomize Array of FTransform and compare if the elements are duplicated C++?

**URL:** <https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877>\
**Category:** C++\
**Tags:** UE4-20, unreal-engine, UE4, question, CPP\
**Created:** [August 15, 2022, 3:19pm UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877 "2022-08-15T15:19:51Z")\
**Posts on this page:** 20\
**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 15, 2022, 3:19pm UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/1 "2022-08-15T15:19:51Z")

</div>

I am having trouble translating this bp functions to C++, I need some help to correct my mistakes.

**`1`**

 ![image_2022-08-15_194709478](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/7/f/c7f194d2d7ce33c2cb14613d45ff6579fd171d22.png)

**`MyGM.h`**

```auto
UFUNCTION(BlueprintCallable, meta = (Category = ItemGeneration, OverrideNativeName = RandomLocation))
void RandomLocation(TArray<FTransform>& Available, FTransform& Location);|

```

**`MyGM.cpp`**

```auto
void AMyGM::RandomLocation( TArray<FTransform>& Available, FTransform& Location)
{
	Location = Available[FMath::RandRange(0, Available.Num() - 1)];
}

```

**`2`** this is called in beginplay

 ![f1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/a/b/0abb03cc0e5980f30aa67779dfbeeaf0411f9ecc.png)

**in beginplay**

```auto
ItemGroupRef->GetAllTransforms(TransformsOut);
RandomLocation(TransformsOut, RandomLocationOut); 
if (UsedLocations.Find(RandomLocationOut) == -1)
	{
		SpawnLocation = RandomLocationOut;
		UsedLocations.Add(SpawnLocation);
	}
else{
		RandomLocation(TransformsOut, RandomLocationOut);
	}
```

---

<div class="post-metadata">

**Author:** ![AndreasV](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/andreasv/32/487213_2.png) [@AndreasV](https://forums.unrealengine.com/u/AndreasV)\
**Post date:** [August 16, 2022, 7:53am UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/2 "2022-08-16T07:53:32Z")

</div>

Can you tell us what’s going wrong? What behaviour do you intend to achieve?

Your function looks OK.  
However I have some suggestions to make it slightly better:  
Make your input parameter const, as well as your function (you’re not affecting it’s values or members). Then you could also make it BlueprintPure, so that it doesn’t require an execution pin.  
Like this:

```auto
UFUNCTION(BlueprintCallable, BlueprintPure, meta = (Category = ItemGeneration, OverrideNativeName = RandomLocation))
void RandomLocation(const TArray<FTransform>& Available, FTransform& Location) const;

```

---

<div class="post-metadata">

**Author:** ![zeaf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zeaf/32/1108035_2.png) [@zeaf](https://forums.unrealengine.com/u/zeaf)\
**Post date:** [August 16, 2022, 8:30am UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/3 "2022-08-16T08:30:17Z")

</div>

Well the way it works in your BP, the function becomes a loop, which is not the case for your C++ code. In C++ you could do what you want to do using a do while loop. You can also change your RandomLocation function to return the vector instead of returning void, and you can also use UsedLocations.Contains instead of Find, just for easier to follow code

---

<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 16, 2022, 8:46am UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/4 "2022-08-16T08:46:32Z")

</div>

> [AndreasV](https://forums.unrealengine.com/u/AndreasV) Time [2h](https://forums.unrealengine.com/t/help-correct-me-converting-blueprint-into-c/628877/2)  
> Can you tell us what’s going wrong? What behaviour do you intend to achieve?

Error:  
`binary ‘==’: no operator found which takes a left-hand operand of type ‘const FTransform’ (or there is no acceptable Conversion]`

Goal:  
1: doing `-1` and randomize range the locations array to avoid any duplicate locations return  
2: rechecking if there is not duplicated in the if statement but it fails with the error type `C2678`

---

<div class="post-metadata">

**Author:** ![zeaf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zeaf/32/1108035_2.png) [@zeaf](https://forums.unrealengine.com/u/zeaf)\
**Post date:** [August 16, 2022, 9:44am UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/5 "2022-08-16T09:44:29Z")

</div>

Making the function pure and using it the way it’s used right now in the BP would not provide the same result. Pure functions are executed once for every node connected to them, and since this function returns a random element (which by the way can be done using the random element node in BP), then the first result plugged in to Find would be different than the one plugged in to Set Spawn Location, therefore producing wrong results

---

<div class="post-metadata">

**Author:** ![zeaf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zeaf/32/1108035_2.png) [@zeaf](https://forums.unrealengine.com/u/zeaf)\
**Post date:** [August 16, 2022, 9:51am UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/6 "2022-08-16T09:51:17Z")

</div>

Use `UsedLocations.Contains(RandomLocationOut)` instead

---

<div class="post-metadata">

**Author:** ![AndreasV](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/andreasv/32/487213_2.png) [@AndreasV](https://forums.unrealengine.com/u/AndreasV)\
**Post date:** [August 16, 2022, 9:52am UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/7 "2022-08-16T09:52:07Z")

</div>

I think because of the loop the Location would actually change each time no?

Anyway, it all depends what OP intends to achieve.  
I suppose it’s just adding an entry from Available Transforms to UsedLocations that was not already in there. In that case the RandomLocation node should re-evaluate each time.

OP what are you trying to achieve?

---

<div class="post-metadata">

**Author:** ![zeaf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zeaf/32/1108035_2.png) [@zeaf](https://forums.unrealengine.com/u/zeaf)\
**Post date:** [August 16, 2022, 9:54am UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/8 "2022-08-16T09:54:16Z")

</div>

It will be reevaluated correctly if it’s not pure.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/e/6/3e6ee4a864e574d572003bb7719c7a2c62948343.png)

If it is pure, then the value that you get on 1 will be different than 2. And since we are still technically in the same loop iteration, the value needs to be the same

---

<div class="post-metadata">

**Author:** ![AndreasV](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/andreasv/32/487213_2.png) [@AndreasV](https://forums.unrealengine.com/u/AndreasV)\
**Post date:** [August 16, 2022, 9:55am UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/9 "2022-08-16T09:55:35Z")

</div>

My mistake, you’re correct, didn’t pay attention to the second one.

---

<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 16, 2022, 10:02am UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/10 "2022-08-16T10:02:51Z")

</div>

1: GetAllTransforms is returning the transforms of a group of 5 static meshes connected to scene.  
2: RandomLocations randomize the range of transforms as available locations  
3: get these available randomized locations from RandomLocations and check if the randomization is correct , if not repeat.  
4: if correct randomization its mean the location is not duplicated and it can be used as a spaen location, so add the used locations to spawn locations.

---

<div class="post-metadata">

**Author:** ![zeaf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zeaf/32/1108035_2.png) [@zeaf](https://forums.unrealengine.com/u/zeaf)\
**Post date:** [August 16, 2022, 11:04am UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/11 "2022-08-16T11:04:18Z")

</div>

If you want to do that in C++ it gets a bit more complex, but in BP it’s quite easy:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/4/f/f4f1451d847141ac64755d4945b40013dee5345c.png)

---

<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 16, 2022, 11:07am UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/12 "2022-08-16T11:07:24Z")

</div>

You exactly get my point what I want to achieve, look at to this post here is the full thread on this issue , [Link](https://forums.unrealengine.com/t/binary-no-operator-found-c/628457/75)

---

<div class="post-metadata">

**Author:** ![zeaf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zeaf/32/1108035_2.png) [@zeaf](https://forums.unrealengine.com/u/zeaf)\
**Post date:** [August 16, 2022, 12:01pm UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/13 "2022-08-16T12:01:00Z")

</div>

The issue is, like others have mentioned that transform in UE doesn’t have a binary operator, so the array internal functions Find or Contain cannot compare each element to the one you want to search for.

If you really want to do it that way, then you can use something like this:

```auto
if (UsedLocations.ContainsByPredicate([RandomLocationOut] (const FTransform Transform)
	{
		return Transform.GetLocation() == RandomLocationOut.GetLocation()
			&& Transform.GetRotation() == RandomLocationOut.GetRotation()
			&& Transform.GetScale3D() == RandomLocationOut.GetScale3D();
	}))
{
     // Your then code here
}
else
{
    // Your else code here
}

```

---

<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 16, 2022, 12:17pm UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/14 "2022-08-16T12:17:34Z")

</div>

so my function will look like this?

```auto
for (auto& SpawnArrayElements : ItemsIDArr)
{
  ItemGroupRef->GetAllTransforms(Available);
  RandomLocation(Available, Location);

	if (UsedLocations.ContainsByPredicate([Location](const FTransform Transform)
	{
		return Transform.GetLocation() == Location.GetLocation()
			&& Transform.GetRotation() == Location.GetRotation()
			&& Transform.GetScale3D() == Location.GetScale3D();
	}))
	{
		SpawnLocation = Location;
		UsedLocations.Add(SpawnLocation);
	}
	else
	{
		RandomLocation(Available, Location);
	}
}
//Debugging printing the output values
FString SpawnLoc = SpawnLocation.ToString();//converting to string
printf100s("Spawn Location: %s", *SpawnLoc);//printing the string on screen
ItemsIDArr.Empty();//empty array each loop ends
print100s("---------------------------------------");//add marker to sort things readable
```

---

<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 16, 2022, 12:21pm UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/15 "2022-08-16T12:21:28Z")

</div>

updated the variable names, it was my mistake 😃

---

<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 16, 2022, 12:25pm UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/16 "2022-08-16T12:25:46Z")

</div>

now I have crashed the editor at this point

 ![image_2022-08-16_172511306](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/d/b/0db5e2264a0158a301253489fb1ee83d30a8e15b.png)

---

<div class="post-metadata">

**Author:** ![zeaf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zeaf/32/1108035_2.png) [@zeaf](https://forums.unrealengine.com/u/zeaf)\
**Post date:** [August 16, 2022, 12:29pm UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/17 "2022-08-16T12:29:55Z")

</div>

Well like I said earlier, after you call RandomLocation, you need to have a do while, otherwise it will just stop if the random transform returned is not one that hasn’t been used before. So you need to do something like:

```auto
do
{
	RandomLocation(Available, Location);
}
while (UsedLocations.ContainsByPredicate([Location](const FTransform Transform)
	{
		return Transform.GetLocation() == Location.GetLocation()
			&& Transform.GetRotation() == Location.GetRotation()
			&& Transform.GetScale3D() == Location.GetScale3D();
	}));

SpawnLocation = Location;
UsedLocations.Add(SpawnLocation);

```

But I don’t recommend this, this can potentially cause freezes

This will keep getting a random element until it finds one that is not in the UsedLocations array

---

<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 16, 2022, 12:39pm UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/18 "2022-08-16T12:39:46Z")

</div>

I updated the code and still getting the crashed breakpoint in `Actor.cpp`

but if I check for a nullptr against `ItemGroupRef` which is calling the GetTransformss function form AGroupActor class , the cast failed.

I am casting like this `AItemGroup* ItemGroupRef{};`

You think this is not the reason for the crash?

---

<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 16, 2022, 12:42pm UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/19 "2022-08-16T12:42:07Z")

</div>

Updated code:

```auto
if (ItemGroupRef)
{
for (auto& SpawnArrayElements : ItemsIDArr)
{
	SpawnType = SpawnArrayElements.Type;
	SpawnID = SpawnArrayElements.ID; 
	ItemGroupRef->GetAllTransforms(Available);
	do
	{
		RandomLocation(Available, Location);
	} while (UsedLocations.ContainsByPredicate([Location](const FTransform Transform)
	{
		return Transform.GetLocation() == Location.GetLocation()
			&& Transform.GetRotation() == Location.GetRotation()
			&& Transform.GetScale3D() == Location.GetScale3D();
	}));
	SpawnLocation = Location;
	UsedLocations.Add(SpawnLocation);
}
FString SpawnLoc = SpawnLocation.ToString();
printf100s("Spawn Location: %s", *SpawnLoc);
ItemGroupRef->GetAllTransforms(Available);
ItemsIDArr.Empty();
print100s("---------------------------------------");
}
else
{
print100s("----------------ITEMGROUPREF CAST FAILED ---------------");
return;
}

```

checking for nullptr avoid editor to crash

---

<div class="post-metadata">

**Author:** ![zeaf](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/zeaf/32/1108035_2.png) [@zeaf](https://forums.unrealengine.com/u/zeaf)\
**Post date:** [August 16, 2022, 1:50pm UTC](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877/20 "2022-08-16T13:50:04Z")

</div>

Not sure what the issue is now, but you are printing the spawn location outside of the for, so that will only print the last spawn location, once

[Next page](https://forums.unrealengine.com/t/randomize-array-of-ftransform-and-compare-if-the-elements-are-duplicated-c/628877.md?page=2)
