Automated Object Cloning
Tutorial by Chris Hunt – nospam@cwhunt.com
in collaboration with Ron Broglio – ron.broglio@lcc.gatech.edu
1. Purpose
This patch demonstrates how to create objects which can clone themselves or automatically create other objects.
We take a flower example. The player sees a flower in a valley. She ‘plucks’ it and a copy of the flower is put into her inventory; the first flower remains in the room for other people to pluck. The plucked flower is automatically removed after two minutes. The same techniques can be used to provide users with keys, identity-tracking objects, or remote controls (as in our Costume Closet).
2. Background
There are many ways to clone objects and none of them are fully satisfying; however, two methods are noteworthy. The first method requires wizard privileges but allows all users to clone the object. Basic programmers may use the second method but it operation excludes guests and simple players.
If you have wizard-level access, I suggest the first method: it is universal, scales easily, and each user has her own copy. The most common type of user is a guest or basic player and the second method of cloning excludes them.
3. Wizard Implementation, Unlimited Users
Once implemented, this version of cloning may be used by players of all security privileges, from guests to wizards. Its method makes a full copy of the object and each copy owns itself. Unless an automatic recycler is included, copies persist until they are recycled explicitly.
As already mentioned, this method has its problems: it requires wizard privileges and may create a small security hole—some security checks are over-ridden.
We will create a Flower object with two verbs. The same process can be used to create any other cloning object, which is why we go into so much detail. Of course, the names ‘Flower’ and ‘pluck’ can be changed. (However, ‘initialize’ cannot be renamed.)
Open the Object Editor. Press the ‘Create New Object’ button at the top, choose a generic thing (under basic objects), and call it Flower. Your new object should appear for editing in the same window. Close the Object Editor.
Open the Program Editor. Press the ‘My Objects’ button and scroll to the bottom of the list displayed in the upper right-hand corner; you should see your new Flower. Click on the text to open the Flower for program editing. Check ‘Read’ and ‘Fertile’, then press ‘Save Object’. Your screen should look like this:

Figure 3.1
Now we will create the verbs.
First, we will replace one of the system’s verbs for our Flower. This will allow objects to create other objects independently of the programmer. This creates a potential--but very low risk--security hole. Press ‘New Verb’ and call it ‘initialize’. Check ‘Read’, ‘Execute’, and ‘Debug’. Leave the arguments as ‘none’, ‘none’, and ‘none’. The text field should be left blank. Press ‘Compile Verb’. You should see the following:

Figure 3.2
Next we write the ‘pluck’ verb. Players will be able to type ‘pluck Flower’ to put a Plucked Flower into their inventory without affecting the Flower in the room.
Return to the primary editing screen for your Flower (figure 3.1)—this might require pressing ‘My Objects’ again and re-selecting the Flower. Press ‘New Verb’ again. Call this verb ‘pluck’. Permissions will be ‘Read’, ‘Execute’, ‘Debug’. Arguments will be ‘this’, ‘none’, ‘none’. Insert the following code into the text box:
temp = create(this, #-1);
temp.name = "Plucked Flower";
temp:moveto(player);
player:tell("The flower snaps as you pluck it and put it in your bag.");
fork (120)
recycle(temp);
player:tell("Your flower has disintegrated.");
endfork
"Code and tutorial by Chris Hunt -- nospam@cwhunt.com";
"Maintained and published by Dr. Ron Broglio, Georgia Tech";
" ron.broglio@lcc.gatech.edu";
Of course, you can change ‘“Plucked Flower”’ to any name you choose. The first line creates a new object with the same type as itself (ergo, ‘this’); ‘#-1’ tells the MOO that you want the new object to own itself. The third line actually moves the new object to the player’s inventory. The ‘fork’ command sets a 120 second delay before the object is deleted. (Omit the last four lines to disable automatic recycling.)
Press ‘Compile Verb’. When you finish, your screen should look like this:

Figure 3.3
Now you have a cloning flower! The parent flower should be in your inventory. Drop it in any room you want; I recommend locking it to that room to prevent other users from stealing it. Then your users can type ‘pluck Flower’ to put a Plucked Flower into their inventory. (Note: If you run into permission problems during this process, make sure you included an empty ‘initialize’ verb and you have wizard privileges.)
4. Wizard and Programmer Implementation, Limited Users
This method works for wizards and programmers. It has a significant flaw: it does not allow guests or standard players to clone objects. Generally, this flaw should preclude the method. It is included for completeness.
Much of the implementation is similar to the first method. We will create a Flower object with one verb. Of course, the names ‘Flower’ and ‘pluck’ can be changed.
Open the Object Editor. Press the ‘Create New Object’ button at the top, choose a generic thing (under basic objects), and call it Flower. Your new object should appear for editing in the same window. Close the Object Editor.
Open the Program Editor. Press the ‘My Objects’ button and scroll to the bottom of the list displayed in the upper right-hand corner; you should see your new Flower. Click on the text to open the Flower for program editing. Check ‘Read’ and ‘Fertile’, then press ‘Save Object’. Your screen should look like this:

Figure 4.1
Next we write the ‘pluck’ verb. Permitted players will be able to type ‘pluck Flower’ to put a Plucked Flower into their inventory without affecting the Flower in the room.
Press ‘New Verb’. Call this verb ‘pluck’. Permissions will be ‘Read’, ‘Execute’, ‘Debug’. Arguments will be ‘this’, ‘none’, ‘none’. Insert the following code into the text box:
temp = player:_create(this);
temp.name = "Plucked Flower";
temp:moveto(player);
player:tell("The flower snaps as you pluck it and put it in your bag.");
fork (120)
recycle(temp);
player:tell("Your flower has disintegrated.");
endfork
"Code and tutorial by Chris Hunt -- nospam@cwhunt.com";
"Maintained and published by Dr. Ron Broglio, Georgia Tech";
" ron.broglio@lcc.gatech.edu";
Of course, you can change ‘“Plucked Flower”’ to any name you choose. The first line creates a new copy of the object with you as its owner. The third line actually moves the new object to the player’s inventory. The ‘fork’ command sets a 120 second delay before the object is deleted. (Omit the last four lines to disable automatic recycling.)
Press ‘Compile Verb’. When you finish, your screen should look like this:

Figure 4.2
Now you have a cloning flower! The parent flower should be in your inventory. Drop it in any room you want; I recommend locking it to that room to prevent other users from stealing it. Then your users can type ‘pluck Flower’ to put a Plucked Flower into their inventory. (Note: Again, only builders, programmers, and wizards can clone objects built with this method. Expect permissions problems if you try to use it as a player or as a guest.)