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!