SOLVED Code Error DoSub :1: undefined value

OpulentCD

Member
Hello,

I'm trying to code movement for the player object inside a game with the following code:

GML:
var dir = key_right - key_left;
hsp += dir * hsp_acc;
if (dir == 0)
{
    var hsp_fric_final = hsp_fric_ground;
    if (!onground) hsp_fric_final = hsp_fric_air;
    hsp = Approach(hsp,0,hsp_fric_final);
}

hsp = clamp(hsp,-hsp_walk, hsp_walk);
'Approach; is a script I saw from a youtuber and it goes as follows:

Code:
///Approach        (a,b,amount)
/// @description Approach (a,b,amount)
/// @param a
/// @param b
/// @param amount

/// Moves "a" towards "b" by "amount" and returns the result
/// Does not overshoot "b"

if (argument0 < argument1)
{
    argument0 += argument2;
    if (argument0 > argument1)
        return argument1;
}
else
{
    argument0 -= argument2;
        if (argument0 < argument1)
            return argument1;
}
return argument0;
When I try to run the game it turns up an error that says Code Error DoSub :1: undefined value. Is there something that I am missing? I am following a youtube tutorial and yet it doesn't work for me
 

TsukaYuriko

☄️
Forum Staff
Moderator
Please always post the full error message. Partial error messages are about as helpful as telling your doctor that you're in pain, but not where it hurts. ;)
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
In GMS 2.3 the scripts system has changed. so initially I'd fix the script to meet the new standards then see if that fixes the issue. Basically wrap it in a function call and update the arguments, EG:

GML:
/// @function approach(a, b, amount)
/// @param a
/// @param b
/// @param amount

/// Moves "a" towards "b" by "amount" and returns the result
/// Does not overshoot "b"

function approach(a, b, amount)
{
if (a < b)
    {
    a += amount;
    if (a > b)
        return b;
    }
else
    {
    a -= amount;
        if (a < b)
            return b;
    }
return a;
}
 

OpulentCD

Member
Please always post the full error message. Partial error messages are about as helpful as telling your doctor that you're in pain, but not where it hurts. ;)
oops sorry im still new to this haha

The error message is:
############################################################################################
ERROR in
action number 1
of Create Event
for object <undefined>:

DoSub :1: undefined value
at gml_GlobalScript_Approach (line 18) - argument0 -= argument2;
############################################################################################
gml_GlobalScript_Approach (line 18)
 
Top