Added example for storing data

Nathan Glover 2018-03-10 11:45:53 -06:00
parent 14e89492d4
commit e5c0ebd9fd

@ -76,7 +76,27 @@ Produces: ![server-info-example-picture](https://i.imgur.com/6yxjpsV.png) when t
Storing data from within the placeholder script is quite easy. When a placeholder script is called, a `Data` object is passed to it. Whenever the script modifies the object, it is modified in Javascript-Expansion as well, and then saved to a data file.
Here is an example script that...
Here is an example script that stores how many times a placeholder is viewed.
```Javascript
// Location keys are just like YAML files
var dataLoc = "%player_name%.viewed";
function viewCount() {
var views = Data.exists(dataLoc) ? Data.get(dataLoc) : 0;
views++;
Data.set(dataLoc, views);
Placeholder.saveData();
return views;
}
viewCount();
```
Produces: ![data-example](https://i.imgur.com/rmuRQcQ.png) when parsed for the first and second times for a given player.
**Valid Methods**:
- `Data.getData()` returns a `Map<String, Object>` of the entire placeholder script's data.
@ -86,6 +106,4 @@ Here is an example script that...
- `Data.remove(key)` removes a key from the data.
- `Data.set(key, value)` stores a `value` under `key`.
- `Data.isEmpty()` returns true if the data is empty; else false.
- `Placeholder.saveData()` saves the current data state to the data file.
// IN PROGRESS
- `Placeholder.saveData()` saves the current data state to the data file.