I made some commits last month to add extension points, which has solved the following use cases.
- Allow plugins to add options to the note when the note is generated
- Allow plugins to read options from the note and designate them to be saved
- Allow plugins to detect purchases that have custom settings associated with them
Still incomplete is the ability to change prices dynamically. That was in progress but I had to prioritize other work.
Here's an example of how to add a single field to the note.
// When clicking Save on a vending machine, this is called for each offer, so you can parse the note to save custom settings
void OnCustomVendingSetupOfferSettingsParse(Dictionary<string, string> localizedSettings, Dictionary<string, object> customSettings)
{
string costIncreasePerTransactionString;
if (localizedSettings.TryGetValue("Cost Increase Per Transaction", out costIncreasePerTransactionString))
{
float costIncreasePerTransaction;
if (float.TryParse(costIncreasePerTransactionString, out costIncreasePerTransaction))
{
customSettings["DynamicCostIncrease"] = costIncreasePerTransaction;
}
}
}
// When clicking Edit on a vending machine, this is called for each offer, so you can display custom settings in the note
void OnCustomVendingSetupOfferSettingsDisplay(Dictionary<string, object> customSettings, Dictionary<string, string> localizedSettings)
{
object costIncreasePerTransactionObject;
if (customSettings.TryGetValue("DynamicCostIncrease", out costIncreasePerTransactionObject))
{
if (costIncreasePerTransactionObject is float || costIncreasePerTransactionObject is double)
{
var amount = costIncreasePerTransactionObject is double
? Convert.ToInt32((double)costIncreasePerTransactionObject)
: (float)costIncreasePerTransactionObject;
localizedSettings["Cost Increase Per Transaction"] = amount.ToString();
return;
}
}
// No value is saved, so display the default value
localizedSettings["Cost Increase Per Transaction"] = "0.0";
}
Whenever you click the Edit button, each note will have a Cost Increase Per Transaction: 0.0 option. When clicking Save, the data file will have a "CustomSettings" entry with field "DynamicCostIncrease", like below.
"Offers": [
{
"SellItem": {
"ShortName": "tshirt",
"Amount": 1
},
"CurrencyItem": {
"ShortName": "scrap",
"Amount": 5
},
"CustomSettings": {
"DynamicCostIncrease": 2.0
}
},
To detect that a sale was made for an item that has custom settings, use the following hook.
void OnCustomVendingSetupTransactionWithCustomSettings(NPCVendingMachine vendingMachine, Dictionary<string, object> customSettings)
{
// TODO: Check customSettings for "DynamicCostIncrease", and maybe change the price
}
I haven't gotten to creating an example plugin. I was going to make a simple dynamic prices plugin, but I realize that the design is unclear. For example, if price should go up when a player makes a purchase, what if they buy in bulk? It's not really feasible to change the price in the middle of their transaction, so it would probably have to change after their transaction, which would create an inconsistency where if they buy individually they have to pay more. If you can clarify the behavior, I can possibly develop an example plugin. If you have other ideas on how to use this API, I can possibly implement an example for those.