Adding gamepad button support ignores distance_to_object ()(solved)

Imiglikos

Member
Hi,

I have a friend in the game that the player is talking to. It works like this, the player will be within the friend's range (range <100), then press enter and a dialogue will start.
and it all works fine ..distance_to_object() and should take it into account as well as in the case of handling a keyboard key

I am asking you for help, thank you


this is my correct code ..

obj_dialogbox_friend

create

GML:
speak = false;
image_speed = 1;

step

GML:
if instance_exists(obj_hero)
if (!instance_exists(obj_box)) {
    if (!obj_hero.inAir) && (!place_meeting(x, y, obj_hero)) {
      
        if (distance_to_object(obj_hero) < 100) && (keyboard_check_pressed(vk_enter)) ;  {
        
    
      
    
      
      
      
        speak = true;
      
        dialogue_inst = instance_create(0, 0, obj_box);
          
            with (dialogue_inst) {
      
          
            text[0] = ("Hi how are you");
            avatar[0] = true;
            avatarSprite[0] = spr_friend;
            textColour[0] = c_black;
            shadow[0] = true;
            shadowWidth[0] = 4;

            text[1] = gmt("I'm fine. And you?");
            avatar[1] = true;
            avatarSprite[1] = spr_hero;
            textColour[1] = c_black;
            shadow[1] = true;
            shadowWidth[1] = 4;

            text[2] = gmt("Okay too");
            avatar[2] = true;
            avatarSprite[2] = spr_friend;
            textColour[2] = c_black;
            shadow[2] = true;
            shadowWidth[2] = 4;

            curText = 0;
            maxText = 2;

            add_text(text[curText], avatar[curText],avatarSprite[curText],textColour[curText],shadow[curText], shadowWidth[curText]);
          
          
  
            
        }
    }
}

}



else {

  

        if (obj_hero.x < x) {
            image_xscale = -1;
        }

        else {
            image_xscale = 1;
        }

        obj_hero.image_xscale = image_xscale * -1;

}

and here now, when I add support for the gamepad key, there is this problem as i described above ..



GML:
        if (distance_to_object(obj_hero) < 100) && (keyboard_check_pressed(vk_enter)) || (gamepad_button_check_pressed(0,gp_shoulderr)); {

GML:
if instance_exists(obj_hero)
if (!instance_exists(obj_box)) {
    if (!obj_hero.inAir) && (!place_meeting(x, y, obj_hero)) {
      
        if (distance_to_object(obj_hero) < 100) && (keyboard_check_pressed(vk_enter)) || (gamepad_button_check_pressed(0,gp_shoulderr)); {
        
    
      
    
      
      
      
        speak = true;
      
        dialogue_inst = instance_create(0, 0, obj_box);
          
            with (dialogue_inst) {
      
          
            text[0] = ("Hi how are you");
            avatar[0] = true;
            avatarSprite[0] = spr_friend;
            textColour[0] = c_black;
            shadow[0] = true;
            shadowWidth[0] = 4;

            text[1] = gmt("I'm fine. And you?");
            avatar[1] = true;
            avatarSprite[1] = spr_hero;
            textColour[1] = c_black;
            shadow[1] = true;
            shadowWidth[1] = 4;

            text[2] = gmt("Okay too");
            avatar[2] = true;
            avatarSprite[2] = spr_friend;
            textColour[2] = c_black;
            shadow[2] = true;
            shadowWidth[2] = 4;

            curText = 0;
            maxText = 2;

            add_text(text[curText], avatar[curText],avatarSprite[curText],textColour[curText],shadow[curText], shadowWidth[curText]);
          
          
  
            
        }
    }
}

}



else {

  

        if (obj_hero.x < x) {
            image_xscale = -1;
        }

        else {
            image_xscale = 1;
        }

        obj_hero.image_xscale = image_xscale * -1;

}
 
Last edited:

saffeine

Member
it's not ignoring it, you just haven't set up the conditions properly, and also there's a stray semi-colon at the end of it too.

if ( distance_to_object(obj_hero) < 100) && (keyboard_check_pressed(vk_enter) ) || (gamepad_button_check_pressed(0,gp_shoulderr) ); vs
if ( distance_to_object(obj_hero) < 100) && (keyboard_check_pressed(vk_enter) ) || (gamepad_button_check_pressed(0,gp_shoulderr) ) vs
if ( distance_to_object(obj_hero) < 100 && ( keyboard_check_pressed(vk_enter) || (gamepad_button_check_pressed(0,gp_shoulderr) ) )

the first one says if ( the distance to obj_hero is less than 100 and enter is pressed ) or ( the right shoulder is pressed ), break. this is leaving the distance out of the gamepad check and breaking early.
the second one says if ( the distance to obj_hero is less than 100 and enter is pressed ) or ( the right shoulder is pressed ), continue, which does the same thing but isn't breaking early.
the last one says if ( the distance to obj_hero is less than 100 ) and ( enter is pressed or the right shoulder is pressed ), continue, which is probably what you're looking for.

keep an eye on your parenthesis and your semi-colons. syntax is vital.
 
Last edited:

Imiglikos

Member
it's not ignoring it, you just haven't set up the conditions properly, and also there's a stray semi-colon at the end of it too.

if ( distance_to_object(obj_hero) < 100) && (keyboard_check_pressed(vk_enter) ) || (gamepad_button_check_pressed(0,gp_shoulderr) ); vs
if ( distance_to_object(obj_hero) < 100) && (keyboard_check_pressed(vk_enter) ) || (gamepad_button_check_pressed(0,gp_shoulderr) ) vs
if ( distance_to_object(obj_hero) < 100 && ( keyboard_check_pressed(vk_enter) || (gamepad_button_check_pressed(0,gp_shoulderr) ) )

the first one says if ( the distance to obj_hero is less than 100 and enter is pressed ) or ( the right shoulder is pressed ), break. this is leaving the distance out of the gamepad check and breaking early.
the second one says if ( the distance to obj_hero is less than 100 and enter is pressed ) or ( the right shoulder is pressed ), continue, which does the same thing but isn't breaking early.
the last one says if ( the distance to obj_hero is less than 100 ) and ( enter is pressed or the right shoulder is pressed ), continue, which is probably what you're looking for.

keep an eye on your parenthesis and your semi-colons. syntax is vital.
Thank you for your help, I put the parenthesis so wrong ..
 
Last edited:
Top