Getting to grips with Advanced Random's Probability Tables

25

Index

Attached Files

The following files have been attached to this tutorial:

.c3p

basic-probability-table.c3p

Download now 157.44 KB
.c3p

advanced-probability-table.c3p

Download now 167.17 KB

Stats

7,998 visits, 16,971 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 29 Oct, 2019.

Advanced Probability Table Setup

So, now you know how to create, set and pull values from one or more probability tables, let’s expand on that a little bit. We’re going to look at creating tables from a dictionary, adding and removing entries from the current table as well as saving any changes we’ve made to local storage. Those tables are then going to be used for some random encounters and items.

Side note – huge thanks for Nepeo to helping me with a LOT of this!

Initialisation

There are a few bits that need setting up before you actually create the probability tables – mostly around setting up local storage calls and filing any necessary AJAX requests.

When the layout first starts, a check needs to be made to see if the probability tables already exist in local storage:

Condition

System ▶︎ On Start of Layout

Action

Local Storage ▶︎ Check item SPAWN_TABLES exists

Depending on whether or not the item exists, it either needs to be created from the project file or loaded from local storage:

Condition

Local Storage ▶︎ On item SPAWN_TABLES exists

Action

Function ▶︎ Call LoadTablesFromString (input: LocalStorage.ItemValue)

Condition

Local Storage ▶︎ On item SPAWN_TABLES missing

Action

AJAX ▶︎ Request ProbabilityTables.json (tag “CreateTables”)

Condition

AJAX ▶︎ On “CreateTables” completed

Action

Function ▶︎ Call LoadTablesFromString (input: AJAX.LastDate)

In both cases, the loading function (which we’ll define in a bit) is called, but uses different values for the parameter – either the JSON taken from local storage, or the freshly loaded project file. Both files allow us to build the probability tables using that load function.

Create a table from JSON

If you’ve been building these projects while following on from the tutorial, then you’ll probably have spotted the Create probability table from JSON action in the AR plugin. This allows you to use a JSON file (in this case a dictionary) to load and create multiple probability tables at once. (You could also build a single table from a file but where's the fun in that?)

The example project includes a prebuilt dictionary containing four probability tables and using a For Each loop allows us to build each of these tables with a simple function. In the dictionary, the name of the probability table (which will be used as its ID) is stored as the key while the values for the table are stored as an array as the key’s value.

On Function LoadTablesFromString

String parameter – Input

Function action

Dictionary ▶︎ Load from JSON string Input

Sub-event Condition

System ▶︎ Is PERSIST_TABLES

Sub-event Condition

Dictionary ▶︎ For each key

Sub-event action

AdvancedRandom ▶︎ Create probability table called Dictionary.CurrentKey from Dictionary.CurrentValue

If you open the debugger (as of r173) you should be able to see all of your probability tables now exist in the project. Should you want to make any changes (we’ll cover ways of doing that in a bit) and you need them saving for the next time you open a project – let’s say you have a unique item in a probability table that you want to remove as an option once the player has obtained it, then you can use a sort of reverse of the load function:

On Function SaveTablesToLocal

Sub-event Condition

System ▶︎ Is PERSIST_TABLES

Sub-event Condition

Dictionary ▶︎ For each key

Sub-event action

AdvancedRandom ▶︎ Set probability table to Dictionary.CurrentKey

Dictionary ▶︎ Set key Dictionary.CurrentKey to value AdvancedRandom.ProbabilityTableAsJSON

Sub-event Action

Local Storage ▶︎ Set item SPAWN_TABLES to Dictionary.AsJSON

In the example project, this function is triggered by hitting the S key. When that happens, your probability tables will be saved in local storage and will then be loaded the next time you preview or open the game.

But what about making those changes? In the example project, two keys are used to add a value and subtract that same value from whichever probability table is currently set:

Condition

Keyboard ▶︎ On “A” pressed

Action

AdvancedRandom ▶︎ Add entry “NewAddition” with weight 5

Condition

Keyboard ▶︎ On “D” pressed

Action

AdvancedRandom ▶︎ Remove entry “NewAddition” with weight 5

Again, this is a very simple way to demonstrate this aspect of probability tables, but if you look at the AR properties in the debugger, you can see where you’re adding and removing entries.

So, now your tables are set up and you can make and save changes to them, you can start using them!

Next Tutorial In Course

Using Advanced Random's Permutation Tables 05:34

This tutorial looks at setting up and using the Permutation Tables feature.

  • 5 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Probability tables are a great feature.

    Before the advanced random plugin you would probably have done something like choose( "A", "B", "B", "C", "C", "C", "C" ) for the 1xA, 2xB and 4xC example. Now imagine doing that for 10xA, 20xB and 40xC :P

    Great tutorial more people need to know about this feature.

  • Thanx.. I am just finishing a project that this would have saved a ton of extra coding.. My next project I will try this out and create a template for the furure.. Thanx again

  • Great! Didn't know this was possible. It's exactly what I need for loot drops. 👍

  • The only part i dont understand is the "load from JSON string" action where you have the "input" string parameter. what's the purpose of this?

  • Thank you so much! It clears a lot.