This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+2 votes

I mean I just can't get it. What's up with this brilliant idea?

in Engine by (1,124 points)
float max( float a, float b )
Return the maximum of two values.

What were you expecting?

4 Answers

+2 votes

It's not weird. Java and C# also use 2 arguments
you can write your own code that may use array as argument

by (490 points)
0 votes

Working with two floats is way faster than working with an array of objects. It's all about performance. But you can write an additional max and min function that accept arrays of untyped values if you want.

by (703 points)
+5 votes

Let's say you want to clamp a value v to max constant 20. You could do it with code

if (v > 20):
  v = 20

But this is shorter and looks better in code:

v = max(v, 20.0)

You can use min max to clamp a value to the range (Godot has a clamp function, but still...)

v = max(0.0, min(20.0, v))

You can nest them, if you want to use more values

v = max(v1, max(v2, max(v3, v4)))

I personally use them a lot in my current Java project.

by (836 points)

Am i wrong or is

if (v > 20):
  v = 20

as max(v,20) the opposite of what you want?
because if v was bigger than 20, the max() function would return the bigger value (v)? so you would get the desired functionality with the min() function rather than the max()

+2 votes

Like the people before me have said, it's a totally standard way of working.

If you only have a couple of variables to compare, you can nest the max() function calls, like Gladosik said.

If you have more variables, consider making them a list; sort them; and retrieve the first value to get the highest number.

by (28 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.