Tutorial - Variables I: Introduction
Mar 17, 2017 22:32:07 GMT -5
MidnightChipmunk and thecomputergeek2 like this
Post by niblexis on Mar 17, 2017 22:32:07 GMT -5
Variables are an extremely useful feature within MagicSpells, and you'll need them for just about any complex system of spells you'll make. They allow you to define a variable and establish values for them.
All information referenced on this tutorial can be found here.
Defining Variables
Variables can be defined in either your general.yml file, or an individual spell file.
Example:
Variables have a few different options you can use to establish rules for them.
Options:
Example:
Using Variables in Spells
So, going off of our example above. Let's say you wanted to have a system where players go up in levels by gaining experience, which they get by killing monsters.
To do this, we'll need to first establish two variables. One for levels, and one for experience.
So, we're assuming for the levels variable, that the level cap is 100. Thus the max value that the "Levels" variable can be at is at 100. The default is 1, as players will start at level 1. And the minimum is 1, because the lowest players will be at is 1. (There's no use for a level 0). And obviously, both "Levels" and "Experience" are permanent variables so that they will persist through a MagicSpells reload. Finally, the "Experience" variable has the "exp-bar" option set to true so that the value will display on the experience bar.
Now that we've established our variables, we need a way to make it so that players can "earn" this experience and gain levels. To do that, we'll need to modify the variables' values.
First thing's first, we'll need to establish how players actually gain this. In this case, we'll make it so killing zombies will give them experience.
Now that we have a method established for players to gain experience, we need to make a spell that actually modifies the value of the "experience" variable.
To modify variables, you use a spell configuration option called "variable-mods-cast". So, in this case, we would use it to add +1 to our "experience" variable.
This option can also be used to subtract an amount. (Put a '-' instead of a '+'). If you were to put "varname 0" (as opposed to "varname +1") it would set the variable's value back to 0.
When modifying variables, I usually just use a flat DummySpell, unless the variable-mods-cast option can be shoved into another spell. (Such as a ConjureSpell decreasing a variable value because you purchased something).
We now have it setup so that killing a zombie will give a player experience. Now we just have to make it so you can gain levels from experience.
To accomplish this, we'll need to make it so reaching 100 experience will give you 1 level and bring your experience back down to 0.
To start off, we'll make it so our experience gain passive links to another spell if the player has 99 experience when activating the passive.
To have a different spell activate when the passive goes off and the player is at 99 experience, we must use the "variableequals" condition. This checks to see if a variable is at a certain value. (You can also use variablemorethan or variablelessthan.)
Finally, we just have to put it all together and make the spell to give players a level.
If you have any questions about variables, post them in this thread and I'll try to answer them as best as I can.
All information referenced on this tutorial can be found here.
Defining Variables
Variables can be defined in either your general.yml file, or an individual spell file.
Example:
variables:
Money:
type: player
default: 0
min: 0
Variables have a few different options you can use to establish rules for them.
Options:
Option | Description | Default Setting |
type | The type of variable that it is. Can be player (value saved a player-to-player basis) or global (value saved for all players). | global |
default | The default value that the variable starts at. | 0 |
min | The minimum value the variable can be. | 0 |
max | The maximum value the variable can be. | (none) |
permanent | Whether the variable's value should persist through a plugin reload or not. | true |
scoreboard-title | The title for the scoreboard used to display the variable's value. Leaving this option out will disable the variable's scoreboard. (Only works on player-type variables). | (none) |
scoreboard-position | The position of the scoreboard. Can be either sidebar, nameplate, or playerlist. | (none) |
boss-bar | The title for the boss bar used to display the variable's value. Works for both global-type and player-type variables. | (none) |
exp-bar | Whether to display the variable's value on the experience bar of players. | false |
Example:
variables:
Experience:
type: player
default: 0
min: 0
max: 100
scoreboard-title: "&9&oExperience"
scoreboard-position: nameplate
boss-bar: "&9&oExperience"
exp-bar: true
Using Variables in Spells
So, going off of our example above. Let's say you wanted to have a system where players go up in levels by gaining experience, which they get by killing monsters.
To do this, we'll need to first establish two variables. One for levels, and one for experience.
variables:
Levels:
type: player
default: 1
min: 1
max: 100
permanent: true
Experience:
type: player
default: 0
min: 0
max: 100
permanent: true
exp-bar: true
So, we're assuming for the levels variable, that the level cap is 100. Thus the max value that the "Levels" variable can be at is at 100. The default is 1, as players will start at level 1. And the minimum is 1, because the lowest players will be at is 1. (There's no use for a level 0). And obviously, both "Levels" and "Experience" are permanent variables so that they will persist through a MagicSpells reload. Finally, the "Experience" variable has the "exp-bar" option set to true so that the value will display on the experience bar.
Now that we've established our variables, we need a way to make it so that players can "earn" this experience and gain levels. To do that, we'll need to modify the variables' values.
First thing's first, we'll need to establish how players actually gain this. In this case, we'll make it so killing zombies will give them experience.
Levels_Gain-Experience_Passive:
spell-class: ".PassiveSpell"
triggers:
- kill zombie
spells:
- Levels_Gain-Experience_Dummy
Now that we have a method established for players to gain experience, we need to make a spell that actually modifies the value of the "experience" variable.
To modify variables, you use a spell configuration option called "variable-mods-cast". So, in this case, we would use it to add +1 to our "experience" variable.
Levels_Gain-Experience_Dummy:
spell-class: ".instant.DummySpell"
variable-mods-cast:
- Experience +1
This option can also be used to subtract an amount. (Put a '-' instead of a '+'). If you were to put "varname 0" (as opposed to "varname +1") it would set the variable's value back to 0.
When modifying variables, I usually just use a flat DummySpell, unless the variable-mods-cast option can be shoved into another spell. (Such as a ConjureSpell decreasing a variable value because you purchased something).
We now have it setup so that killing a zombie will give a player experience. Now we just have to make it so you can gain levels from experience.
To accomplish this, we'll need to make it so reaching 100 experience will give you 1 level and bring your experience back down to 0.
To start off, we'll make it so our experience gain passive links to another spell if the player has 99 experience when activating the passive.
Levels_Gain-Experience_Passive:
spell-class: ".PassiveSpell"
triggers:
- kill zombie
spells:
- Levels_Gain-Experience_Dummy
modifiers:
- variableequals Experience:99 castinstead Levels_Gain-Levels_Dummy
To have a different spell activate when the passive goes off and the player is at 99 experience, we must use the "variableequals" condition. This checks to see if a variable is at a certain value. (You can also use variablemorethan or variablelessthan.)
Finally, we just have to put it all together and make the spell to give players a level.
Levels_Gain-Levels_Dummy:
spell-class: ".instant.DummySpell"
variable-mods-cast:
- Levels +1
- Experience 0
If you have any questions about variables, post them in this thread and I'll try to answer them as best as I can.