Tut 4: Particle Control HUD

The objective of this tutorial is to build and script a HUD that can be used to control a particle system. The HUD will be able to start/stop particles emitted from an object rezzed on land. It will also be able to change the colour of the particles.

This tutorial also demonstrates how the description field of prims can be used as a carrier of data which can be used by scripts.

Particles 25

You can see each picture in its original dimensions by clicking on it!


Step 1. Rez a box.

Particles 01


Step 2. Shift-drag the box to create three copies of it as show in the picture below.

Particles 02


Step 3. Change the dimensions of the original box to <1.2, 0.2, 1.5>.

Particles 03


Step 4. Change the dimensions of each of the other three boxes to <0.25, 0.25, 0.25>.

Particles 04


Step 5. Move the large box forward until it overlaps with the three small boxes.

Particles 05


Step 6. Change the texture of the big box (the board) to Blank and move the three small boxes (the buttons) closer to each towards the top/left area of the board.

Particles 07


Step 7. Shift-drag the topmost button to the right and change the shape of the box in the right to a sphere. This sphere will act as a bullet to indicate which button was pressed.

Particles 08


Step 8. Shift-drag one of the buttons again and resize it in order to create a fourth large button at the bottom.

Particles 09


Step 9. Texture all buttons Blank and colour them as shown in the picture below.

Particles 10


Step 10. Right-click to download the two pictures below (start and stop) and then upload them to the virtual world.

start

stop


Step 11. Drag the start image from your inventory to the front face of the big white button. This will be the Start/Stop button.

Then select the button and drag and drop both the start and the stop images from your inventory to the content of the Start/Stop button as shown in the picture below.

Snapshot_028


Step 12. Hold Shift and select all prims, clicking on the Start/Stop button last. Link all prims (press Ctrl+L) and make sure the Start/Stop button is the root prim of the linkset, i.e. the prim with yellow borders.

Name the object Particle Control HUD and type a random number in its description field (in the example below the number is -200). This is the number of the hidden chat channel that your HUD will use to communicate to the particle system. Make sure you pick a number that is unlikely to be picked by another students who is working near you.

Particles 12


Step 13. Create a new script in the the content the Particle Control HUD (which is also the content of the root prim) and rename it to main script.

Open the script and copy&paste the following code into it:

integer on;
vector current_color;

default
{
  state_entry() {
    on = FALSE;
    llSetTexture("start", ALL_SIDES);
    llMessageLinked(LINK_ALL_CHILDREN, 0, "reset", NULL_KEY);
    llSay((integer)llGetObjectDesc(), "stop");
  }
 
  touch_start(integer total_number) {
    if (on) {
      on = FALSE;
      llSetTexture("start", ALL_SIDES);
      llSay((integer)llGetObjectDesc(), "stop");
    }
    else {
      on = TRUE;
      llSetTexture("stop", ALL_SIDES);
      llSay((integer)llGetObjectDesc(), "start");
    }
  }
  
  link_message(integer sender_number, integer number, string message, key id) {
    list bits = llCSV2List(message); 
    //note bits is a list of two STRING elements 
    //["color_vector", "position_vector"]

    current_color = (vector)llList2String(bits, 0); //update current color
    llSay((integer)llGetObjectDesc(), "color,"+(string)current_color); 
  } 
}

Particles 13


Step 14. Right-click on the Particle Control HUD and select Edit from the pie menu. Then check Edit Linked Parts in the Edit/Build dialogue box as shown below. Finally, click on the square purple button and

  1. type 1 into its description field (that will indicate the purple button is the pressed button when the main script is reset).
  2. add the button script (copy&paste the script below the image) into its content.

Particles 14

select_color() {
  llMessageLinked(LINK_ALL_OTHERS, 0, 
                  llList2CSV([llGetColor(ALL_SIDES), llGetLocalPos()]), 
                  NULL_KEY);
}

default {
  touch_start(integer total_number) {
    select_color();
  }

  link_message(integer sender_number, integer number, string message, key id) {
    if ((sender_number = LINK_ROOT) && (message == "reset")) {
      if (llGetObjectDesc() == "1") select_color();
    }
  }
}

Step 15. Open your inventory and then drag the button script from the content of the purple button to the Scripts folder in your inventory. Then select the orange and the green buttons in turn and drag&drop the button script from your inventory to the content of either of them.

Particles 15


Step 16. Now, select the purple bullet and create a new script in its content called bullet.

default {
  link_message(integer sender_number, integer number, string message, key id) {
    list bits = llCSV2List(message);
    //note bits is a list of two STRING elements 
    //["color_vector", "position_vector"]

    //change color to the color vector sent with the message
    llSetColor((vector)llList2String(bits, 0), ALL_SIDES);

    //change posiion to the posiion sent with the message
    vector cube_position = (vector)llList2String(bits, 1);
    vector ball_position = llGetLocalPos();
    llSetPos(<ball_position.x, ball_position.y, cube_position.z>);
  }
}

Particles 16


Step 17. Now it’s time to create the particle system. Rez a new sphere and name it particle system. In its description field type the same number that you typed in the description field of the Particle Control HUD (see step 12). Texture the sphere Blank.

Particles 17


Step 18. Make the sphere smaller, e.g. <0.25, 0.25, 0.25>, and add a new script called particle system to its content. Copy&paste the following code into the particle system script.

integer on;
vector current_color;

make_particles() {
  llParticleSystem([
  PSYS_PART_FLAGS, PSYS_PART_WIND_MASK |
                   PSYS_PART_INTERP_COLOR_MASK |
                   PSYS_PART_INTERP_SCALE_MASK |
                   PSYS_PART_EMISSIVE_MASK |
                   PSYS_PART_TARGET_POS_MASK,
  PSYS_SRC_PATTERN, PSYS_SRC_PATTERN_EXPLODE,
  PSYS_SRC_BURST_PART_COUNT, 2,
  PSYS_PART_MAX_AGE, 5,
  PSYS_PART_START_SCALE, <0.25, 0.25, 0.0>,
  PSYS_PART_END_SCALE, <0.01, 0.01, 0.0>,
  PSYS_PART_START_COLOR, current_color,
  PSYS_PART_END_COLOR, current_color + <0.5, 0.5, 0.5>,
  PSYS_SRC_TARGET_KEY, llGetKey()]);
}

default 
{
  state_entry() {
    on = FALSE;
    llParticleSystem([]); //the argument is an empty list
    llListen((integer)llGetObjectDesc(), "", NULL_KEY, "");
  }

  listen(integer channel, string name, key id, string message) {
    list bits = llCSV2List(message);
    string command = llList2String(bits, 0);

    if (command == "color") {
      current_color = (vector)llList2String(bits, 1);
      llSetColor(current_color, ALL_SIDES);
      if (on) make_particles();
    }
    else if (command == "start") {
      on = TRUE;
      make_particles();
    }
    else if (command == "stop") {
      on = FALSE;
      llParticleSystem([]);
    }
  }
}

Particles 19


Step 19. You can already test the Particle Control HUD. Try clicking the square buttons as well as the Start/Stop button. The Start/Stop button should switch on/off particles. When particles are off, clicking the square buttons should still colour the sphere of the particle system as show in the pictures below.

Particles 20

Particles 21


Step 20. Now, it’s time to make the HUD a real HUD. Take it to your inventory and find it in your Object folder.

Particles 22


Step 21. Right-click on Particle Control HUD in your inventory and choose Attach to HUD > Top Right. You should see this object attached to the top/right corner of your screen, but only a tiny bit of it will be visible.

Particles 23


Step 22. Right-click on the bit of the HUD that you can see in the top/right corner and choose Edit from the pie menu. Then hold Alt and move/roll the mouse to zoom out. You should see the screen area zoomed out as in the picture below.

Particles 24


Step 23. While the Particle Control HUD is selected, go to the Object tab in the Edit/Build dialogue box and change the rotation to <0.0, 0.0, 90.0>. Alternatively, hold Ctrl and turn the HUD around until it faces you.

Particles 25


Step 24. Hold Shift+Ctrl and use the grey squares at the corners of the HUD to resize it until it fits into the screen area.

Particles 26


Step 25. Finally, close the Edit/Build dialogue box. Now you can right-click on and detach the HUD from your screen. Then right-click on it in your inventory and choose to Wear it. It should get attached to the same place on your screen.

Press the buttons on the Particle Control HUD on your screen to test it again.

Particles 27


Step 26. Going beyond:

  1. Add a few more buttons with different colours to the HUD.
  2. Change the type of the particle effect emitted by the particle system.
  3. Add a button or buttons to change the type of the particle effect.

About nikniko101v

Lecturer at the University of Limerick, Ireland.

One comment

  1. Ravi

    Great tut, can’t wait to try it – thanks so much!

    Liked by 1 person

Leave a comment