• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Max Function

H

Heat4Life

Guest
I am currently learning GML and I wonder what is the "max" code here... Please Explain... Thanks :D
 
A

Aura

Guest
Glad I could help :3

Since the query has been answered, you'd perhaps want to set the status of the topic as solved. To do that, go to Edit Title/Thread Tools >> Edit Title >> Set the topic prefix to Solved.
 
H

Heat4Life

Guest
Glad I could help :3

Since the query has been answered, you'd perhaps want to set the status of the topic as solved. To do that, go to Edit Title/Thread Tools >> Edit Title >> Set the topic prefix to Solved.
Okay awesome Thanks Aura!
 
H

Heat4Life

Guest
If some of you watched Shaun Spalding's 2nd Tutorial Gamemaker for Begginers on YouTube, then I need help please... I got a question about the "max" code... I meant the "max(speed - 0.01, 0)"... Can somebody explain this?

Note: I don't want Manual please lol
 
L

Lars Karlsson

Guest
I haven't watched the tutorial. But if I understand the function correctly, it looks like the Max() function is there to make sure the value doesn't go negative. So for example:
If 'speed - 0.01' were to equal a negative value, lets say -0.01. Then the Max() function would return a 0 instead.
 
H

Heat4Life

Guest
I'm kind of Nooby at this and I really don't get it to be honest...
 
A

Aura

Guest
I haven't watched any Shaun Spalding video; but I guess I know what you are talking about.

Code:
speed = max(speed - 0.01, 0);
Basically the code is used to slow down the speed of an instance without making it go any lower than 0. If you read the Manual entry on max() that I linked you to in another thread posted by you, you'd know that it returns the largest value amongst the fed arguments. So, for instance, if your speed is 2.56 then the code would make it go down to 2.55 the next step. But if, for instance, the speed is already 0, the code would force the speed to remain at zero the next step; because max() would return 0 since it is larger than -0.01. Hope that helps! ^^
 
H

Heat4Life

Guest
Lol okay I'm tiny bitsy getting it.... man... I feel like coding with GML is more Brainstorming than coding with Java lol
 
L

Lars Karlsson

Guest
The Max function exists in Java too and works the same way ;)
 

TheouAegis

Member
max(a,b,c,...,o) returns the largest value among the set passed to the function (up to 16 entries). The values can be real number or an arithmetic expression (which is almost anything in Game Maker).

The inverse of max() is min(a,b,c,...,o) which returns the smallest value.

The speed variable will cause the instance to move in the direction specified by the variable direction every step if it is positive. So inversely, if the value of speed is negative, the instance will move away from the direction specified - it will move backwards.

Shaun's formula is for implementing friction/deceleration to the speed. When you let go of the movement input, normally the player would just stop abruptly (e.g., speed=0); to avoid that, he implemented custom friction, which is what the speed-0.01 is about. As was said, if speed is 0, then that formula was result in setting speed to -0.01. Over time it would get lower and lower and eventually you would notice the instance moving backward. To prevent that, he tells GM to only use the largest value between 0 and speed-0.01 for the speed.
 

zendraw

Member
any value that has a minus before it -5323 is lower then 0. 0 is the line that separates left and right up and down positive and negative etc. its like the sea level.

minus "-" simply says its below zero,
 
H

Heat4Life

Guest
I am really having a hard time about what does the max() do and all that stuff... I'm stuck at this for like 2 weeks now -_-
 

Roa

Member
it returns the largest value of the tested argument in it. IE:

a=2 b=5 c=-1

g=max(a,b,c)

g equals 5
 
A

Aura

Guest
https://forum.yoyogames.com/index.p...gs-2nd-tutorial-gamemaker-for-begginers.3841/

https://forum.yoyogames.com/index.php?threads/max.3523/

You already have two threads for this, so I'd suggest sticking to a single thread instead of creating one again.

Either way, I don't see what's so complicated in grasping that logic, unless you're way too young. I'll try to explain again.

Shaun used that code to avoid negative speed. The function would force the speed to remain at 0 while decreasing the speed by 0.01. The code basically does something similar to this:

Code:
speed -= 0.01;
if (speed < 0) {
   speed = 0;
}
Better now? ^^"
 
H

Heat4Life

Guest
that will just set the speed ot zero.
Well, I created a Spaceship game and I want to reduce the speed when aren't holding the key and this code works... But you said It returns the Highest Value and It will just set it to 0 because 0 is more higher... how did It reduce the speed then with this code speed = max(speed - 0.01, 0); ?!
 
H

Heat4Life

Guest
https://forum.yoyogames.com/index.p...gs-2nd-tutorial-gamemaker-for-begginers.3841/

https://forum.yoyogames.com/index.php?threads/max.3523/

You already have two threads for this, so I'd suggest sticking to a single thread instead of creating one again.

Either way, I don't see what's so complicated in grasping that logic, unless you're way too young. I'll try to explain again.

Shaun used that code to avoid negative speed. The function would force the speed to remain at 0 while decreasing the speed by 0.01. The code basically does something similar to this:

Code:
speed -= 0.01;
if (speed < 0) {
   speed = 0;
}
Better now? ^^"

I thought what the max() function do is to return the highest Value... but -0.01 is lower than 0.. I don't get It, I don't know why....
 

Roa

Member
because speed is larger than 0, and you are testing for speed=speed-0.01

so speed-0.01 is larger than 0 and so there for speed=speed-0.01
By the way, this is a really crappy way to do this.
 

TheouAegis

Member
You are supposedly 17 years old but you don't even know how negative numbers, 0, and positive numbers all relate?

Update:
I thought what the max() function do is to return the highest Value... but -0.01 is lower than 0.. I don't get It, I don't know why...
Correct, -0.01 is lower than 0, which is why the max function WILL NOT return -0.01. No one ever said it will return -0.01; we said it will return 0 when the result of speed-0.01 is negative.

@Roa Well, it's from a Shaun Spalding tutorial and his tutorials aren't the best; but for what they are they suffice. Anyway, what's your friction alternative? Maybe it might help him out.
 
Last edited:
H

Heat4Life

Guest
You are supposedly 17 years old but you don't even know how negative numbers, 0, and positive numbers all relate?
I know how the Positive and Negative relate lmfao :l It's just I don't really get the explanation really... I feel like so stupid for the only guy here who can't understand a simple max() function -_-
 

TheouAegis

Member
I updated my post.

@Roa He probably could just use the built-in function, but if he ever wants to implement his own friction mechanic, understanding how Shaun's works would help him. Shaun's friction and GM's friction are fundamentally nearly identical, just one is handled under the hood while the other needs to be coded by the user.
 

Roa

Member
you give it arguments, and it returns the value of the highest number, thats it. That's literally all it does. IN your case, you have the varaible speed being passed in. Unless speed-.01 is larger than zero, it will return a value of speed-.01 which is what your are setting the speed to when its above zero.

You could just as easily set friction=.01 in the create event and completely remove the speed=max() line
 
H

Heat4Life

Guest
OHHH, I think I am getting It... I believe I am getting It... ohh... okay okay... Wow Lol that took a lot of days to get my head around it :l Thanks for the one who posted from this stupid, dumb thread :D
 

Roa

Member
I updated my post.

@Roa He probably could just use the built-in function, but if he ever wants to implement his own friction mechanic, understanding how Shaun's works would help him. Shaun's friction and GM's friction are fundamentally nearly identical, just one is handled under the hood while the other needs to be coded by the user.
yeah... making tutorials using unorthodox methods just leads to this. imagine how much less confused he would be if this was just friction=-.01 lol. brevity is the soul of witt, dont do more than you have to.
 

TheouAegis

Member
I wouldn't really call it unorthodox. Sure, since he's using speed and direction in a top-down game, friction would be fine to use, but if he ever wants to make a game without s&d, he'd need something else to handle "friction".

Code:
var slow = hspd - sign(hspd)/100;
hspd = slow * (sign(slow) == sign(hspd))
Or if hspd can only be in one direction....

Code:
hspd = max(0,hspd-0.01);
So as a tutorial should do, Shaun is teaching a method of implementing friction that can work outside the scope of GM's built-in functionality and shows how the built-in functionality essentially works.
 
H

Heat4Life

Guest
So you can like do "speed - 0.01" inside of the max() function to reduce the speed?
 

TheouAegis

Member
speed = max(speed - 0.01, 0)

You must use it in that format. Just putting...

max(speed - 0.01, 0)

...won't do anything at all.

And @Roa it indefinitely counts down using friction also, you just don't see it counting down indefinitely. Even if it checks if speed!=0 first, it's still fundamentally counting down indefinitely - even if it's not reducing speed each step, it's still checking speed with the intent of reducing speed.
 

TheouAegis

Member
Well the way he worded it, his post wouldn't do anything at all, as I said at the top of my last reply.

speed -= 0.01; //reduce speed indefinitely

max(speed-0.01); //does literally nothing

max(speed-0.01,0); //does literally nothing

speed = max(speed-0.01,0); //reduces speed but never below 0
 
Top