Monthly Archives: May 2012

StatSystem: Experience and Level Management

[This is a tutorial for my AS3 stat library which can be found here. You can find the full documentation here.]

Typically when stats are involved in a game, there are also levels and experience that alter them. This tutorial will teach you how to implement an experience system using the library. When functions and properties are mentioned, they will be linked to their respective location in the documentation.

So let’s say we have a hero who should gain levels by meeting the required experience per level. First we must set up our stat system for the hero:

The health, mana, speed, and strength stats should be nothing new if you’ve completed the previous tutorials. The level and experience stats are what we are interested in.

The level stat will hold the player’s current level while the experience stat will hold the player’s current experience towards the next level. As you can see in the above code, the player is currently at level one with a max level of 100. Experience is currently at 0 with the requirement of 50 point in order to reach the next level.

Now we need to add one additional detail to the experience stat in order for this to work – the onFull function:

This property takes a function that is executed once the stat’s value meets the max max value, which just so happens to be very useful in occasions like this.

PRO TIP: There is also an onEmpty property that is executed once a stat is equal to zero. This can be useful for when a player’s health runs out!

Here’s how our levelUp function should look:

The above code block introduces two new functions – setValue and setMaxValue. Each of which sets the respective variable to the value passed in the parameter as opposed to adding via the addValue/addMaxValue functions.

PRO TIP: The setValue function will not allow a new value less than zero or greater than the max value and will execute the onEmpty and onFull functions respectively. Similarly, the setMaxValue function will not allow a new value less than zero, but has no positive limit. It will also scale the base value if it exceeds the new max value.

We’ll use addValue to reset the current experience to 0 and then just add a level-based amount to the max value. Using this method, the required experience for level2 will be 60, level3 will be 84, level4 134, and so on. However, you can use any formula you wish to calculate experience values. You can even use a preset array or vector for this too:

This concludes the experience and level management tutorial!

 

StatSystem: Getting Started

[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:

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:

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:

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:

Our speed stat now has a value of 100! To remove value from a stat, simply pass a negative value:

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:

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:

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:

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:

PRO TIP: You can also save/load individual stats by calling their own saveToString and loadFromString functions respectively.

This concludes the getting started tutorial!

StatSystem: Modifiers

[This is a tutorial for my AS3 stat library which can be found here. You can find the full documentation here.]

This tutorial will cover on how to use modifiers. When functions and properties are mentioned, they will be  linked to their respective location in the documentation.

Modifiers can be thought of as temporary stat effects that can be removed at a later time. Say, for example, you put on the “Boots of Blinding Speed” which has a +50 speed effect and -10 field of view effect. This effect isn’t permanent as it should only be active when the boots are being worn. Such cases are where modifiers come into play.

Adding a modifier is easy! First you must of course have an existing stat system:

Now the boots have two effects: a +50 speed boost and a -10 FOV penalty. So we’ll need to apply a modifier to each stat by using the addModifier function:

The modifier constructor takes several parameters – a name, the value, and the type. Let’s just look at the name and value for now, the type will be discussed later.

The name property is works the same way as a Stat - anything goes as long as you retain the same spelling and capitalization later when you want to retrieve or modify it. The value property is the amount that the modifier should add to the stat or the amount it should remove if the value is negative.

In order to get the value of the stat with modifiers, you must use the valueTotal property instead of the value property. The value property only returns the base value of that stat, that is, the amount without any modifiers (even if there modifiers present). The valueTotal property on the other hand, returns the value of that stat with all affecting modifiers. You can also get the total value of  just the modifiers by using the valueModifiers property. For example, if a stat has a modifier that adds +4 but also a modifier that removes -2, then the valueModifiers property will return 2.

Say you are tired of running around blind and would like to regain your vision, so you take off the boots which means you also need to remove the modifiers. To do so, use the removeModifier function:

PRO TIP: The removeModifier function also returns the removed modifier just in case you need it post-removal.

PRO TIP: The modifier class also contains saveToString and loadToString functions just in case you would like to save them.

As previously mentioned, modifiers also have a type parameter. Modifiers have two types as defined by the constants VALUE and MAX_VALUE in the Stat class. VALUE type modifiers affect the basic value of a stat while MAX_VALUE type modifiers will affect the max value of a stat. Why should there be two types?

Simply because there are situations where you may want to alter the max value of a stat rather than the basic value. For instance, say you put on a “Ring of Health” that grants +10 health points. Typically, you don’t want such an item to affect the current health but rather the total amount of health overall. This is when you use the MAX_VALUE type modifier:

This concludes the modifier tutorial!