本篇是游戏中物件的定义与使用(2)的续篇。
MIL编辑器的使用
快速且容易地创建物件的需求诞生了MIL编辑器,MIL编辑器不过是一个windows应用程序,它允许玩家操控物件列表,编辑每个想要的物件的属性特征。可以保存和加载MIL,但是物件属性的列表将保持固定不变(直到根据自己的意图重编它们)。
如下图所示,该对话框由一个包含所有物件的列表框,以及编辑物件信息,保存和加载物件列表的按钮组成。列表提供了1024个物件的空间,这意味着可以使用一个16位的变量去存储物件的编号(范围从0
- 1023)。
To begin using the Master Item List Editor, locate and execute the
MILEdit.exe file.
At the Master Item List Editor dialog box, you can perform the
following steps to add
or edit items, and then save them to disk:
1. Select an item from the Item List by double-clicking the item (or selecting
the item and clicking the Edit button), or add an item by clicking the Add
button. The Modify Item dialog box appears.
2. Edit the appropriate fields in the Modify Item dialog box.
3. When you finish editing, click OK to apply the changes and return to the
Master Item List Editor dialog.
4. To make the changes permanent, click Save on the Master Item List Editor
dialog box, and in the Save MIL File dialog box, enter a filename and save
the MIL to disk.
The MIL Editor uses the same
version of the sItem structure shown earlier, but I added
some extra item categories. Those extra categories are Shield, Healing, and
Container (a container object
such as a backpack). Here these extra categories are added to the ItemCategories
enum list, shown previously
in the section “Items Categories and Values”:
enum
ItemCategories
{
MONEY = 0, WEAPON, ARMOR,
SHIELD, ACCESSORY, EDIBLE,
HEALING, COLLECTION, TRANSPORTATION,
CONTAINER, OTHER
};
If you decide to modify the MIL
Editor to use different item attributes or categories,
modify the sItem structure as well. When you’re ready, you can start using
the item data you created in your game project.
Accessing Items from the MIL
Once you have a MIL, you can load the entire list into an array of sItem
structures
using the following code:
sItem
Items[1024]; // Array of sItem structures
// Open the Default.mil file
FILE *fp = fopen(“Default.mil”, “rb”);
// Read in all items
for(short i=0;i<1024;i++)
fread(&Items[i], 1, sizeof(sItem), fp);
// Close file
fclose(fp);
At this point, I’m assuming
that your item structure is relatively small and that you
are using no more than 1,024 items in your MIL. What happens if you extend each
item’s sItem structure or you begin storing more items in the MIL? We’re talking
about some serious memory usage.
To avoid loading each and every single item in memory from the MIL at once, you
can access individual items directly from the MIL. Because the size of each item
structure is fixed, you can access each item’s data by moving the file pointer
to the
appropriate position and reading in the structure, as in the following code bit:
//
ItemNum = reference # of item to load
sItem Item;
// Open the MIL file titled items.mil
FILE *fp=fopen(“items.mil”, “rb”);
// Seek to the appropriate position in file
// based on the size of the sItem structure and
// the number of the item to load.
fseek(fp, sizeof(sItem) * ItemNum, SEEK_SET);
// Read in the item structure
fread(&Item, 1, sizeof(sItem), fp);
// Close the file
fclose(fp);
And there you go—quick and easy access to every item in the MIL! Now, it’s time
to do something with those items.