Questions from the forum

Top  Previous  Next

Q: I have a problem with an weapon code from the Aum; it waves as the player moves, but when the player stops the gun will stop at a different position every time.

A: Here's the updated weapon action that fixes the problem.

 

action players_weapon() // place your weapon model in the level and attach it this action

{

       weapon1 = my; // I'm weapon1

       VECTOR player1_pos; // stores the initial position of the player

       VECTOR player2_pos; // stores the position of the player after a frame

       VECTOR weapon_offset;

       var weapon_height;

       set (my, PASSABLE); // the weapon model is passable

       while (!player) {wait (1);} // wait until the player is created

       while (vec_dist (player.x, my.x) > 50) {wait (1);} // wait until the player comes close to pick up the weapon

       my.roll = 0;

       while (1)

       {

               vec_set (player1_pos.x, player.x); // store the initial player position

               // place the weapon 30 quants in front, 18 quants to the right and 20 quants below camera's origin

               vec_set (weapon_offset.x, vector (front_offset, -18, -10));

               if (vec_dist (player1_pos.x, player2_pos.x) != 0) // the player has moved during the last frame?

               {

                       weapon_height += 30 * time_step; // then offset weapon_height (30 = weapon waving speed)

                       weapon_offset.z += 0.3 * sin(weapon_height); // (0.3 = weapon waving amplitude)

               }

               else

               {

                       weapon_height = 0;

               }

               // rotate weapon_offset according to the camera angles

               vec_rotate (weapon_offset.x, vector (camera.pan, camera.tilt, 0));

               vec_add (weapon_offset.x, camera.x); // add the camera position to weapon_offset

               vec_set (my.x, weapon_offset.x); // set weapon's coords to weapon_offset

               my.pan = camera.pan; // use the same camera angles for the weapon

               my.tilt = camera.tilt;

               vec_set (player2_pos.x, player.x); // store the new player coordinates after 1 frame

               wait (1);

       }

}

 

 

Q: How do I set up a piece of code, so that when I close the game using Esc it saves the current date to a text file automatically?

A: Here's a fully working example.

 

STRING* log_str = "                     "; // ex: August 25, 2011

STRING* temp_str = "  ";  // temporary string

 

function write_data()

{

  var filehandle;

  filehandle = file_open_write("date.txt");

  if (sys_month == 1)

         str_cpy(log_str, "January ");

  if (sys_month == 2)

         str_cpy(log_str, "February ");

  if (sys_month == 3)

         str_cpy(log_str, "March ");

  if (sys_month == 4)

         str_cpy(log_str, "April ");

  if (sys_month == 5)

         str_cpy(log_str, "May ");

  if (sys_month == 6)

         str_cpy(log_str, "June ");

  if (sys_month == 7)

         str_cpy(log_str, "July ");

  if (sys_month == 8)

         str_cpy(log_str, "August ");

  if (sys_month == 9)

         str_cpy(log_str, "September ");

  if (sys_month == 10)

         str_cpy(log_str, "October ");

  if (sys_month == 11)

         str_cpy(log_str, "November ");

  if (sys_month == 12)

         str_cpy(log_str, "December ");

  if (sys_day < 10)

  {

         str_cat(log_str, "0");

  }

  str_for_num(temp_str, sys_day);

  str_cat(log_str, temp_str);

  str_cat(log_str, ", ");

  str_for_num(temp_str, sys_year);

  str_cat(log_str, temp_str);   

  file_str_write(filehandle, log_str);

  file_close(filehandle);

  wait (-1); // wait for a second

  sys_exit(NULL); // shut down the engine

}

 

function init_startup()

{

  on_esc = write_data;        

}

 

 

Q: Anyone knows where I can download free sky cubes?

A: Acknex Unlimited contains several quality sky cubes; some of the direct download links are listed below.

 

http://www.opserver.de/coni_users/web_users/pirvu/au/textures/zips/skies.zip

http://www.opserver.de/coni_users/web_users/pirvu/au/textures/zips/sky.zip

http://www.opserver.de/coni_users/web_users/pirvu/au/textures/zips/skyboxes.zip

http://www.opserver.de/coni_users/web_users/pirvu/au/textures/zips/skycube5.zip

 

 

 

Q: How can I pass a variable into an action like I would with a function?

A: Use an entity pointer - here's a simple example.

 

ENTITY* my_entity;

 

action tall_guy()

{

       var height_data;

       my_entity = my;

       while (1)

       {

               height_data = my.skill99;        

               my.scale_z = 1 + my.skill99;

               wait (1);

       }

}

 

function init_startup()

{

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

       while (1)

       {

               my_entity.skill99 = random(10); // pass the data using the 99th skill of the entity

               wait (-2); // change the height of the entity every 2 seconds

       }

}

 

 

Q: I would like to have a button that toggles my game from full screen to window mode and back. How can I do that?

A: There you go.

 

var toggled = 0;

 

BMAP* pointer_tga = "pointer.tga";

BMAP* picture1_pcx = "picture1.pcx";

BMAP* picture2_pcx = "picture2.pcx";

 

function toggle_fullscreen();

 

function mouse_startup()

       mouse_mode = 2;

       mouse_map = pointer_tga;

       while (1)

       

               vec_set(mouse_pos, mouse_cursor);

               wait(1);

       }

}

 

PANEL* time_pan =

{

       // no need to use a bitmap for the panel this time

       layer = 15;

       button(40, 10, picture2_pcx, picture1_pcx, picture2_pcx, toggle_fullscreen, NULL, NULL);

       flags = SHOW;

}

 

function toggle_fullscreen()

{

       toggled +=1;

       wait (3);

       if (toggled % 2)

       {

               video_switch(0,0,2); // change to window mode

       }

       else

       {

               video_switch(0,0,1); // change to full screen mode

       }

}

 

 

Q: Is there a simple way to trigger an event when a mouse button is released?

A: Here's a simple example.

 

var toggle_id;

 

function toggle_ambient() // runs each time the player presses the right mouse button

{

       while (mouse_right) {wait (1);} // wait until the right mouse button is released

       // the line of code below will be executed as soon as the player releases the right mouse button

       toggle_id += 1;

       if (toggle_id % 2)

       {

               camera.ambient = 100;        

       }

       else

       {

               camera.ambient = -100;        

       }

}

 

function mouse_startup()

{

       on_mouse_right = toggle_ambient;        

}

 

 

Q: I need the code for an NPC that rotates towards the player at all times, rotating gently until it reaches the final position. Can you help?

A: There you go.

 

action npc1()

{

       VECTOR temp;

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

       // wait until the player model is loaded - your player action should include a player = my; line of code

       while (player) // run this loop for as long as the player pointer exists

       {

               vec_set (temp.x, player.x);

               vec_sub (temp.x, my.x);

               vec_to_angle (my.skill99, temp);

                // rotate the npc towards the player slowly - 0.05 gives the rotation speed

               my.pan -= ang(my.pan - my.skill99) * 0.05 * time_step;

               wait (1);

       }

}

 

 

Q: I would like to play several sounds randomly, but I don't want them to overlap - is this possible?

A: Sure.

 

SOUND* sound1_wav = "sound1.wav";

SOUND* sound2_wav = "sound2.wav";

SOUND* sound3_wav = "sound3.wav";

SOUND* sound4_wav = "sound4.wav";

SOUND* sound5_wav = "sound5.wav";

 

var soundhandle, sound_id;

 

function sounds_startup()

{

       while (1)

       {

               sound_id = integer(random(5));

               switch (sound_id)

               

                       case 0:   

                               soundhandle = snd_play(sound1_wav, 80, 0);

                               break; 

                       case 1:   

                               soundhandle = snd_play(sound2_wav, 80, 0);

                               break; 

                       case 2:    

                               soundhandle = snd_play(sound3_wav, 80, 0);

                               break; 

                       case 3:   

                               soundhandle = snd_play(sound4_wav, 80, 0);

                               break; 

                       case 4:    

                               soundhandle = snd_play(sound5_wav, 80, 0);

                               break; 

               }

               while (snd_playing(soundhandle)) {wait (1);}

               wait (1);

       }

}

 

 

Q: I would like to congratulate the player at the end of the level, displaying a bunch of "congratulations" messages. Is there a quick way of doing that without having to create lots of panels, etc?

A: Of course.

 

function create_panels()

{

       var digits_value, congrats_x, congrats_y, i;

       for (i = 0; i < 10; i++)

       {

               congrats_x = 600 - random(300);

               congrats_y = 200 + random(400);

               PANEL* my_panel = pan_create(NULL,0);

               FONT* my_font = font_create("Arial#20b");  

               pan_setstring(my_panel, 0, congrats_x, congrats_y, my_font, str_create("Congratulations!"));   

               set(my_panel,SHOW);

       }

}

 

function panels_startup()

{

       on_p = create_panels; // press the "P" key to create 10 congratulation panels at random positions on the screen

}

 

 

Q: I would like the player to be attacked only by the enemies that are closer to a certain distance to it. How can I do that?

A: Here's a simple example.

 

function scan_event()

{

       if (event_type == EVENT_SCAN)

       {

               my.skill99 = 1;

       }        

}

 

action my_enemies() // attach this action to your enemies

{

       var anim_percentage;

       VECTOR temp;

       my.emask |= ENABLE_SCAN;

       my.event = scan_event;

       while (1)

       {

               while (my.skill99 == 0) {wait (1);}

               vec_set(temp.x, player.x);

               vec_sub(temp.x, my.x);

               vec_to_angle(my.pan, temp); // rotate the enemy towards the player

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

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

                  anim_percentage += 2 * time_step;

               my.skill99 = 0;

               wait (1);

       }

}

 

action players_code() // sample player code

{

       var anim_percentage, distance_to_ground;

       VECTOR movement_speed, temp;

       player = my;

         while (1)

         {

               c_scan(my.x, my.pan, vector(360, 0, 300), SCAN_ENTS | SCAN_LIMIT | IGNORE_ME);

               camera.x = player.x - 250 * cos(player.pan);

               camera.y = player.y - 250 * sin(player.pan);

               camera.z = player.z + 150;

               camera.pan = player.pan;

               camera.tilt = -20;

               my.pan += 6 * (key_a - key_d) * time_step;

               vec_set (temp, my.x);

               temp.z -= 5000;

               distance_to_ground = c_trace (my.x, temp.x, IGNORE_ME | USE_BOX);

               movement_speed.x = 5 * (key_w - key_s) * time_step;

               movement_speed.y = 0;

               movement_speed.z = - (distance_to_ground - 17); // 17 = experimental value

               movement_speed.z = maxv (-35 * time_step, movement_speed.z); // 35 = falling speed

               c_move (my, movement_speed.x, nullvector, GLIDE);

               if (!key_w && !key_s)

               {

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

               }

            else // the player is moving?

               {

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

               }

            anim_percentage += 5 * time_step; // 5 = animation speed

            wait (1);

       }

}