Questions from the forum |
Top Previous Next |
Q: I would like to display several pictures in a random order, matching the value of a random variable. A: Use the following snippet.
bmap picture1_pcx = <picture1.pcx>; bmap picture2_pcx = <picture2.pcx>; bmap picture3_pcx = <picture3.pcx>; bmap picture4_pcx = <picture4.pcx>; bmap picture5_pcx = <picture5.pcx>;
panel picture_pan { layer = 10; pos_x = 100; // use your own values here pos_y = 100; flags = visible; }
starter change_pictures() { while (1) { temp = 1 + int (random(5)); if (temp == 1) { picture_pan.bmap = picture1_pcx; } if (temp == 2) { picture_pan.bmap = picture2_pcx; } if (temp == 3) { picture_pan.bmap = picture3_pcx; } if (temp == 4) { picture_pan.bmap = picture4_pcx; } if (temp == 5) { picture_pan.bmap = picture5_pcx; } sleep (2); // change the picture every 2 seconds, play with this value } }
Q: Can you tell me what is vec_rotate? Where and how do I use it? A: Vec_rotate rotates a vector (camera's position, in the following example) with the angles given by another vector (vector (-300, -50, 70) in my example). Here's a small function that keeps the camera glued to the player, at the distances specified in the script; check Aum54's workshop for more pictures and info.
starter follow_player() { while (player == null) {wait (1);} // wait until the player is created while (1) { vec_set (camera.x, vector (-300, -50, 70)); // play with these values vec_rotate (camera.x, player.pan); vec_add (camera.x, player.x); camera.pan = player.pan; // only if you need this as well wait (1); } }
Q: Do you happen to have the code for a particle effect which will be created at a model's vertex position and will move upwards? A: Use the following example.
bmap smoke_tga = <smoke.tga>;
function fade_smoke() { my.alpha -= 0.15 * time; // particle fading speed if (my.alpha < 0) {my.lifespan = 0;} }
function smoke_effect() { temp.x = random(1) - 0.5; // move a bit on the x temp.y = random(1) - 0.5; // and y axis temp.z = random(2); // but always move upwards on the z axis vec_add (my.vel_x, temp); my.alpha = 10 + random(20); my.bmap = smoke_tga; my.size = 40; // size of the smoke my.bright = on; my.move = on; my.lifespan = 350; // lifespan for the smoke particles my.function = fade_smoke; }
starter generate_smoke() { while (player == null) {wait (1);} while (1) { vec_for_vertex (temp, player, 246); // generate smoke from the 246th vertex on the player model // create more smoke particles on more powerful computers effect (smoke_effect, (2 / (time_step + 0.1)), temp.x, normal); wait (1); } }
Q: I need a bonus score bitmap that appears on the screen and then moves upwards but fades away before it gets to the top of the screen. A: There you go.
bmap bonus_pcx = <bonus.pcx>;
panel bonus_pan { bmap = bonus_pcx; layer = 20; flags = transparent; }
function generate_bonus() { bonus_pan.pos_x = 400; // use your own x and y values here bonus_pan.pos_y = 500; bonus_pan.alpha = 100; bonus_pan.visible = on; while (bonus_pan.pos_y > 0) // scroll the panel upwards { bonus_pan.pos_y -= 10 * time_step; // 10 = vertical scrolling speed bonus_pan.pos_x -= 3 * time_step; // 3 = horizontal scrolling speed, use positive values as well bonus_pan.alpha -= 2.5 * time_step; // 2.5 = fading speed bonus_pan.alpha = clamp (bonus_pan.alpha, 0, 100); // limit the alpha to 0...100 wait (1); } }
on_b = generate_bonus; // call this function from within your game or press "B" to generate a bonus bitmap
Q: I need a function that reads a text from a file and copies it into string. Can you help? A: Sure. Use the following snippet.
string info_string[100]; // store up to 100 characters
text mytext { pos_x = 200; pos_y = 20; font = _a4font; // feel free to use any other previously defined font string = info_string; // this text shows the content of info_string flags = visible; }
function read_text() { var text_handle; text_handle = file_open_read("mytext.txt"); // create a file named mytext.txt and put your text inside it file_str_read(text_handle, info_string); // read the text and store it inside info_string file_close (text_handle); // now close the file }
// press the "R" key to read the text or call function read_text() from within your game when you need it on_r = read_text;
Q: Is it possible to allow the player to walk on a sphere? A: If your game is using a first person camera you can simulate that by rotating the sphere and keeping the player in a fixed position; I did that in the past and it worked fine. If you plan to use a 3rd person camera, you'll have to write your own (complex) movement code, using something like this in player's loop in order to adjust its angle, making it match the surface normal:
temp.tilt = 0; temp.roll = 0; temp.pan = -my.pan; vec_rotate(normal, temp); temp.tilt = -asin(normal.x); temp.roll = -asin(normal.y); my.tilt += 0.5 * ang(temp.tilt - my.tilt); // play with 0.5 my.roll += 0.5 * ang(temp.roll - my.roll); // play with 0.5
You will need to compute player's proper speeds on the x, y and z axis depending on its tilt angle.
Q: How can I rotate a model around a certain vertex? A: Use this piece of code.
var init_position; // position of the needed vertex var rotation_radius = 30;
string guard_mdl = <guard.mdl>;
function rotate_around() { while (1) { my.skill1 += 3 * time; my.y = init_position.y + rotation_radius * cos(my.skill1); // you can use any combination of x, y, z here my.z = init_position.z + rotation_radius * sin(my.skill1); wait (1); } }
action master_blaster { vec_for_vertex (init_position, my, 246); // get the position of the rotation center ent_create (guard_mdl, my.x, rotate_around); // create the model that will rotate around that point }
Q: How can I input some text? I mean like a text for player's name beneath his health bar. A: Use this example.
string players_str = " "; // use up to 20 characters for the name
text players_txt { layer = 30; pos_x = 20; // use proper x and y values here pos_y = 50; // make sure that the blinking cursor appears below player's health bar font = _a4font; // use any other previously defined font here string = players_str; }
function player_name_input() { players_txt.visible = on; // show the text str_cpy (players_str, ""); while (str_cmpi (players_str, "") == 1) // make sure that the player has typed something { inkey (players_str); // show the cursor -> store the input in players_str wait (1); } players_txt.visible = on; // show the text }
on_p = player_name_input; // press "P" to input player's name
Q: How can you position anything on an entity's coordinate axis instead of the world axis. A: Use this snippet as a base.
action dummy_player { player = my; }
action follow_player { // keeps the entity 50 quants ahead of its master, 20 quants sideways and 15 quants above master's origin var entity_offset[3] = 50, 20, 15; my.passable = on; // not really needed, but might come in handy for some objects while (player == null) {wait (1);} // wait until the player is created (you can use any other entity here) while (1) { vec_set (my.x, entity_offset.x); vec_rotate (my.x, player.pan); vec_add (my.x, player.x); my.pan = player.pan; wait (1); } }
Q: In my game I have created a swamp. If I touch the swamp block and the player is moving, I want to hear the swamp.wav sound. How can I make this possible? A: Use modern technology (A6) and the following snippet:
var swamp_handle;
sound swamp_wav = <swamp.wav>;
action player1 // attach this action to your player model { var movement_speed = 7; // movement speed var distance_covered = 0; player = my; // I'm the player my.invisible = on; while (1) { my.pan -= 7 * mouse_force.x * time; camera.x = my.x; camera.y = my.y; camera.z = my.z + 50 + 1.1 * sin(my.skill44); // play with 50 and 1.1 camera.pan = my.pan; camera.tilt += 5 * mouse_force.y * time; vec_set (temp.x, my.x); // trace 10,000 quants below the player temp.z -= 10000; temp.z = -c_trace (my.x, temp.x, ignore_me | ignore_passable | scan_texture) + 20; temp.x = movement_speed * (key_w - key_s) * time; temp.y = movement_speed * (key_a - key_d) * 0.6 * time; distance_covered = c_move (my, temp.x, nullvector, ignore_passable | glide); if (key_w + key_s > 0) // if the player is walking { my.skill44 += 25 * time; ent_cycle("walk", my.skill46); // play its "walk" frames animation my.skill46 += 5 * time; my.skill46 %= 100; // loop the animation } else // if the player is standing { ent_cycle("stand", my.skill48); // play the "stand" frames animation my.skill48 += 2 * time; // "stand" animation speed my.skill48 %= 100; // loop animation } // use any other name for the swampy texture; I have used "tiles3" from standard.wad in this example if ((str_cmpi ("tiles3", tex_name)) && (distance_covered > 0)) // moving over the tiles3 (swamp) texture? { if (snd_playing (swamp_handle) == null) // swamp.wav isn't being played at the moment? { swamp_handle = snd_play (swamp_wav, 40, 0); // then play it! } } wait (1); } }
|