Wednesday, May 31, 2006

Code Sample: Object Population & Cloning

Object Population

Every object, from NPC's to weapons to landmarks, is given a StartRoom property. This property determines where the object begins at the beginning of the game.

Now all objects are coded outside of any room definition. In fact, each type of object is created in a respective .qlb (library) file. All wearable objects are coded in GAME_Items_Wearable.qlb. All static objects (scenery) are created in GAME_Items_Static.qlb. NPC's break this rule: there are too many of them. Some are contained in their own qlb file, while others are within geographically differentiated qlb files.

The Start Script for Faeguard loops through each object in the game, moving it to the value of the object's StartRoom property. This happens only when Faeguard is run for the first time.
I've implemented this because it's easier to manage large amounts of code in smaller chunks.

Cloning

But what if I need 2, or 3 or 50 short swords? Do I create each one individually, assigning unique object name's as I go? Hell, no.

Each object may also have a Clone property. This is a string property that lists additional locations to create the object. The string looks like this: DarkCave^WindyBeach^Clothier . This tells the Start Script to create additional copies of the object in the listed rooms. (The ^ just separates the locations so they can be recognized by the Start Script.)

Each cloned object substantiates with a numerical appendage to it's name. Let's say the object is an apple. It is created in it's StartRoom property. It has its Clone property set to Here^There^Everywhere. We first clone the object in the room called Here. We check if any object exists with the name apple2. There isn't an object with that name. We create the apple in the room Here with the name apple2. (It's alias is unchanged, so it looks the same, ie. 'apple', to the player.) Then we clone the object in the room called There. We check if any object exists with the name apple2. It does. We just created that object in the room called Here. So we check if any object exists with the name apple3. It doesn't. Apple3 has now been created in the room There. We repeat the same process for the coom called Everywhere.

You can check out the Start Script code here.

0 Comments:

Post a Comment

<< Home