How do I make a percentage-based chance for an event?

0 favourites
  • 4 posts
From the Asset Store
Hand-animated Sprite Base for isometric game/animation development
  • Hello. I'm currently trying to make upgrades for a character in my game and I was trying to make a mechanic where there is a chance not to consume ammo in the current magazine every time you shoot. That is simple enough, just make the percentage into a fraction and use the choose action on a boolean variable (or a number variable using 0 to stand for using ammo and 1 for not) to randomly pick whether ammo is used on the shot or not (choose(0, 0, 0, 1) for a 25% chance not to use ammo / 75% chance to use ammo, etc.). Now the only problem is that I want an upgrade that increases the chance to not consume ammo every time you hit an enemy up to a maximum of X percent (which would be like hitting 20 out of 40 bullets in the magazine to reach that cap where the chance not to consume ammo does not go up anymore) until you reload and it resets the chance to consume ammo to 100% / having the chance to not consume ammo be set back to 0%. I've already done the math, and if I want the player to reach the maximum of a 25% of not consuming ammo by hitting at least 20 bullets out of all the bullets in the magazine I would have to add 1.25% to the chance not to consume ammo (or subtract 1.25% from the chance to consume ammo if I wanted to do it that way around but it's pretty much the same thing just a different way to look at it) everytime a bullet hits an enemy. Unfortunately I don't think I can use choose on this, otherwise I would have to make an event and choose action for each seperate 1.25% containing different fractions that would be very unsimplified (I remember subtracting 1.25 from 100 and then converting that into fractions and the most simplified conversion I got was 70-something / 80-something. This means I would have to type out 0s and 1s and match the fraction perfectly in the choose action like the example above). This would take a long time to do, meaning I would have to make around 20 events just for this. And considering I want an ability that automatically bumps the chance to an additional 25% chance not to consume ammo for a few seconds means I would have to make like 40 events if i were to have the upgrade while using the ability. I'm pretty sure you can see how tedious and complicated this can be.

    So, I was wondering how I could make a percentage-based chance into an event like this where every time a bullet object is created it would have that random chance to consume ammo and how it could fit in with the upgrade I'm trying to make where the same percentage / variable is constantly being changed and has about 20-40 different total unique percentages.

    Hopefully this wasn't too confusing and maybe someone will be able to help me out with this? Thank you for just reading this, even if you don't answer and if you do then I'm very grateful (especially if your solution works out).

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Make a local variable in the consumption event, call it roll. Set it to random(0,100) in the ammo consumption event in the first sub-event.

    Have the chance of no ammo consumption as a variable of the player object, let's say it is called chance_of_no_ammo_consumption.

    In the ammo consumption event, add a subevent after the one where you set roll, compare variable roll great or equal to player.chance_of_no_ammo_consumption.

    As an action if that condition is met, add ammo consumption. If the roll is higher than the chance of ammo consumption, ammo will be consumed. If you roll a value lower than the ammo consumption chance, nothing will happen (so no ammo consumed).

  • Basic % chance logic - roll 100, compare results

    On Trigger

    Set RollVariable to random(100)

    If RollVariable comparison 1 is true, do something

    Else If RollVariable comparison 2 is true, do something


    For your comparison conditions, you can compare to another variable, which can be changed as you dictate

    For example, if you want a decreasing trigger chance -

    TriggerChanceVariable = 25

    If RollVariable <= TriggerChanceVariable - Subtract 1.25 from TriggerChanceVariable

    Else - Set TriggerChanceVariable to 25, subtract 1 bullet

  • Thank you very much, this was crazy helpful!

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