Now, let’s look at an in-game use for probability tables. The advanced example project demonstrates two similar methods for using these tables: random encounters on different terrains, and with interactable ‘item boxes’. The project is all done with placeholder art, so just use your imaginations!
There are two types of terrain square – grass and cave. Handily, they have their own probability tables. So the first thing to do is put those two terrain objects into a family (in the example it’s called EncounterZones) and give them a family instance variable called EncounterID. This variable will be used to tell the game which table it should be drawing values from when the player collides with a zone.
Condition
Player ▶︎ On collision with EncounterZones
Action
AdvancedRandom ▶︎ Set current probability table to EncounterZones.EncounterID
Now, every time the player walks on one of these terrain tiles, the correct probability table will be set. The debug box in the example project uses the AdvancedRandom.Weighted expression to display the value pulled from the table when the player walks on a tile too.
The item boxes work in a similar way, but the player has to press a button to pick an item and set the correct table. The ItemBox object has a similar instance variable to the EncounterZones family – ItemSetID. This is needed because each ItemBox in this project works with a different probability table.
Condition
Keyboard ▶︎ On Z key pressed
Sub-event Condition
Player ▶︎ Is overlapping ItemBox at offset (8,0)
OR
Player ▶︎ Is overlapping ItemBox at offset (-8,0)
OR
Player ▶︎ Is overlapping ItemBox at offset (0,8)
OR
Player ▶︎ Is overlapping ItemBox at offset (0,-8)
Sub-event Action
AdvancedRandom ▶︎ Set current probability table to ItemBox.ItemSetID
Now, when the player walks up to an item box and presses the Z key, the probability table will be set and you can trigger whatever action(s) you like.
And it’s as easy as that. It’s quite simple to set up probability tables and they’re really quite versatile when you know how to manipulate them. These projects showed relatively basic ways of implementing these tables, but I hope you can see how you could easily build up more complex systems with them!