In the last post I mentioned that I had expanded on the variables feature Mobs uses (slightly!). In this post I’ll outline how it works in detail. Please note that while the code for this is pretty much complete, I still might change the syntax a little before release.
First, there are three kinds of variables:
- local – only used within the event
- global – available from any event
- constant – relevant to the event (and yes, I realize how odd ‘constant variable’ sounds!).
Variables local to the event are cleared every time the event happens. Global variables are defined when the plugin starts. Constants are read-only values (although you can copy and modify them) which differ depending on the event. Examples of these are the player’s name or HP, world name, etc.
Currently there are five types of variables:
- int – a whole number, like 1, 2, etc.
- string – a word or phrase
- bool – a yes/no or true/false value
- mobs – a list of mobs (including players)
- list – a list of strings
- blocks – a list of blocks
You interact with these in a similar way to actions, so for example creating a new int with a value of 10 would look like this
Later you can reference this value by name and display it, add to it, copy it, subtract from it, etc.
<log>The value myInt is currently ^myInt^!</log>
Global variables can be referenced by adding g: before the name, and event constants by adding c:
<log>Hello from ^c:world-name^! The value globalInt is currently ^g:globalInt^!</log>
Once we have created some variables we can alter them later in the event (for local variables) or from any event (for global ones)
Whatever the old value was, from here it will be 10 more.
When you want to change a value you normally edit the only copy but it is also possible to use a value to create a second, updated one
<add var="newInt" fromvar="oldInt">10</add>
In this example we have previously created a variable called oldInt and now are adding 10 to it and saving it as newInt. This means that we now have two values available, one of which is 10 more than the other.
Numbers are nice and simple but we can do more!
This code adds the event’s player to a global list, creating it if it doesn’t exist. Note that this command will be ignore if the event didn’t involve a player.
Let that go on a while (player spawn event, for example) and later we can do this
<select var="tempList" fromvar="g:myList" random="yes">5</select>
and now we have 5 random players to perform actions on!
More to come in part 2.