[This is a tutorial for my AS3 stat library which can be found here. You can find the full documentation here.]
As of version 2.1.0, StatSystem contains inventory and item management features! This tutorial was created to introduce these new features. As usual, when functions and properties are mentioned, they will be linked to their respective location in the documentation.
The new features summarized in one sentence: an Inventory is a collection of ItemStacks, each of which hold an amount of an Item.
The Item class contains three basic properties:
- name – the name of the item as a string
- type – the type of item (this can be anything e.g. weapon, consumable, potion, and etc)
- stack – the maximum stackable amount for this item in an ItemStack
- stats – the StatSystem for the item
To create a new Item simply pass the desired name, (optional) item type, and (optional) max stack of the item. Here I’ve created a new item called “Health Potion” that is a “potion” type item and stacks to the maximum of eight.
|
|
var itemdata:Item = new Item("Health Potion", "potion", 8); |
PRO TIP: If you set the max stack of an item to -1, there will be no limit. This happens to be the default value if you do not pass a value in the constructor.
To clarify the “type” property: In RPGs, there are often different types of items – weapons, shields, armor, consumables, food, and so on – each that have a different set of stats. If you are familiar with FlashPunk, you can think of it the same way the Entity’s “type” property.
Now back to our Health Potion! Let’s say all of our potions in our imaginary game have following stats – health, mana, and worth. Since this our Health Potion obviously affects health, we’ll give it a value of 20, mana a value of 0, and lets make it an expensive by setting the worth to 500:
|
|
itemdata.stats.addStat(new Stat("health", 20)); itemdata.stats.addStat(new Stat("mana", 0)); itemdata.stats.addStat(new Stat("worth", 500)); |
The reason we still add the “mana” stat to the health potion is because we want all “potion” type items to have the same stats (but they don’t have to have the same value!) so when we need to do something with it, we don’t need have have any special cases and use a “catch all” code block. For instance, take this code snippet for example:
|
|
// inside function that uses an item if (itemdata.type == "potion") { player.stats.getStat("health").addValue(itemdata.getStat("health").value); player.stats.getStat("mana").addValue(itemdata.getStat("mana").value); } else if (itemdata.type == "weapon") { // weapon-y stuff here } |
Other good examples are weapons in rouge-likes since they often have a multitude of different attributes:
|
|
//Sword of Fire var itemdata:Item = new Item("Sword of Fire", "weapon", 1); itemdata.stats.addStat("slashing", 3); // swords do slashing damage itemdata.stats.addStat("crushing", 0); itemdata.stats.addStat("piercing", 0); // I guess you could poke someone with a sword itemdata.stats.addStat("fire", 10); // this isn't your normal kind of sword itemdata.stats.addStat("water", 0); itemdata.stats.addStat("earth", 0); |
|
|
//Mace of Rocks var itemdata:Item = new Item("Mace of Rocks", "weapon", 1); itemdata.stats.addStat("slashing", 0); itemdata.stats.addStat("crushing", 20); // smash them slimes! itemdata.stats.addStat("piercing", 0); itemdata.stats.addStat("fire", 0); itemdata.stats.addStat("water", 0); itemdata.stats.addStat("earth", 5); |
Now that we’re overflowing with cool items, you’ll likely want to store them all somewhere. This is where ItemStacks and Inventories come in handy!
First off, let me make it clear that an Inventory is a collection of ItemStacks, not Items. ItemStacks are stacks of identical items. If an item contains a property that changes over time (such as, charge, durability, etc) or can be changed by the player, you’ll want to set the max stack of the item to 1. For example, say you find a Steel Sword that has 10 points of durability left and you have a Steel Sword in your inventory that has full durability points. If the max stack isn’t set to 1, that almost broken Steel Sword will have max durability once you add it to your inventory since it will be added to a stack. Now here’s to actually use ItemStacks and Inventories.
An ItemStack has two properties : the item it contains and the amount of said item. So say we wanted to create a stack of Health Potions for a trip down into the dungeons:
|
|
var item:Item = new Item("Health Potion", "potion", 8); var stack:ItemStack = new ItemStack(item, 8); |
PRO TIP: It’s also worth mentioning that ItemStacks have three methods for altering the amount – add(), remove(), and setAmount() which should all be self explanatory.
Now we need to create an Inventory to hold the stack. Inventories have only one property – the maximum size. So let’s create a small inventory of size 5 and then add our stack of potions:
|
|
var inventory:Inventory = new Inventory(5); inventory.addItemStack(stack); |
PRO TIP: the size of an inventory is the number of slots, not the number of items it can contain.
Since our inventory was empty when we used addItemStack(), our stack of potions will take up the first slot. You can also add items via the addItem() method which takes an Item instead of an ItemStack and the amount of the Item to add.
To get our stack of items back from the inventory, we can use getBySlot() which returns an ItemStack at a specified slot in an inventory but doesn’t remove the ItemStack. Since our potions are in slot one, we’d pass “0″ into the parameters as the inventory slots are zero-based. To remove an ItemStack from a specified slot, use removeBySlot() by simply passing the slot and amount to remove in the parameters.
There will be other occasions where we won’t know where an item is in an inventory. This is where the removeByName() method comes in. The function takes a name and the amount to be removed.
This concludes the introduction to the item and inventory management features!