Approach function not working GMS2

T

tomsonite

Guest
I tried typing in the approach function for my player object, only for it to be recognized as a variable and turn purple. I put this in my player object's step event under horizontal movement:

//Calculate Horizontal Movement
var dir = key_right - key_left;
hsp += dir * hsp_acc;
if (dir == 0)
{
var hsp_fric_final = hsp_fric_ground;
if (longround) hsp_fric_final = hsp_fric_air;
hsp = Approach(hsp,0,hsp_fric_final);
}
hsp = clamp(hsp,-hsp_walk,hsp_walk);


If anyone knows why it won't turn orange I'd appreciate it!
 

Attachments

samspade

Member
I tried typing in the approach function for my player object, only for it to be recognized as a variable and turn purple. I put this in my player object's step event under horizontal movement:

//Calculate Horizontal Movement
var dir = key_right - key_left;
hsp += dir * hsp_acc;
if (dir == 0)
{
var hsp_fric_final = hsp_fric_ground;
if (longround) hsp_fric_final = hsp_fric_air;
hsp = Approach(hsp,0,hsp_fric_final);
}
hsp = clamp(hsp,-hsp_walk,hsp_walk);


If anyone knows why it won't turn orange I'd appreciate it!
Approach isn't a built in function. You have to code it yourself. It is this.

Code:
/// @description scr_approach(start, end, shift);
/// @function scr_approach
/// @param start
/// @param end
/// @param shift

if (argument[0] < argument[1]) {
   return min(argument[0] + argument[2], argument[1]);
} else {
   return max(argument[0] - argument[2], argument[1]);
}
 

Nidoking

Member
It seems to have worked well enough for the OP. That, or they've been very patient these last three and a half years with the same problem and chose not to reply again.
 
  • Like
Reactions: Rob

FrostyCat

Redemption Seeker
doesnt work
Script syntax has changed since the topic was last active. See this article:


GML:
function scr_approach(start, target, shift) {
    if (start < target) {
        return min(start+shift, target);
    } else {
        return max(start-shift, target);
    }
}
Before you take another solution from search results again, read the timestamp. Anything before 2020 will certainly have old syntax that needs to be adapted.

And before you post on another dead thread again, read the timestamp. Don't hijack thread that aren't yours. Don't bring up inactive threads that are more than a year old.
 
Top