Questions from the forum

Top  Previous  Next

Q: I need a snippet that runs at game start, asks the player to input his name, and then writes it in a player.txt file. How do I do that?

A: Here's an example:

 

STRING* name_str = "#30"; // store up to 30 characters

 

TEXT* name_txt =

{

       pos_x = 10;

       pos_y = 50;

       string(name_str);

       flags = SHOW;

}

 

function input_name()

{

       var filehandle;

       str_cpy (name_str, "Input Player's Name, Please!");

       wait (-3);

       str_cpy(name_str, "                           "); // reset the input string

       inkey(name_str); // now let's input the name

       filehandle = file_open_write("player.txt"); // opens / overwrites the existing player.txt file (if any)

       file_str_write(filehandle, name_str); // write player's name

       file_close(filehandle);

}

 

function init_startup()

{

       wait (-3);

       input_name();

}

 

 

Q: I need to know how many quants an entity moves per frame.

A: The c_move instruction returns the covered number of quants per frame; you will find a simple example below. Please be aware that sometimes the covered distance can be very small (depending on the speed of the object) so you may need to multiply the tracking variable, just like I did (with 1000) in my example.

 

var covered_distance;

 

action my_model() // simple action that rotates this model in a circle

{

       while (1)

       {

               covered_distance = c_move (my, vector(5 * time_step, 0, 0), nullvector, IGNORE_PASSABLE | GLIDE);

               my.pan += 2 * time_step; // 2 sets the radius of the circle

               wait (1);

       }

}

 

PANEL* distance_pan =

{

       layer = 15;

       digits(10, 10, "Covered distance: %.f", *, 1000, covered_distance);

       flags = SHOW;

}

 

 

Q: How can I simulate day / night transitions?

A: You can find an example in the RPG template; here's the standalone version of the code:

 

var trpg_daynight_on = 1; // set it to 0 to stop the day / night transitions

var trpg_daynight_speed = 5;

var trpg_daynight_duration = 50;

var trpg_daynight_darkness = 90;

 

BMAP* black_tga = "black.tga";

 

ENTITY* sky;

 

PANEL* fader_pan;

 

function toggle_daynight();

 

function daynight_startup()

{

       while (trpg_daynight_on == 0) {wait (1);}

       

       fader_pan = pan_create(NULL, 0);

       fader_pan.bmap = bmap_create(black_tga);

       fader_pan.flags |= TRANSLUCENT;

 

       sky = ent_createlayer("skycube+6.tga", SKY | CUBE | SHOW, 1);

               

       var i;

       set (fader_pan, SHOW);

       

       while (trpg_daynight_on > 0)

       {

               fader_pan.scale_x = screen_size.x / bmap_width(black_tga); // scale the fader panel properly

               fader_pan.scale_y = screen_size.y / bmap_height(black_tga); // allowing it to fill the entire screen

               while (camera.ambient > -100)

               {

                       sky.ambient = maxv(-100, sky.ambient - 0.01 * trpg_daynight_speed * time_step);

                       camera.ambient = maxv(-100, camera.ambient - 0.01 * trpg_daynight_speed * time_step);

                       fader_pan.alpha = minv(trpg_daynight_darkness, fader_pan.alpha + 0.01 * trpg_daynight_speed * time_step);

                       wait (1);

               }

               i = 0;

               while (i < (trpg_daynight_duration * 10))        

               {

                       i += 1;

                       wait (1);        

               }

               while (camera.ambient < 0)

               {

                       sky.ambient = minv(100, sky.ambient + 0.01 * trpg_daynight_speed * time_step);                        

                       camera.ambient = minv(100, camera.ambient + 0.01 * trpg_daynight_speed * time_step);

                       fader_pan.alpha = maxv(0, fader_pan.alpha - 0.01 * trpg_daynight_speed * time_step);

                       wait (1);

               }

               i = 0;

               while (i < (trpg_daynight_duration * 10))        

               {

                       i += 1;

                       wait (1);        

               }

       }

       sky.ambient = 0;

       camera.ambient = 0;

       fader_pan.alpha = 0;

}

 

 

Q: I would like to have several enemies run their c_scan instructions, but I want to save CPU resources when the player is away from them. How do I do that?

A: Reduce the scanning range to the desired value - that's it! Here's a simple example:

 

action basic_enemy()

{

       while (1)

       {

               // don't scan for entities that are over 200 quants away from this entity

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

               if (you) // a target was detected?

               {

                       if (you == player)

                       {

                               beep();

                               // do what you need here

                       }

               }

               wait (1);

       }

}

 

 

Q: What is the best way to make a chain link fence?

A: Create a transparent (tga, png) sprite, make it passable and oriented (set its tilt angle to 0.1) and then use an invisible, impassable level block that prevents the player from entering the restricted area.

 

 

Q: How do you change the default game screen color from blue to another color?

A: Use this simple snippet:

 

function background_startup()

{

       wait (3);

       screen_color.red = 200; // purple background, set the desired RGB values here

       screen_color.green = 0;

       screen_color.blue = 200;

}

 

 

Q: I need a security camera that is triggered when the player enters a certain area, but it is reset when the player leaves the area (after a period of time)

A: There you go:

 

var camera_timer;

 

VIEW* camera1 =

{

       pos_x = 0;

       pos_y = 0;

       size_x = 400;

       size_y = 300;

       layer = 30;

}

 

function init_startup()

{

       wait (-1);

       video_switch(8, 0, 0); // change the resolution to 1024 x 768 pixels

       // set the desired positions and angles for the surveillance cameras

       vec_set (camera1.x, vector (800, 700, 100));

       // set the desired camera angles below

       camera1.pan = 50;

         camera1.tilt = 20;

       camera1.roll = 0;

}

 

action target_area() // attach this action to a model or sprite that was placed at the desired location

{

       set (my, PASSABLE | INVISIBLE); // don't shot the target entity

       while (!player) {wait (1);}

       while (1)

       {

               if (vec_dist (player.x, my.x) < 300) // the player has approached the target_area entity

               {

                       camera1.flags = SHOW;

                       camera_timer = 0;

               }

               else

               {

                       camera_timer += time_step / 16;

                       if (camera_timer > 3) // deactivate the security camera after 3 seconds

                               reset (camera1, SHOW);

               }

               wait (1);                

       }        

}

 

 

Q: I need to play a movie with media_play and want the program to wait until the movie has finished before continuing with the next line of code.

A: There you go:

 

var movie_handle;

 

function start_movie()

{

       movie_handle = media_play("mymovie.avi", NULL, 50);

       while (media_playing(movie_handle)) {wait (1);}

       beep(); beep(); // these beeps will only be heard at the end of the movie, use your own code here

}

 

function init_startup()

{

       on_s = start_movie; // press the "S" key to start the movie

}

 

 

Q: I want to adjust the volume of the snd_loop using hslider. Can anyone help me?

A: Here's a fully functional example.

 

BMAP* slider_tga = "slider.tga";

 

BMAP* pointer_tga = "pointer.tga";

 

var loop_volume = 80;

var loop_handle;

 

SOUND* myloop_wav = "myloop.wav";

 

function loop_startup()

{

       loop_handle = snd_loop(myloop_wav, loop_volume, 0); // play the sound in a loop

}

 

PANEL* adjust_pan =

{

       pos_x = 0;

       pos_y = 0;

       hslider(16, 24, 298, slider_tga, 0, 100, loop_volume);

       flags = SHOW;

}

 

function mouse_startup()

       mouse_mode = 2;

       mouse_map = pointer_tga;

       while (1)

       

               vec_set(mouse_pos, mouse_cursor);

               snd_tune(loop_handle, loop_volume, 0, 0); // adjust the volume whenever it is needed

               wait(1);

       }

}

 

 

Q: Just wanted to ask if anyone knows a way to make specific panel buttons visible or invisible.

A: You can easily use pan_setbutton to move them away and bring them back where they were whenever you need to. Here's a quick example:

 

var button_offset = 0;

 

BMAP* mybutton_pcx = "mybutton.pcx";

 

function start_game();

function move_button();

 

PANEL* mypanel_pan =

{

       layer = 10;

       bmap = "main.jpg";

       pos_x = 0;

       pos_y = 0;

       button (250, 0, mybutton_pcx, mybutton_pcx, mybutton_pcx, start_game, NULL, NULL);

       flags = SHOW;

}

 

function start_game()

{

       wait (1); // put your own code here        

}

 

function buttons_startup()

{

       on_m = move_button;

}

 

function move_button()

{

       if (button_offset == 0)

               button_offset = 5000; // move the button 5000 pixels downwards to hide it

       else

               if (button_offset == 5000)

                       button_offset = 0; // bring the button back to its initial y offset (zero)

       pan_setbutton(mypanel_pan, 1, 0, 250, button_offset, mybutton_pcx, mybutton_pcx, mybutton_pcx, NULL, start_game, NULL, NULL);

}