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:
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
AndreasV Time 2h
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
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
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.
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
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.
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:
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
}
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: