Questions from the forum |
Top Previous Next |
Q: How do I build a red, rotating light that can be used for a prison ceiling? A: Here you go:
VECTOR light_pos;
ENTITY* my_cube;
function red_light() { // make the light generating model invisible set (my, INVISIBLE | SPOTLIGHT); // generate light on a range of 200 quants my.lightrange = 200; // set a red light color (B = 0, G = 0, R = 255) while (1) { // add a random flickering effect if (random(1) > 0.95) vec_set(my.blue, vector(0, 0, 255)); else vec_set(my.blue, vector(0, 0, 155)); wait (-0.1); } }
action rotating_light() { my_cube = ent_create(CUBE_MDL, light_pos, red_light); while(1) { // generate a light source from the 10th vertex of the model (get the vertex number from Med) vec_for_vertex(light_pos, my, 10); vec_set (my_cube.x, light_pos.x); my.pan += 5 * time_step; wait(1); // wait one second } }
Q: How can I control the animation speed of my player model in real time? I'd like to use a system that allows me to fine tune the things and displays the animation speed value. A: Take a look at the code below:
var default_anim = 3; // default animation speed var anim_speed;
function speed_startup() { while (1) { // increase the animation speed when the player presses the Q key if (key_q) { default_anim += 0.1 * time_step; } else { // decrease the animation speed when the player presses the E key if (key_e) { default_anim -= 0.1 * time_step; } } wait (1); } }
action animated_model() { while (1) { anim_speed += default_anim * time_step; // use your own animation name below ent_animate(my, "walk", anim_speed, ANM_CYCLE); wait (1); } }
PANEL* animationspeed_pan = { layer = 15; digits(10, 10, "Animation speed: %.2f", *, 1, default_anim); flags = SHOW; }
Q: How can I create a rotating particle effect, like a spiral? A: Use a rotating model and generate particles from one or more if its vertices.
BMAP* effect_tga = "effect.tga";
function fade_particle(PARTICLE *p) { p.alpha += 0.5 * time_step; // fade in the particles if (p.alpha > 50) // they are fully visible? { p.alpha -= 1 * time_step; // then fade them out! } if (p.alpha < 0) p.lifespan = 0; }
function spiral_effect(PARTICLE *p) { p->vel_x = 0; // also try 1 - random(2); p->vel_y = 0; // also try 1 - random(2); p->vel_z = 3; // also try 3 + random(1); p.lifespan = 30; // this value sets the lifespan for your particles - play with it p.alpha = 40 + random(50); p.bmap = effect_tga; p.size = 3; p.flags |= (BRIGHT | MOVE); p.event = fade_particle; }
// place a model in the level and attach it this action - it will become invisible action spiral_generator() { VECTOR temp; set (my, INVISIBLE | PASSABLE); while (1) { my.pan += 15 * time_step; // generate particles using the 917th vertex of the model (get the vertex number from Med) vec_for_vertex(temp, my, 917); effect(spiral_effect, 1, temp.x, nullvector); // generate particles using the 1001st vertex of the model (get the vertex number from Med) vec_for_vertex(temp, my, 1001); effect(spiral_effect, 1, temp.x, nullvector); wait (1); } }
Q: How do I rotate a satellite around a planet? A: Here's an example:
var rotation_speed = 4; var rotation_radius = 60;
function rotate_around() { while (1) { my.skill1 += rotation_speed * time_step; // use any combination of x, y, z here my.y = you.y + rotation_radius * cos(my.skill1); my.z = you.z + rotation_radius * sin(my.skill1); wait (1); } }
action my_planet() { ent_create ("satellite.mdl", my.x, rotate_around); // create the satellite model that will rotate around the planet while (1) { // do other things with your planet here wait (1); } }
Q: How do I load a new level after the player passes through a vertically opening gate? A: There you go:
STRING* level2_wmb = "level2.wmb";
action sliding_gate() { // make sure to add a "player.my;" line of code to your player action while (!player) {wait (1);} // wait until the player comes closer than 100 quants to the gate while (vec_dist (player.x, my.x) > 200) {wait (1);} // the door slides along the z axis my.skill90 = my.z; while (my.z < my.skill90 + 100) // the door slides 100 quants along the z axis { my.z += 3 * time_step; // 3 gives the sliding speed wait (1); } level_load (level2_wmb); }
Q: Is there any way to dump all the resource names from my project into a text file? A: Sure, here's a sample:
// use a text as an array of strings TEXT* resources_txt = { strings = 200; // stores up to 200 file names }
function resources_startup() // get the names for all the files in the current folder { var filehandle; var i = 0; // find all the files names txt_for_dir(resources_txt, "*.*"); // opens / overwrites the existing resources.txt file (if any) filehandle = file_open_write("resources.txt"); // write up to 200 resources to the text file while (i < 200) { file_str_write(filehandle, (resources_txt.pstring)[i]); // write player's name // move on to the following row in the resources.txt file file_asc_write (filehandle, 13); // write the following strings on a new line file_asc_write (filehandle, 10); // using asc(13) = carriage return + asc(10) = line feed i++; // increment i } // all the resources are written to the text file now, so let's close the file file_close(filehandle); }
Q: How do I read a text using capitals from a file, change it to lower case, and then save it to a new text file? A: There you go:
STRING* data_str = "#50"; // store up to 50 characters per string
function resources_startup() // get the names for all the files in the current folder { var filehandle_read, filehandle_write; var end_of_file = 0; // open the upper.txt file for reading filehandle_read = file_open_read("upper.txt"); // create and open the lower.txt file for writing filehandle_write = file_open_write("lower.txt"); // read the data until the end of the file is reached while (end_of_file != -1) { // read the strings inside the file, one at a time end_of_file = file_str_read(filehandle_read, data_str); // convert them to lower case str_lwr(data_str); // write the string to lower.txt file_str_write(filehandle_write, data_str); // separate the strings using a comma file_str_write(filehandle_write, ","); } file_close(filehandle_read); // close upper.txt file_close(filehandle_write); // close lower.txt }
Q: Do you have a simple path_spline path patrolling example? I have tried several snippets, but none of them is good enough for what I need. A: Here's a snippet that uses path_spline and also decreases the NPCs movement speed at the corners, for an even smoother experience.
ENTITY* dummy1;
action smooth_walker() { var walk_percentage; var distance = 0; var previous_pan; var following_pan; var min_speed = 1; var max_speed = 3; var walking_speed; VECTOR last_pos[3]; VECTOR direction[3]; // create a dummy entity that will move on the path dummy1 = ent_create(NULL, nullvector, NULL); // make sure to name your path this way path_set(dummy1, "path_000"); while(1) { previous_pan = my.pan; // place the dummy entity on the path path_spline(dummy1, my.x, distance); distance += walking_speed * time_step; // let the npc look ahead vec_diff(direction, my.x, last_pos); vec_to_angle(my.pan, direction); vec_set(last_pos, my.x); wait(1); following_pan = my.pan; // sudden direction change during the last frame? Then lower the speed of the npc! if (abs(following_pan - previous_pan) > 1) { walking_speed -= 4 * time_step; } else { walking_speed += 2 * time_step; } // don't allow the walking speed to exceed the specified min / max limits walking_speed = clamp(walking_speed, min_speed, max_speed); ent_animate(my, "walk", walk_percentage, ANM_CYCLE); // play the "walk" animation walk_percentage += 3 * time_step; // "3" controls the animation speed } }
Q: I use a dynamic light with a variable range, but the lightrange increases and decreases effects are too abrupt. Is there any way of making a green light that gently increases its range, and then fades back, in a loop? A: Set a fixed lightrange value, and then change the RGB values (green, in your case) for a much smoother effect.
action my_light() { // make the light generating model invisible set (my, INVISIBLE); my.lightrange = 300; my.green = 0; // set a green light color (B = 0, G = 255, R = 0) vec_set(my.blue, vector(0, 255, 0)); while (1) { while (my.green < 250) { my.green += 3 * time_step; wait (1); } while (my.green > 10) { my.green -= 3 * time_step; wait (1); } } }
Q: How to I read all the soundtracks in my game folder, playing them in a random order, one at a time? A: There you go:
var track_handle;
// empty text used as a string array, this example uses 10 soundtracks TEXT* soundtracks_txt = { strings = 10; }
function random_tracks_startup() { var i; random_seed(0); // fill the text arrays with up to 30 soundtrack file names from the game folder txt_for_dir(soundtracks_txt,"*.mp3"); while (1) { i = integer(random(10)); // generate a ramdom number in the 0...9 interval if ((soundtracks_txt.pstring)[i] != NULL) { track_handle = media_play((soundtracks_txt.pstring)[i], NULL, 50); } while (media_playing (track_handle)) {wait (1);} // wait until the current track has ended } }
|