Platform Tutorial Defining Items
From Multiverse
| MARS |
| Mob Server |
| Object Manager |
| Combat Server |
|
Abilities and Combat Plugin • Combat Statistics • Extending the MARS Combat System • Group System • Trainer Plug-in • Professions • Experience System |
| Tutorials |
|
Defining Quests • Scripting Mobs • Defining Items |
| Other Examples |
|
Triggering an Action with a Mouseclick • Scheduled Execution • Working with Sound • Creating a Teleporter |
See also Adding Consumable Items to Your World by Zik.
Contents |
Create a simple item
To create an item:
- Define an item template, and register it.
- Add an ability to the item. Usually, you want to make the item provide some ability.
- Make the item consumable, if appropriate.
Create an item template
Add the following code to mv-home/config/worldname/templates.py to define an item template:
item = Template("Healing Potion")
item.put(InventoryClient.ITEM_NAMESPACE,
InventoryClient.TEMPL_ICON,
"Interface\FantasyWorldIcons\ITEM_potion_A")
The constructor in the first line defines the item template. The argument is the name of the item (in this case, "Healing Potion"). The second line specifies the icon to use.
Add an ability for the item
Often, you want to make an item give the PC that is carrying it an ability. For example, a weapon may give some special combat ability.
So, add the following code to add the "heal potion" ability to the item:
item.put(InventoryClient.ITEM_NAMESPACE,
InventoryClient.TEMPL_ACTIVATE_HOOK,
AbilityActivateHook("heal potion"))
Register the template
Finally, as for any template, the new item template must be registered with the ObjectManagerClient:
ObjectManagerClient.registerTemplate(item)
Make the item consumable
A consumable item is eliminated once a player uses it. So, for example, a potion might be consumable, but generally something like a sword is not.
To make a consumable item, you need to make the item a reagent for the ability in mv-home/config/worldname/ability_db.py. To do that, look at this line from the "heal potion" ability:
ability.addReagent("Healing Potion")
Create an equippable item
An equippable item is one that a player character can "put on" or "wield," such as an article of clothing or a weapon. More accurately, it is an object that can be associated with an attachment point on a PC.
Create the equippable item template
The first step is to create the Template instance:
item = Template("Leather Pants")
Create an equippable item display context
Before you can equip an item, you must have a DisplayContext object to represent the information the client needs to display the item.
Below is code from multiverse/config/sampleworld/templates.py that creates the display context for human female leather pants:
human_female_leather_pantsDC = DisplayContext()
human_female_leather_pantsDC.setMeshFile("human_female.mesh")
human_female_leather_pantsDC.addSubmesh(DisplayContext.Submesh("leather_a_pantsShape-lib.0",
"human_female.leather_a_material"))
human_female_leather_pantsDC.addSubmesh(DisplayContext.Submesh("leather_a_beltShape-lib.0",
"human_female.leather_a_material"))
As in the example in Platform Tutorial Defining Mobs, the DisplayContext object is created, then it's mesh file is set property is set, and finally submesh name/material name pairs are added to the DisplayObject.
Create the MarsEquipInfo object
Add the following code to make an equippable item called "pants":
equipInfo = MarsEquipInfo("pants")
Make the item equippable
An equippable item can be carried or worn by an avatar. Use item.addEquipSlot() to specify the attachment point for an item. Valid attachment points are defined by static fields of MarsEquipSlot:
- MarsEquipSlot.CHEST
- MarsEquipSlot.FEET
- MarsEquipSlot.HANDS
- MarsEquipSlot.HEAD
- MarsEquipSlot.LEGS
- MarsEquipSlot.PRIMARYWEAPON
Add the following code to specify that a player can equip (attach) the item to the legs:
item.addEquipSlot(MarsEquipSlot.LEGS)
Associate the new display context with the item
The following lines from above associate the item's display context with the base human female model display context.
dcMap = DCMap() dcMap.add(human_female_base_DC, human_female_leather_pantsDC)
Associate the appropriate icon with the template:
item.put(InventoryClient.ITEM_NAMESPACE,
InventoryClient.TEMPL_ICON,
"Interface\FantasyWorldIcons\ARMOR_leather_A_legs")
This boilerplate causes this hook, which toggles the equip state of the item, to run whenever the client user clicks on the icon for the item:
item.put(InventoryClient.ITEM_NAMESPACE,
InventoryClient.TEMPL_ACTIVATE_HOOK,
EquipActivateHook())
Finally, complete the template definition by associating the MarsEquipInfo and the display context map with the item, and register
the template with the ObjectManagerClient:
item.put(InventoryClient.ITEM_NAMESPACE, InventoryClient.TEMPL_EQUIP_INFO, equipInfo) item.put(InventoryClient.ITEM_NAMESPACE, InventoryClient.TEMPL_DCMAP, dcMap) ObjectManagerClient.registerTemplate(item)
Create a display context for a hard attachment
Add the following code to create a display context for a hard attachment.
Hard attachment means it always looks the same....soft attachment means a diff submesh for attaching to different base models.
LongswordContext = DisplayContext()
LongswordContext.setMeshFile("sword.mesh")
LongswordContext.addSubmesh(DisplayContext.Submesh(
"polySurfaceShape1-lib.0/Material__45____Standard__",
"sword.Material__45____Standard__"))
LongswordContext.setAttachInfo(DisplayState.IN_COMBAT,
MarsEquipSlot.PRIMARYWEAPON,
MarsAttachSocket.PRIMARYWEAPON)
LongswordContext.setAttachInfo(DisplayState.NON_COMBAT,
MarsEquipSlot.PRIMARYWEAPON,
MarsAttachSocket.PRIMARYWEAPON)
