I know it’s been a while already, but a couple of posts above, the guys were trying to figure out the best way to determine the minimum and maximum variable in a given set. So I just wanted to share my solution:
I created a subroutine that accepts as many arguments as you want and it will return the one with the highest value.
- Name:
max() - Signature:
gosub max() arg_1 arg_2 arg_3 etc - Description:
selects the highest value in a set - Arguments:
-
arg_1: Integer: number of variables to be compared; -
arg_2: String: name of the variable (inside quotation marks), e.g.: “honesty”; - any subsequent argument follows the pattern of the second one.
-
- Return:
-
output_1: String: name of the highest variable, the value of the variable can be retrieved with curly brakets, i.e. {output_1} -
output_2: Boolean: returns true if there was a tie during testing
-
*temp honesty 0
*temp courage 0
*temp intelligence 0
*temp influence 0
*rand honesty 0 100
*rand courage 0 100
*rand intelligence 0 100
*rand influence 0 100
*temp output_1 ""
*temp output_2 ""
*gosub max() 4 "honesty" "courage" "intelligence" "influence"
*if output_2 = false
The stat with the highest value is ${output_1} at ${{output_1}} points.
*else
There was a tie!
*finish
*label max()
*params
*temp test_length param[1]
*temp maxval param[2]
*temp test_index 3
*temp tie false
*if (test_length < 2)
*return
*label max_foreach
*if ({maxval} > {param[test_index]})
*goto max_continue
*elseif ({maxval} < {param[test_index]})
*if (tie = true)
*set tie false
*set maxval param[test_index]
*goto max_continue
*else
*set tie true
*goto max_continue
*label max_continue
*set test_index +1
*if (test_index <= test_length)
*goto max_foreach
*comment return result
*set output[1] maxval
*set output[2] tie
*return
You can tweak it so that in case of a tie it returns the value of the other variable as well. But it’s never going to be perfect For example if there is a tie between three variables.
You can tweak it to return the lowest variable instead.
If testing local variables, make sure the subroutine is in the same file.
