[This is a tutorial for my AS3 stat library which can be found here. You can find the full documentation here.]
This tutorial is meant to be a quick overview of the library. When functions and properties are mentioned, they will be linked to their respective location in the documentation.
Okay, let’s get started! In order to begin, you need to create a new stat system:
|
1 |
var myStats:StatSystem = new StatSystem(); |
From here, you can add as many stats as you want with addStat which takes a single parameter – a stat. Create a new Stat and set the first parameter as the desired name:
|
1 2 3 |
myStats.addStat(new Stat("speed")); myStats.addStat(new Stat("strength")); myStats.addStat(new Stat("awesomeness")); |
PRO TIP: You can also remove stats via removeStat function, however, you probably won’t use this too often as stats typically don’t disappear mid-game.
Congratulations, you have created your first stat system! However, you’ll probably want to know how to manipulate the values of the stats. Above we initialized our stats with only a name, which means all their values are currently 0. So let’s add some value to them. First, we need to retreive the stat from the stat system with getStat:
|
1 |
myStats.getStat("speed"); |
The function retrieves stats by name, so make sure you use the same spelling and capitalization, otherwise it will return null. To alter the value of the stat, we use the addValue function:
|
1 |
myStats.getStat("speed").addValue(100); |
Our speed stat now has a value of 100! To remove value from a stat, simply pass a negative value:
|
1 |
myStats.getStat("speed").addValue(-40); // speed now has a value of 60 |
PRO TIP: You can initialize a stat with as starting value by passing a second paramater into the Stat constructor. For example, to initialize a stat with a value of 9: myStats.addStat(new Stat(“awesomeness”, 9)).
Stats not only have a basic value, but also a max value. When the max value is set, the basic value will never be allowed to exceed the max value. Right now, the max value is at zero, so there is no limit to how high the stat can reach. Use addMaxValue to set the max value:
|
1 2 |
myStats.getStat("speed").addMaxValue(100); // max value is now 100 myStats.getStat("speed").addMaxValue(-60); // max value is now 40 |
As you may have noticed, the max value is now less than what we had set the basic value to previously. However, the addMaxValue function scales back the basic value if it exceeds the max value. So right now the max value and the basic value are set to 40.
To get the basic value and max value, you can use the read-only variable value and maxValue:
|
1 2 |
myStats.getStat("speed").value; // returns 40 myStats.getStat("strength").maxValue; // returns 0 |
PRO TIP: You can initialize a stat with as max value by passing a third paramater into the Stat constructor. For example, to initialize a stat with a value of 9and max value of 10 : myStats.addStat(new Stat(“awesomeness”, 9, 10)).
There will also be situations where you may want to save a stat or an entire system to a string, such as, saving a characters current progression to a file.
To save a entire stat system to a string, call saveToString:
|
1 2 3 4 5 6 7 8 9 |
myStats.saveToString(); /* OUTPUT: speed,40,40 strength,0,0 awesomeness,0,0 */ |
The above output is the result of our stat system up to this point. To load a this output into a new system, simply pass the string into the loadFromString function of the new system:
|
1 2 3 4 |
var systemData:String = myStats.saveToString(); var myNewStats = new StatSystem(); myNewStats.loadFromString(systemData); |
PRO TIP: You can also save/load individual stats by calling their own saveToString and loadFromString functions respectively.
This concludes the getting started tutorial!