Questions from the forum

Top  Previous  Next

Q: Is there an opposite event for EVENT_SCAN? I want to display a text when I "see" an entity and let this text disappear when I don't see the entity anymore.

A: Here's an example that uses EVENT_SCAN alone to do what you want.

 

TEXT* scanning_txt =

{

       string("Entity detected!");

}

 

function got_scanned()

{

       while (event_type == EVENT_SCAN)

       {

               set (scanning_txt, VISIBLE);

               wait (1);

       }

       reset (scanning_txt, VISIBLE);

}

 

action my_entity() // attach this action to your entities

{

       my.emask |= ENABLE_SCAN; // make the entity sensitive to scanning

       my.event = got_scanned;

}

 

action players_code() // simple player and 1st person camera code

{

       player = my; // I'm the player

       set (my, INVISIBLE); // no need to see player's model in 1st person mode

       while (1)

       {

               // move the player using the "W", "S", "A" and "D" keys; "10" = movement speed, "6" = strafing speed

               c_move (my, vector(10 * (key_w - key_s) * time_step, 6 * (key_a - key_d) * time_step, 0), nullvector, GLIDE);

               vec_set (camera.x, player.x); // use player's x and y for the camera as well

               camera.z += 30; // place the camera 30 quants above the player on the z axis (approximate eye level)

               camera.pan -= 5 * mouse_force.x * time_step; // rotate the camera around by moving the mouse

               camera.tilt += 3 * mouse_force.y * time_step; // on its x and y axis

               player.pan = camera.pan; // the camera and the player have the same pan angle

 

               // the following line makes the player scan up to 300 quants around it

               c_scan(my.x, my.pan, vector(360, 180, 300), IGNORE_ME);

 

               wait (1);

       }

}

 

 

Q: I use particles for my waterfall; when I move away from my particle effect I can only see a few particles and this doesn't look natural. How can I fix that?

A: The particles are clipped when you move away from them, in order to increase the frame rate; check out "clip_particles" in the manual and set a better value for it in order to fix the problem.

 

 

Q: I'd like a function that fades in a music track, plays it for 1 minute and then fades it out. Can you help?

A: Here's an example.

 

function music_startup()

{

       var tune_handle;

       var tune_factor = 1; // play the sound at an initial volume of 1

       tune_handle = media_play("mymusic.wav", NULL, tune_factor);

       while (tune_factor < 80) // increase the volume until it reaches 80

       {

               media_tune(tune_handle, tune_factor, 100, 0);

               tune_factor += 1 * time_step; // 1 gives the fade-in speed

               wait (1);

       }

       wait (-60); // play the music loud for 1 minute

       while (tune_factor > 0) // now change the volume - decrease it gently

       {

               media_tune(tune_handle, tune_factor, 100, 0);

               tune_factor -= 0.6 * time_step; // 0.6 gives the fade-out speed

               wait (1);

       }

       media_stop (tune_handle); // now we can stop the sound

}

 

 

Q: I'd be happy to make an aimlessly moving AI that changes direction at a random interval between 4 and 10 seconds.

A: There you go.

 

action random_guy()

{

       var anim_percentage;

       var time_passed = 0;

       var random_interval;

       while (1)

       {

               if (time_passed == 0)

               {

                       random_interval = 4 + random(6);

               }

               c_move (my, vector(5 * time_step, 0, 0), nullvector, GLIDE); // "5" controls the walking speed

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

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

               time_passed += time_step / 16;

               if (time_passed > random_interval)

               {

                       time_passed = 0; // reset time_passed

                       my.pan += 90 - random(180); // and then add -90...+90 degrees to the pan angle

               }

               wait (1);

       }

}

 

 

Q: In my car simulation, I want to make a log file that shows what the driver does at a certain time. Can anyone help?

A: Here's an example that logs player's x y z pan tilt roll every time when the "L" key is being pressed and ends the logging process when the player presses the "E" key.

 

var end_logging = 0;

var enable_log = 0;

 

function log_startup()

{

       var filehandle;        

       // make sure that player's action includes a "player = my;" line of code

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

       filehandle = file_open_write("carlog.txt");

       while (!end_logging)

       {

               file_var_write(filehandle, player.x);

               file_var_write(filehandle, player.y);

               file_var_write(filehandle, player.z);

               file_var_write(filehandle, player.pan);

               file_var_write(filehandle, player.tilt);

               file_var_write(filehandle, player.roll);

               file_asc_write (filehandle, 13); // add a newline character (move to the following line)

            file_asc_write (filehandle, 10);

               wait (1);

               while (!enable_log) {wait (1);} // don't log anything until enable_log isn't set to 0

       }

       file_close(filehandle);

       // the log was closed

}

 

function init_startup()

{

       while (!key_e) // press "E" to end the logging process for good

       {

               // press "L" to log player's current x y z pan tilt roll

               if (key_l) // or use something like "if(time_to_write_to_log)" here

               {

                       while (key_l) {wait (1);} // wait until the "L" key is released

                       enable_log = 1; // enable logging

                       wait (1); // for a frame

                       enable_log = 0; // and then stop logging

               }

               wait (1);

       }

}

 

 

Q: I'm trying to add scripts that come with GameStudio to my project, but there are a few scripts which can not be opened, even though the files are already there. Did anyone else encounter this problem or knows a way to overcome it?

A: First of all, make sure that you are including the new scripts using quotation marks, just like in the picture below.

 

aum74_faq1

 

If you already did that, the problem lies somewhere else: your scripts are run by lite-C's (free or full) script editor and not by GameStudio's Sed, which is updated more often. Choose Help -> About and you should see a window that looks like this:

 

aum74_faq2

 

If the default script editor that you are using is the one that comes with lite-C (free or full version), the "About" box will show an older version of the program:

 

aum74_faq3

 

If this is the case, you are right: the older version will error out as you have indicated. The solution is simple: open your GameStudio scripts using GameStudio's Sed (make it the default script editor); the problems will go away.

 

 

 

Q: Four entities share the same action. In a separate function I set one to be the active entity using a pointer; if I set its skill1 to any number, all the entities with the same action get the same value for skill1. Is this a bug or am I doing something wrong here? Have pointers changed under lite-C? Skill1 is being treated like a global variable here.

A: You are probably using a global variable instead of a local variable somewhere. Here's an example that highlights only the clicked entity, depending on the value that was set for its skill1.

 

ENTITY* active_ent;

 

BMAP* arrow_pcx = "arrow.pcx";

 

function mouse_startup()

       // allow the player to click the people even if they are 5,000 quants away from its model

       mouse_range = 5000;

       mouse_mode = 1;

       mouse_map = arrow_pcx;

       while (1)

       

               vec_set(mouse_pos, mouse_cursor);

               wait(1);

       }

}

 

function highlight_me()

{

       active_ent = my;

       active_ent.skill1 = 10; // let's set a value for skill1

       if (active_ent.skill1 == 10) // let's check if skill1 is set to 10 for all the entities or not

       {

               while (active_ent.ambient < 100) // this loop should only run for the entity that was clicked

               {

                       active_ent.ambient += 2 * time_step;

                       wait (1);

               }

               active_ent.ambient = 0; // now reset the ambient for the clicked entity

               active_ent.skill1 = 0; // let's reset skill1 as well

       }

}

 

action my_people() // attach this action to your entities

{

       my.emask |= ENABLE_CLICK; // make the entity sensitive to mouse clicking

       my.event = highlight_me;

}

 

 

Q: I'm interested in reading out the sound volume for my music track. Anyone knows a variable or is it possible to get a handle with the volume to a sound or anything else?

A: The easiest method is to play the sound using a variable (and not a fixed value) for its volume. Here's an example that allows you to increase and decrease the volume of a music track and displays its value using a panel.

 

var track_volume = 50;

 

PANEL* test_pan =

{

       digits(160, 180, 4, *, 1, track_volume);

       flags = visible;

}

 

function soundtrack_startup()

{

       var track_handle;

       // the track starts playing at an initial volume of 50

       // use the same mechanism for any other sound (replace media_loop with media_play if you need to)

       track_handle = media_loop("mymusic.wav", NULL, track_volume);

       while (1)

       {

               if (key_m) // press the "M" key to increase the volume

               {

                       track_volume += 2 * time_step; // 2 gives the speed of the volume increase

               }

               if (key_n) // press the "N" key to decrease the volume

               {

                       track_volume -= 2 * time_step; // 2 gives the speed of the volume increase

               }

               // don't allow the volume to go below 1 or grow over 100

               track_volume = clamp(track_volume, 1, 100);

               media_tune(track_handle, track_volume, 100, 0);

               wait (1);

       }

}

 

 

Q: My game includes 5 important entities; I'd like to display their health values on top of their heads at all times. Can you help?

A: Here's an example of 5 entities that move in random directions, decrease their health when they collide with each other or with a wall and display their random health values above their heads.

 

var entity_number = 0;

var health1 = 90; // set different health values for the entities (or not)

var health2 = 80;

var health3 = 70;

var health4 = 60;

var health5 = 50;

 

PANEL* health1_pan =

{

       digits(0, 0, 3, *, 1, health1);

       flags = visible;

}

 

PANEL* health2_pan =

{

       digits(0, 0, 3, *, 1, health2);

       flags = visible;

}

 

PANEL* health3_pan =

{

       digits(0, 0, 3, *, 1, health3);

       flags = visible;

}

 

PANEL* health4_pan =

{

       digits(0, 0, 3, *, 1, health4);

       flags = visible;

}

 

PANEL* health5_pan =

{

       digits(0, 0, 3, *, 1, health5);

       flags = visible;

}

 

function health_ent1()

{

       VECTOR temp_pos;

       while (1)

       {

               health1 = my.skill40;

               vec_set (temp_pos, my.x); // copy the xyz coordinates of the entity to temp_pos

               temp_pos.z += 60; // play with this value

               vec_to_screen (temp_pos, camera); // convert the 3D position to 2D screen coordinates

               health1_pan.pos_x = temp_pos.x; // and then set the position of the text

               health1_pan.pos_y = temp_pos.y; // on x and y

               wait (1);

       }                

}

 

function health_ent2()

{

       VECTOR temp_pos;

       while (1)

       {

               health2 = my.skill40;

               vec_set (temp_pos, my.x); // copy the xyz coordinates of the entity to temp_pos

               temp_pos.z += 60; // play with this value

               vec_to_screen (temp_pos, camera); // convert the 3D position to 2D screen coordinates

               health2_pan.pos_x = temp_pos.x; // and then set the position of the text

               health2_pan.pos_y = temp_pos.y; // on x and y

               wait (1);

       }                

}

 

function health_ent3()

{

       VECTOR temp_pos;

       while (1)

       {

               health3 = my.skill40;

               vec_set (temp_pos, my.x); // copy the xyz coordinates of the entity to temp_pos

               temp_pos.z += 60; // play with this value

               vec_to_screen (temp_pos, camera); // convert the 3D position to 2D screen coordinates

               health3_pan.pos_x = temp_pos.x; // and then set the position of the text

               health3_pan.pos_y = temp_pos.y; // on x and y

               wait (1);

       }                

}

 

function health_ent4()

{

       VECTOR temp_pos;

       while (1)

       {

               health4 = my.skill40;

               vec_set (temp_pos, my.x); // copy the xyz coordinates of the entity to temp_pos

               temp_pos.z += 60; // play with this value

               vec_to_screen (temp_pos, camera); // convert the 3D position to 2D screen coordinates

               health4_pan.pos_x = temp_pos.x; // and then set the position of the text

               health4_pan.pos_y = temp_pos.y; // on x and y

               wait (1);

       }                

}

 

function health_ent5()

{

       VECTOR temp_pos;

       while (1)

       {

               health5 = my.skill40;

               vec_set (temp_pos, my.x); // copy the xyz coordinates of the entity to temp_pos

               temp_pos.z += 60; // play with this value

               vec_to_screen (temp_pos, camera); // convert the 3D position to 2D screen coordinates

               health5_pan.pos_x = temp_pos.x; // and then set the position of the text

               health5_pan.pos_y = temp_pos.y; // on x and y

               wait (1);

       }                

}

 

// this function runs whenever the entity collides with the wall or with another entity

function decrease_health()

{

       my.pan += 90 - random(180);

       my.skill40 -= 2 - random(1);

}

 

action important_guy()

{

       var anim_percentage;

       // make the entity sensitive to collisions with other entities or level blocks

       my.emask |= ENABLE_IMPACT | ENABLE_ENTITY | ENABLE_BLOCK;

       my.event = decrease_health;

       entity_number += 1; // get a unique entity number

         my.skill99 = entity_number; // and store it inside skill99 (don't use skill99 for anything else)

       if (my.skill99 == 1)

       {

               my.skill40 = health1;

               health_ent1();

       }

       if (my.skill99 == 2)

       {

               my.skill40 = health2;

               health_ent2();

       }

       if (my.skill99 == 3)

       {

               my.skill40 = health3;

               health_ent3();

       }

       if (my.skill99 == 4)

       {

               my.skill40 = health4;

               health_ent4();

       }

       if (my.skill99 == 5)

       {

               my.skill40 = health5;

               health_ent5();

       }

       while (1)

       {

               c_move (my, vector(3 * time_step, 0, 0), nullvector, GLIDE); // "5" controls the walking speed

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

                   anim_percentage += 4 * time_step; // "6" controls the "walk" animation speed

               wait (1);

       }

}

 

aum74_faq4

 

 

Q: Can I have an entity that is controlled using the arrow keys and acts like a magnet for some other entities?

A: You can use c_scan to do that; here's an example:

 

 

function got_scanned()

{

       my.event = NULL; // the entities will follow the magnet from now on

       VECTOR temp_pos;

       while (1)

       {

               if (vec_dist (player.x, my.x) > 50) // the object isn't close enough to the magnet?

               {

                       vec_set(temp_pos, player.x);

                       vec_sub(temp_pos, my.x);

                       vec_to_angle(my.pan, temp_pos);

                       my.tilt = 0;

                       // the entity is oriented towards the magnet here

                       c_move (my, vector(3 * time_step, 0, 0), nullvector, GLIDE);        

               }

               wait (1);

       }

}

 

// the entities that have this action attached to them are sensitive to the magnet entity

action sensitive_ents()

{

       my.emask |= ENABLE_SCAN; // make this entity sensitive to scanning

       my.event = got_scanned;

}

 

action my_magnet() // attach this action to your entity

{

       player = my; // I'm the player

       while (1)

       {

               // move the player using the cursor keys

               c_move (my, vector(10 * (key_cuu - key_cud) * time_step, 6 * (key_cul - key_cur) * time_step, 0), nullvector, GLIDE);

               vec_set (camera.x, player.x); // use player's x and y for the camera as well

               camera.z += 500; // place the camera 500 quants above the player on the z axis

               camera.tilt = -90;

               // make the magnet active on a radius of up to 200 quants around it

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

               wait (1);

       }

}

 

aum74_faq5