Questions from the forum

Top  Previous  Next

Q: Is it possible to have a spinning object that decreases the health of all the entities that approach it?

A: Make sure that your entities store their health inside skill99 (just an example) and then use the code from below:

 

sound damaged_wav = <damaged.wav>; // use a very short, looping sound effect here

 

action spinning_entity // attach this action to your entity

{

       while (1)

       {

               my.pan += 5 * time_step; // 5 = rotation speed

               // scan around this entity on a range of 70 quants

               c_scan (my.x, my.pan, vector(360, 180, 70), SCAN_ENTS | SCAN_LIMIT | IGNORE_ME);

               wait (1);

       }

}

 

function player_scanned()

{

       my.skill99 -= 1.5 * time_step; // play with 1.5

       snd_play (damaged_wav, 20, 0);

}

 

action players_action // sample player action with health stored inside skill99

{

       my.skill99 = 100; // the player has 100 health points

       my.enable_scan = on;

       my.event = player_scanned;

       player = my; // I'm the player

       my.polygon = on;

       var players_distance;

       var anim_percentage;

       var attack_percentage;

       var sword_base;

       var sword_tip;

       while (my.skill99 > 0)

       {

               // place the camera 230 quants behind the player and 250 quants above it

               vec_set (camera.x, vector (-230, 0, 250));

               vec_rotate (camera.x, player.pan);

               vec_add (camera.x, player.x);

               camera.pan = player.pan;

               camera.tilt = -30; // look down at the player

               // rotate with the keys A and D or with the mouse

               my.pan += 5 * (key_a - key_d) * time_step - 10 * mouse_force.x * time_step;

               // move forward / backward with W / S, 10 gives the movement speed

               players_distance.x = 10 * (key_w - key_s) * time_step;

               players_distance.y = 0;

               players_distance.z = 0;

               c_move (my, players_distance, nullvector, glide);

 

               if ((key_w == 1) || (key_s == 1)) // the player is walking?

               {

                       ent_animate(my, "walk", anim_percentage, anm_cycle);

                       anim_percentage += 8 * time_step; // "8" controls the animation speed

               }

               else // the player is standing still?

               {

                       ent_animate(my, "stand", anim_percentage, anm_cycle);

                       anim_percentage += 0.5 * time_step; // "0.5" controls the animation speed

               }                        

 

               if (mouse_left == 1) // if we press the left mouse button

               {

                       attack_percentage = 0; // reset the "attack" animation frames

                       while (attack_percentage < 100)

                       {

                               vec_for_vertex (sword_tip, my, 456); // sword tip vertex - get the value in Med

                               vec_for_vertex (sword_base, my, 512); // sword base vertex - get the value in Med        

                               // the player has hit something?

                               if (c_trace (sword_base, sword_tip, ignore_me | ignore_passable) > 0)

                               {

                                       snd_play (sword_wav, 10, 0); // sword sound

                                       if (you != null)

                                       {

                                               // if the player has hit an entity, decrease its health

                                               you.healthpoints -= 10 * time_step;

                                       }

                               }

                               ent_animate(my, "attack_sword_up", attack_percentage, null);

                               attack_percentage += 6 * time_step;

                               wait (1);

                       }

               }

               wait (1);

       }

       // the player is dead here

       my.enable_scan = off;

       anim_percentage = 0;

       while (anim_percentage < 90)

       {

               ent_animate(my, "death", anim_percentage, null);

               anim_percentage += 3 * time_step; // "3" controls the animation speed

               wait (1);

       }

}

 

 

Q: How can I make an object "glow" while the mouse is placed over it, without clicking it?

A: Use the following example.

 

bmap cursor_tga = <cursor.tga>;

 

starter init_mouse()

{

       mouse_mode = 2; // show the mouse pointer

       mouse_map = cursor_tga;

       while (1)

       {

               mouse_pos.x = mouse_cursor.x;

               mouse_pos.y = mouse_cursor.y;

               wait (1);

       }

}

 

function entity_glow()

{

       while (event_type == event_touch)

       {

               my.lightrange = 100;

               my.blue = 255;

               my.green = 255;

               my.red = 255;                

               wait (1);

       }

       while (event_type == event_release)

       {

               my.lightrange = 0;

               my.blue = 0;

               my.green = 0;

               my.red = 0;                

               wait (1);

       }

}

 

action my_entity

{

       my.enable_touch = on;

       my.enable_release = on;

       my.event = entity_glow;

       // the rest of the code for your entity would go here

}

 

 

Q: How can I play an explosion as a stereo sound?

A: Use media_play with a stereo sound effect or fake the stereophony by playing a mono sound on both sound channels with a small delay.

 

sound explomono_wav = <explomono.wav>;

 

function explosion()

{

       media_play ("explosion.wav", null, 100); // use a stereo explosion.wav sound effect

}

 

function fake_stereo() // uses a monophonic explosion sound

{

       snd_play (explomono_wav, 100, -100); // play the explosion from the left speaker

       sleep (0.1); // play with 0.1

       snd_play (explomono_wav, 100, 100); // now play the same explosion from the right speaker

}

 

on_e = explosion;

on_f = fake_stereo;

 

 

Q: I want my gun model to display an animation when I press the left mouse button (fire). How can I do that?

A: Create your weapon as an "entity" definition - use the following snippet.

 

entity my_weapon

{

       type = <my_weapon.mdl>;

       x = 25; // 25 quants ahead of the view, play with this value

       y = -12; // 12 quants towards the right side of the screen, play with this value

       z = -9; // 9 quants below, play with this value

       pan = 2; // weapon's pan angle (you can also use tilt and roll)

       flags = visible;

}

 

starter animate_weapon()

{

       proc_kill(4); // don't allow more than 1 instance of this function to run

       while (my_weapon.frame < 10) // the weapon has 10 animation frames

       {

               my_weapon.frame += 2 * time_step; // 2 sets the animation speed

               wait (1);        

       }

       my_weapon.frame = 1; // restore the initial animation frame

}

 

on_mouse_left = animate_weapon;

 

 

Q: I have a model that only has the "stand" animation, but uses bones. How do I get it to loop just this animation?

A: Use this simple example.

 

action loop_bones

{

       var anim_speed;

       while(1)

       {

               ent_animate(my, NULL, 0, 0); // not really needed here

               anim_speed += 2 * time_step; // 2 = animation speed

               ent_animate(my, "stand", anim_speed, ANM_CYCLE);

               wait (1);

       }

}

 

 

Q: I would like to display several pictures in order, matching the value of a variable.

A: Use the following piece of code.

 

entity sprite_entity // adjust x, y, z until the picture is placed properly on the screen

{

       type = <pictures+6.tga>; // display 6 pictures

       x = 1000; // 100 quants ahead of the view, play with this value

       y = 280; // 280 quants towards the left side of the screen, play with this value

       z = -100; // 100 quants below, play with this value

       flags = visible;

}

 

starter change_picture()

{

       while (1)

       {

               sprite_entity.frame += 0.03 * time_step;

               if (sprite_entity.frame >= 7)

               {

                       sprite_entity.frame = 1;

               }

               wait (1);

       }

}

 

 

Q: Is there any way to save a string to an external .txt file? I need this for a question and answer / essay type of game.

A: Use this snippet.

 

string temp1_str = "evolution ";

string temp2_str = "is ";

string temp3_str = "progress ";

string temp4_str = "! ";

string temp5_str = "? ";

 

string temporary_str = "                                                                  "; // allow long quotes

 

var filehandle;

 

starter random_quotes()

{

       randomize();

}

 

function save_quote() // generates random, "wise" quotes

{

       var index;

       index = random(7);

       var tempo = 0;

       str_cpy (temporary_str, ""); // reset the string

       while (index > 0)

       {

               tempo = random (1);

               if (tempo <= 0.2)

               {

                       str_cat (temporary_str, temp1_str);

               }

               if ((tempo > 0.2) && (random (1) <= 0.4))

               {

                       str_cat (temporary_str, temp2_str);

               }

               if ((tempo > 0.4) && (random (1) <= 0.6))

               {

                       str_cat (temporary_str, temp3_str);

               }

               if ((tempo > 0.6) && (random (1) <= 0.8))

               {

                       str_cat (temporary_str, temp4_str);

               }

               if (tempo > 0.8)

               {

                       str_cat (temporary_str, temp5_str);

               }

               index -= 1;

       }

       // the quote was stored inside temporary_str here, so let's save it

       filehandle = file_open_write ("important_quote.txt");

       file_str_write (filehandle, temporary_str);

       file_close (filehandle);

}

 

on_s = save_quote; // press "S" to generate a new quote and save it to the "important_quote.txt" file on the disk

 

 

Q: I need an action that makes the player die when he touches a model. How can I do that?

A: Use event_impact to do it.

 

function kill_player()

{

       if (you == player) // the player has touched the killer entity?

       {

               player.skill99 = 0; // kills the player if its health is stored inside skill99

       }

}

 

action killer_entity

{

       my.enable_impact = on;

       my.event = kill_player;

}

 

 

Q: I have a problem with my character; it moves quite fast. How can I slow it down and where can I customize the moving options? I use a 3rd person camera and PlBiped01 for my player.

A: Right click your player model in Wed, and then choose "Behavior". Edit the values in the "Movement" section to change the movement speed and the values in the "Animation" section to change the animation speed. Read Aum59's "Using the templates" section for more information.

 

 

Q: How can we create particles that flow over a certain area, just like a bridge?

A: Use something like this.

 

bmap flame_pcx = <flame.pcx>;

 

function flame_particles()

{

       my.alpha -= 1 * time; // particle fading speed

       if (my.alpha < 0) {my.lifespan = 0;}

}

 

function torch_fx()

{

       temp.x = random(1) - 2; // play with x, y, z

       temp.y = random(1) - 4;

       temp.z = random(1) + 2;

       vec_normalize (temp, 20); // particle speed

       vec_add(my.vel_x, temp);

       my.bmap = flame_pcx;

       my.flare = on;

       my.bright = on;

       my.size = 20; // particle size

       my.move = on;

       my.beam = on;

       my.gravity = 1.2; // particle gravity

       my.function = flame_particles;

       my.lifespan = 100; // particle life span

}

 

action particle_generator

{

       my.invisible = on;

       my.passable = on;

       while (1)

       {

               effect (torch_fx, 5, my.x, normal); // generate 5 particles per frame

               wait (1);

       }

}

 

aum62_faq1