Scripting your pet bug

Scripting your pet bug

The bug is the simplest to script so we will start with it.

This image is the one you need to upload to Second Life for the legs. The script will make it play as a movie.


The name should NOT be changed. The name should be Animation1;1;2;5. The numbers that are encoded in the name are the correct parameters to make the legs move in Second Life using one of my free scripts.

Right click it, save it to disk, and upload it to Second Life. Then drag a copy of it into the bug prim.

Make sure you put the texture for the legs inside the bug prims Object tab along with both scripts:


click for a larger view

You will need two scripts from the zip file or you can get the bug legs here and the movement script here.

Save these files to disk as text files. If they appear as one long line, you are editing them with notepad.exe. Try opening them in Wordpad.exe or some other text editor.

Now edit your bug and put both scripts in them by clicking new Script, editing that script, and copying and pasting the script from your editor into the Second Life window.


click for a larger view

Make sure the Mono box is checked, and press save.

When you save the animation script, the legs will appear and start to move:

with legs

click for a larger view

Now you can change and move your leg prim into place. Just remember that the orientation of 0,270,0 is important! The reason why is explained in my deep notes, later. For now, lets just move on to making your new pet.

Booting up your bugmobile

Go ahead and make a copy of your bug, because in the next step it is going to run away.

Now click the bug and it should start to move randomly.

If you want to modify this behavior and make your own bugs, read on.

If you want to make the pet frog, then go to the next step, Scripting Your Frog.

Scripting Details

The scripts consists of a program to read the numbers from any texture made by Gif2SLA and apply it to all sides of a prim. Put any texture converted by gif_2_SL_animation_v0.6.exe into a prim with this script to get it to play back automatically as a movie.

The second script contains a simple movement program and a listener.

Boot Up

When the program starts up, it runs the following code section:

        pos = llGetPos();            // save start
        llSetObjectName("Bug");      // the frog will look for this name


When you touch the prim, this section of code executes:

 // if owner touches us, set a new position
    touch_start(integer howmany)
        if (llDetectedKey(0) == llGetOwner())
            pos = llGetPos();

            //and start the movement
            llListen(2222,"","","eat");     // we turn invisible and rename when we hear commands on this channel


This code checks that the person that touched it is the owner. If this is true, the script will remember the position of the bug, set up a 2 second timer, and listen on a special channel for the word "eat".


This section of code then runs every 2 seconds:

        // get a position somewhere within 1/2 meter of home
        float newX = llFrand(1)+.5 -.75;
        float newY = llFrand(1)+.5 -.75;

         vector newpos = <pos.x + newX, pos.y + newY, pos.z>;  // in the same Z plane as when we were rezzed

        // look at that spot
        // and go there

This code section will calculate a new position for the bug to move to.

The llFrand() and math causes the bug to pick a number somewhere between 0.5 meters and 1.5 meters, either positive, or negative from the original remembered position. If you want to change this radius, just change the first number (1), and then make the second number (0.5) to be 1/2 of whatever you entered in place of the first number, and then add in 3/4's of the first number.

For example, if you want your bug to travel 3 meters, set the first number to 2, change the second to 1, and the last number to 1.5. The system must then pick a number from 0 to 2, adds 1 to it for a maximum of 3 and a minimum of 1, and then subtracts 1.5 from the number making it 50/50 chance it will be negative.

The bug then faces the destination with an OpenSim compatible replacement for llLookAt(). It then moves to the new position with llSetPos(). If you were wondering about the funny rotations when you made the bug, the reason is here. The llLookAt command causes an object to point its up axis (positive z) towards target, while keeping its forward axis (positive x) below the horizon. So we rotated the prim to compensate for this upward looking weirdness in llLookAt.


When the frog gets close to the bug, it will attempt to eat it by whispering the word "eat' on channel 2222. This triggers the listener function in the bug:

listen(integer n, string name, key id, string message)
        if (message == "eat")        // the frog is trying to grab us.
            list details = llGetObjectDetails(id,[OBJECT_POS]);

            // see if he is close enough to grab us.
            if (llVecDist( llList2Vector(details,0),llGetPos()) < 0.3)
                // make us invisible to people

                // and make us invisible to the frog
                llSetObjectName("invisible Bug");

                // okay, time to live again

When the message it hears is 'eat', the script checks that the distance between the bug and the frog is less than 0.3 meters. If it is further away, the bug does nothing. If the frog is that close or closer, the bug pretends to be eaten by turning invisible with the llSetAlpha and llSetLinkPrimitiveParams commands. This makes it appear that it has been eaten.

The bug then changes the name so that the frog can no longer find the bug. After 15 seconds, the bug reappears and changes the name back to Bug so that the frog can eat again.

Go to the next step, Scripting Your Frog.

Back to the Best Free Tools in Second Life and OpenSim.