GMS 2 Making the equipped weapon change the whole player object sprite sheet

Discussion in 'Programming' started by Strigon, Jul 9, 2019.

  1. Strigon

    Strigon Member

    Jul 9, 2019
    Hello there,

    I'm new to GM2 and game design in general but have done a bunch of tutorial series and have now started my own project. My game is (or will be) a 2D side-scrolling shooter. The player doesn't aim and shoot with the mouse but simply fires left or right depending on which way they are facing. Players will have access to six weapons throughout the game and I have decided to incorporate each weapon into the player sprite. This means that when the player has the pistol equipped there will be a sprites for sPlayer_Idle_Pistol, sPlayer_Walk_Pistol, sPlayer_Shooting_Pistol and for the shotgun there will be sPlayer_Idle_Shotgun, sPlayer_Walk_Shotgun and so on and so forth.

    The issue I'm having is making the game swap sprite sets when a specific weapon is equipped. I've tried using the object_set_sprite function but that just replaces one sprite instead of replacing all of the sprites for each action (idle, walking, shooting, reloading etc).

    Can someone point me in the right direction of how I can fix this or point out a more efficient way of doing this?

    Thanks in advance,

    - Strigon
  2. Phil Strahl

    Phil Strahl Member

    Jul 3, 2016
    Welcome to the forums, Strigon!
    My approach to this would be having an object for each weapon that latches onto the player object. For example, it would follow the player's coordinates with an offset (since the player character is presumably holding the gun in their hands and that's not where their origin is). I hope that makes sense. If you have more questions or specific coding questions, feel free to ask :)
    woods likes this.
  3. obscene

    obscene Member

    Jun 21, 2016
    If you stuck to your naming convention, you could assemble the correct sprite name from a series of strings...

    Wherever you set your animation, set this string...

    Wherever you set your weapon, set this string...

    When it's time to draw, assemble them together...
    var sprite_name="spr_player_" + action_string + weapon_string;
  4. HeWhoShallNotBeNamed

    HeWhoShallNotBeNamed Member

    Nov 9, 2018
    You could also initialize your sprites in a switch statement. Then you'd end up with something like:

    case "pistol":
    idle_sprite = sPlayer_Idle_Pistol;
    walk_sprite = sPlayer_Walk_Pistol;
    shoot_sprite = sPlayer_Shoot_Pistol;

    ...and so on for the rest of your weapons. Then, when you push the key to shoot, you just call sprite_index = shoot_sprite.
  5. Strigon

    Strigon Member

    Jul 9, 2019
    Thanks for your replies and suggestions. I'll try using the switch statement and see how I get on with that.

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