With the removal of dynamic property definitions, I found that default values used to be quite useful, so I wrote my own dynamic property APIs. This comes with a few useful features:
- accessing properties through code - no more hardcoded strings
- default values (of course)
- optional setter callbacks, to have a function that will always run when setting the value of a property
Here's an example of how this implementation can be used:
for (const player of world.getAllPlayers()) {
// get the DynamicProperty object for the player
const money = DynamicProperties.Money.get(player);
// change the value - the callback will run
money.value = 100;
// display the new value
world.sendMessage(money.value);
}```
And this is how they are defined in JS:
```js
export const DynamicProperties = {
Nickname: new DynamicPropertyDef("nickname", "Unknown"),
HasEvolved: new DynamicPropertyDef("has_evolved", false),
Money: new DynamicPropertyDef("fear", 0, x => Math.max(x, 0)), // example callback - the value cannot go below 0
};