I'm creating an AI Director and need a way to change when the AI needs to pressure the player and when they need to move away. I've got a TArray of Objects and am checking the distance from the player. I'd like to either get the largest distance or the smallest distance.
I know this doesn't work:
operator comparer = PlayerTensionLevel > BackstageThreshold ? > : <;
Both of the variables used in the one line Bool are floats. I'm hoping that the comparer can be used in a situation like:
if(DistanceSquared(objectA, objectB) comparer _currentThresholdDistance){
_currentObject = objectA;
_currentThresholdDistance = DistanceSquared(objectA, objectB);
}
CodePudding user response:
You can compute with bool
! If you aren’t concerned with differing behavior for ties, you can just write
if((DistanceSquared(objectA, objectB) > _currentThresholdDistance) ==
(PlayerTensionLevel > BackstageThreshold)) …
(Technically, the extra parentheses here are unnecessary, but it’s probably not reasonable to expect the reader to know that relational operators have higher precedence than equality operators.)
CodePudding user response:
As already mentioned in @SamVarshavchik's comment above,
You can use std::function
and assign it to either std::less
or std::greater
based on PlayerTensionLevel
and BackstageThreshold
.
After you determine the comparator you can use it against the current values of _currentThresholdDistance
and the the squared distance between the objects (here I just used dist2
to represent it).
#include <iostream>
#include <functional>
std::function<bool(double a, double b)>
GetComp(double PlayerTensionLevel, double BackstageThreshold)
{
if (PlayerTensionLevel > BackstageThreshold) {
return std::less<double>{};
}
return std::greater<double>{};
}
int main() {
double _currentThresholdDistance = 1;
double dist2 = 2;
double PlayerTensionLevel = 100;
double BackstageThreshold;
BackstageThreshold = 101;
std::cout << GetComp(PlayerTensionLevel, BackstageThreshold)
(dist2, _currentThresholdDistance) << std::endl;
BackstageThreshold = 99;
std::cout << GetComp(PlayerTensionLevel, BackstageThreshold)
(dist2, _currentThresholdDistance) << std::endl;
}
Output:
1
0