1. Hey! Guest! The 32nd GMC Jam will take place between Feb 22nd, 12:00 UTC (Friday noon) and Feb 25th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GML Toggling dual-wielding with one key

Discussion in 'Programming' started by MegaJim73, Jan 17, 2019.

  1. MegaJim73

    MegaJim73 Member

    Joined:
    Sep 21, 2018
    Posts:
    15
    I have 3 gun objects for an FPS I'm making. One is the main gun object (pstl) and two are when the gun is dual wielded (leftpstl and rightpstl respectively). All are children of the parent gun object (par_gun). The main problem I have with this system is that when I press the toggle key to dual-wield weapons, it works, but when I implement the reverse with the same key, it no longer works. Any help would be greatly appreciated.

    When the toggle key is pressed:
    Code:
    if (global.havetwoguns[2]==true)
    {
    if (image_speed==0)
    {
        if (global.akimbo==0)
        {
            global.akimbo=1;
        }
        else
        {
            global.akimbo=0;
        };  
          switch (global.akimbo)
      {
          case 0:
          {
          with (par_gun) {instance_destroy();}
          instance_create(x,y,pstl);
          };
          break;
          case 1:
          {
          instance_deactivate_object(pstl);
          instance_create(x,y,leftpstl);
          instance_create(x,y,rightpstl);
          };
          break;
      };
    };
    };
    else
    {
        exit;
    };
     
  2. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    504
    The issue is too vague to work out what’s going on.

    So if you have two guns, the image speed is zero and akimbo is 0 (after it toggles in the code), then you destroy all guns and create a new pstl gun.

    What do you mean by “it no longer works”? Is there an error? Do the dual wielded guns destroy but no single gun appears?

    We need more details. Otherwise, for now, confirm that all those checks are in fact true when you need them to be (havetwoguns[2], akimbo, image_speed).
     
  3. MegaJim73

    MegaJim73 Member

    Joined:
    Sep 21, 2018
    Posts:
    15
    My apologies, I should have explained the code in more detail at the beginning.
    havetwoguns basically checks whether the player has picked up two guns to be able to dual-wield them.
    akimbo checks whether or not the gun is currently dual-wielded.
    image_speed checks whether the gun is not firing (no animation) so as to allow dual-wielding.
    When dual-wielding is enabled (aka akimbo is true), the single gun object is deactivated and the two gun objects are created as you can see from the code. This initially worked before I coded in the reverse.
    I then coded the reverse into the left gun object so that when akimbo is false, the two guns are destroyed and the single gun is created.
    The problem is that both are bound to the same key press event (it works when each are bound to different keys, but I want it to be bound to the same). That means that when I press the key just once, the single gun object is still stuck at that without ever changing to the two gun objects.
    I hope this helps explain what the problem is.
     
  4. Phischermen

    Phischermen Member

    Joined:
    Dec 23, 2016
    Posts:
    2
    If I understand correctly, your saying two objects control the toggle; your single pistol object and your left gun object. Which object does the code your showing belong to?
     
  5. MegaJim73

    MegaJim73 Member

    Joined:
    Sep 21, 2018
    Posts:
    15
    The code belongs to the single pistol object.
     
  6. Relic

    Relic Member

    Joined:
    Jun 27, 2017
    Posts:
    504
    you deactivate the pstl object when you started dual wielding. Can’t run the code after that.
     
  7. MegaJim73

    MegaJim73 Member

    Joined:
    Sep 21, 2018
    Posts:
    15
    I see. I actually kinda "solved" the problem; without destroying or deactivating the pstl object, I just rendered it unable to fire and invisible when dual-wielding is enabled, and vice-versa when it's disabled. Bound to the same key event, works like a charm.

    Code:
    if (global.havetwoguns[2]==true)
    {
    if (image_speed==0)
    {
        if (global.akimbo==0)
        {
            global.akimbo=1;
        }
        else
        {
            global.akimbo=0;
        };   
          switch (global.akimbo)
      {
          case 0:
          {
          with (leftpstl) {instance_destroy();}
          with (rightpstl) {instance_destroy();}
          with (pstl) {
          can_shoot=true;
          visible=true;
          }
          };
          break;
          case 1:
          {
          can_shoot=false;
          visible=false;
          instance_create(x,y,leftpstl);
          instance_create(x,y,rightpstl);
          };
          break;
      };
    };
    };
    else
    {
        exit;
    };
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice