Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Using Variables to Set Probabilities

Since I haven't seen any tutorials on how to use Variables to create a percentage chance of something happening, I thought I would make one!

This can be very useful for things like:

Making spells with randomized effects, making treasure chests that give random loot, slot machines, etc.

First off, a percentage is simply a number over 100. For example, 12% is equal to 12/100, which means that if a spell has a 12% chance of hitting, on average if you cast the spell 100 times your spell would hit 12 of those times.

To make a Variable into a percentage, use the Set Variable function in the Event. Pick a Variable (I will use Variable 001 in all my examples) and click on Set. Now, go down below where it should say Random ___ ~ ___. This option allows you to enter 2 numbers and the Variable will pick a random number between the two you give it.

Now, say we wanted a 27% chance that the spell will cause Stun. Well, we would set the variable to Random 1 ~ 100.

Now, down below that, use Conditional Branch and click on Variable. Pick the variable you set to Random 1 ~ 100. The boxes below will now light up. Click on the drop-down box that should say Equal to. Change it to Less than or Equal To. Click the box below that (should be 0 by default) and change it to your percentage (in this case 27).

On the next line use the Change State function to inflict Stun on the enemy party.

Under the 'Else' option, you can put text, for example, saying "Failed to stun the enemy".

It should look something like this:

@ Set Variable 001 to Random 1 ~ 100
@ Conditional Branch: Variable 001 <= 27
@ Change State = +Stun
<> Else
@ Text: "Failed to stun the enemy"
@End Branch

Now, this variable will choose a random number from 1 to 100 and, if the number chosen is less than or equal to 27, then stun the enemy. This gives you exactly a 27% chance to stun.

You do not always need to pick numbers between 1 and 100. Here is a list the percentages that do not require you pick between 1 and 100:

Percentage...Variable Range...Less Than or Equal To
2%............1 ~ 50...........Variable <= 1
4%............1 ~ 25...........Variable <= 1
5%............1 ~ 20...........Variable <= 1
6%............1 ~ 50...........Variable <= 3
8%............1 ~ 25......….Variable <= 2
10%..........1 ~ 10...........Variable <= 1
12%..........1 ~ 25...........Variable <= 3
14%..........1 ~ 50...........Variable <= 7
15%..........1 ~ 20...........Variable <= 3
16%..........1 ~ 25...........Variable <= 4
18%..........1 ~ 50...........Variable <= 9
20%..........1 ~ 5.............Variable <= 1
22%..........1 ~ 50...........Variable <= 11
24%..........1 ~ 25...........Variable <= 6
25%..........1 ~ 4.............Variable <= 1
26%..........1 ~ 50...........Variable <= 13
28%..........1 ~ 25...........Variable <= 7
30%..........1 ~ 10...........Variable <= 3
32%..........1 ~ 25...........Variable <= 8
33%..........1 ~ 3.............Variable <= 1
34%..........1 ~ 3.............Variable <= 1
35%..........1 ~ 20...........Variable <= 7
36%..........1 ~ 25...........Variable <= 9
38%..........1 ~ 50...........Variable <= 19
40%..........1 ~ 5.............Variable <= 2
42%..........1 ~ 50...........Variable <= 21
44%..........1 ~ 25...........Variable <= 11
45%..........1 ~ 20...........Variable <= 9
46%..........1 ~ 50...........Variable <= 23
48%..........1 ~ 25...........Variable <= 12
50%..........1 ~ 2.............Variable <= 1
52%..........1 ~ 25...........Variable <= 13
54%..........1 ~ 50 ..........Variable <= 27
55%..........1 ~ 20 ..........Variable <= 11
56%..........1 ~ 25 ..........Variable <= 14
58%..........1 ~ 50...........Variable <= 29
60%..........1 ~ 5.............Variable <= 3
62%..........1 ~ 50...........Variable <= 31
64%..........1 ~ 25...........Variable <= 16
65%..........1 ~ 20...........Variable <= 13
66%..........1 ~ 3.............Variable <= 2
67%..........1 ~ 3.............Variable <= 2
68%..........1 ~ 25...........Variable <= 17
70%..........1 ~ 10...........Variable <= 7
72%..........1 ~ 25 ..........Variable <= 18
74%..........1 ~ 50...........Variable <= 37
75%..........1 ~ 4............Variable <= 3
76%..........1 ~ 25..........Variable <= 19
78%..........1 ~ 50..........Variable <= 39
80%..........1 ~ 5............Variable <= 4
82%..........1 ~ 50..........Variable <= 41
84%..........1 ~ 25..........Variable <= 21
85%..........1 ~ 20..........Variable <= 17
86%..........1 ~ 50..........Variable <= 43
88%..........1 ~ 25..........Variable <= 22
90%..........1 ~ 10..........Variable <= 9
92%..........1 ~ 25..........Variable <= 23
94%..........1 ~ 50..........Variable <= 47
95%..........1 ~ 20..........Variable <= 19
96%..........1 ~ 25..........Variable <= 24
98%..........1 ~ 50..........Variable <= 49

There you go.


How to get a percent of a value:

This is useful for things like:

A spell that heals 20% of MaxHP, spell that halves/quarters Current HP (ie: Demi spell), etc.

To do this, in the Event handler set a Variable equal to whatever value you are trying to alter. For example, if you are trying to heal a Player 1 for 20% of his Max HP:

Set Variable [001] = Player 1's MaxHP

Now, multiply that variable by whatever percentage you would like, in this case 20.

Set Variable [001] *= 20

Now, divide the variable by 100.

Set Variable [001] /= 100

This will give you exactly 20% of the original value.

Now, you can use the Change HP function to heal the player by Variable 001.

Example:

@Set Variable [001] = Player 1's MaxHP
@Set Variable [001] *= 20
@Set Variable [001] /= 100
@Change Player 1's HP + Variable [001]


Another Example: Player 1's Max HP is 250, we want to find 50% of that.

250 * 50 = 1,250

1,250 / 100 = 125

125 is 50% of 250. This variable can now be used to halve the player's Max HP, for example, by dealing 125 damage.
 
No they aren't, but they can be used to express a probability. ;p
If say... your gas tank is 40% full, that's not a probability >.>

Anyways, I don't really see an advantage to NOT using 1~100 for a variable >.>

To get an actual percentage itd be something like
Variable1 = Current Amount * 100 / Max Amount

o_ob
 
I meant percentages like 40% chance of X happening is a 4/10 probability. But, I should change the title to make it less confusing. How do you change the title?

And, by selecting from a smaller array can make it easier on your CPU if you are trying to draw multiple probabilities at once. Plus, it is just kind of a pet peeve of mine :)
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top