Weapon reload with ammo - how do I 'shorten' this code?

0 favourites
  • 4 posts
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • Hi all,

    I'm trying to make a weapon reload system, where you can reload your pistol if the clip has less than 10 bullets, and the clip can hold 10. However, it becomes problematic when trying to program it to add the remainder of the ammo when it is less than 10.

    So say there's 5 in the clip, and 8 in spare. It should subtract 5 from 8 and add 3 to the clip making it a total of 10 again. But there are many different combinations and it feels very inefficient to do it this way:

    As you can see it's 'easy' when there are always enough (10 or more) spare ammo. But say there is 3 in the clip left, and 4 in spare, it should add only 4 making it a total of 7.

    How do I go about shorten the code and not become 100 lines (as demonstrated in the image I posted)?

    Tagged:

  • Have a variable that holds your MaxAmmo (10) and a variable that holds your CurrentAmmo (0-10).

    When you reload:

    On reload you want to Figure out the amount of ammo you will be moving: MaxAmmo - CurrentAmmo

    Verify you have that amount, if you have less you move over everything you have, if you have that amount or more you move over that amount and then subtract it from your total ammo.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks for you reply.

    I'm not sure if I'm getting it, I have this now;

    Basically I'm trying to prevent the total ammo from going into the minus and just add what's left of the ammo to the clip.

    Am I heading in the right direction? 😅

  • Like this ^.^

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)