- Home /
Choosing function with highest return value
Is there a way for me to create a function which activates the highest return value?
Example:
public void ChooseFunction()
{
//Choose function2, as it has the highest returning value
}
float Function1 ()
{
return 1;
}
float Function2 ()
{
return 2;
}
Answer by SolomonG · Jul 05, 2017 at 05:15 PM
Just to clarify, do you want ChooseFunction()
to return the highest value returned between Function1()
and Function2()
, or do you only want the function that would return the highest value to be called, and not the other?
I want the function that would return the highest value to be called and not the other
That's more difficult then. I'm guessing that the functions will be modifying and accessing variables outside of method scope (i.e. not stateless), so you can't just run them normally to evaluate. I'd maybe add an argument bool evalRun
and modify the functions so that if evalRun == true
, the functions won't modify anything outside of method scope. However, depending on the function purpose, that might not be possible. What will the functions be doing?
Each function will correspond to a utility in an AI.
For example, the need to flee if low hp.
What I need to do is find a way to import these into something like an array - and then evaluate the highest utility need.
Perhaps a way to do it is to make all functions return an array with the utility weight (amount) and their name.
After that I loop through the first level of the array to find highest returned value - when found I then take the 2nd level of the array and use .Sendmessage --- $$anonymous$$aybe this would work?
Answer by efeguclu · Jul 05, 2017 at 07:56 PM
Can use this :
float highest = ((Function1() > Function2()) ? Function1() : Function2());
Or if you want to call the function you can use something like :
public void ChooseFunction()
{
//Choose function2, as it has the highest returning value
bool oneisbigger = (Function1() > Function2())
if(oneisbigger)
Function1();
else
Function2();
}
float Function1 ()
{
return 1;
}
float Function2 ()
{
return 2;
}
void Function1(){
}
void Function2(){
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612125602im_/https://answers.unity.com/themes/thub/images/avi.jpg)