Scripting your pet bug

Join us in Outworldz at www.outworldz.com:9000 or follow us:

[Table of Contents]

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.

Animation1;1;2;5

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:

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.

Scripted

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:

state_entry()
    {
        llSetAlpha(1,ALL_SIDES);
        llSetLinkPrimitiveParams(2,[PRIM_COLOR,ALL_SIDES,<1,1,1>,1.0]);
        pos = llGetPos();            // save start
        llSetObjectName("Bug");      // the frog will look for this name
    }
    

Touch

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
            llSetTimerEvent(2);
            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".

Timer

This section of code then runs every 2 seconds:

timer()
    {
        // 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
        face_target(newpos);
        // and go there
        llSetPos(newpos);
    }

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.

Listen

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
                llSetAlpha(0,ALL_SIDES);
                llSetLinkPrimitiveParams(2,[PRIM_COLOR,ALL_SIDES,<1,1,1>,0]);

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

                // okay, time to live again
                llSetAlpha(1,ALL_SIDES);
                llSetLinkPrimitiveParams(2,[PRIM_COLOR,ALL_SIDES,<1,1,1>,1.0]);
                llSetObjectName("Bug");
            }
           
            
        }
    }
    

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.